2015-05-13 11 views
5

Tôi đang có một Ứng dụng dựa trên XamarinForms.Chuyển dữ liệu từ dịch vụ Android đến ContentPage trong ứng dụng dựa trên biểu mẫu Xamarin

Một dịch vụ nền mà tôi đã tạo trong dự án Android và dịch vụ đó muốn gửi dữ liệu đến ContentPage (trong PCL) được hiển thị cho người dùng.

Làm cách nào để chuyển dữ liệu vào ContentPage (Từ dự án xx.Droid đến PCL)?

Một giải pháp là:

  • Để Tạo lớp trong PCL với biến tĩnh, mà sẽ được truy cập từ dự án xxx.Droid (ví dụ var TEMP_VAR.).
  • Cập nhật giá trị của biến tĩnh đó (TEMP_VAR) từ lớp dịch vụ từ dự án xxx.Droid.
  • Cần tạo Trình thông báo trên biến tĩnh đó (TEMP_VAR)
  • Cập nhật trang nội dung bằng Cơ chế MessageCenter nếu cần.

Nếu có giải pháp nào tốt hơn, bạn có thể vui lòng cung cấp cho tôi không?

Trả lời

5

này có thể đạt được bằng cách sử dụng khái niệm về C#

  • dịch vụ phụ thuộc
  • Event

Cần phải có 4 lớp cho một thực hiện như:

  1. Interface trong PCL (ví dụ: CurrentLocationService.cs) với các trình xử lý sự kiện được định nghĩa trong nó.

namespace NAMESPACE 
 
{ 
 
\t public interface CurrentLocationService 
 
\t { 
 
\t \t void start(); 
 

 
\t \t event EventHandler<PositionEventArgs> positionChanged; 
 
\t } 
 
}

  1. Thực hiện giao diện của PCL trong dự án xxx.Droid (ví dụ CurrentLocationService_Android.cs) sử dụng dịch vụ phụ thuộc

class CurrentLocationService_Android : CurrentLocationService 
 
{ 
 

 
\t public static CurrentLocationService_Android mySelf; 
 

 
\t public event EventHandler<PositionEventArgs> positionChanged; 
 
\t 
 
\t 
 
\t public void start() 
 
\t { 
 
\t \t mySelf = this; 
 
\t \t Forms.Context.StartService(new Intent(Forms.Context, typeof(MyService))); 
 

 
\t } 
 

 
\t public void receivedNewPosition(CustomPosition pos) 
 
\t { 
 
\t \t positionChanged(this, new PositionEventArgs(pos)); 
 
\t } 
 

 
}

  1. ContentPage trong PCL - sẽ có đối tượng triển khai giao diện. Object có thể thu được bằng cách

public CurrentLocationService LocationService 
 
{ 
 
\t get 
 
\t { 
 
\t \t if(currentLocationService == null) 
 
\t \t { 
 
\t \t \t currentLocationService = DependencyService.Get<CurrentLocationService>(); 
 
\t \t \t currentLocationService.positionChanged += OnPositionChange; 
 
\t \t } 
 
\t \t return currentLocationService; 
 
\t } 
 
    
 

 
} 
 

 
private void OnPositionChange(object sender, PositionEventArgs e) 
 
{ 
 
\t Debug.WriteLine("Got the update in ContentPage from service "); 
 
}

    dịch vụ
  1. nền trong dự án xxx.Droid. Dịch vụ này sẽ có tham chiếu đến việc triển khai dịch vụ phụ thuộc CurrentLocationService.cs

[Service] 
 
    public class MyService : Service 
 
    { 
 
     public string TAG = "MyService"; 
 
     
 
     public override IBinder OnBind(Intent intent) 
 
     { 
 
      throw new NotImplementedException(); 
 
     } 
 

 
     
 

 
     public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId) 
 
     { 
 
      Log.Debug(TAG, TAG + " started"); 
 

 
      doWork(); 
 

 
      return StartCommandResult.Sticky; 
 
     } 
 

 
     public void doWork() 
 
     { 
 
      var t = new Thread(
 
       () => 
 
       { 
 
        Log.Debug(TAG, "Doing work"); 
 
        Thread.Sleep(10000); 
 
        Log.Debug(TAG, "Work completed"); 
 

 
        if(CurrentLocationService_Android.mySelf != null) 
 
        { 
 
         CustomPosition pos = new CustomPosition(); 
 
         pos.update = "Finally value is updated"; 
 
         CurrentLocationService_Android.mySelf.receivedNewPosition(pos); 
 
         
 
        } 
 

 
        StopSelf(); 
 
       }); 
 
      t.Start(); 
 
     } 
 

 
    }

Lưu ý: PositionEventArgs lớp cần phải được tạo ra theo cách sử dụng để vượt qua trên dữ liệu giữa các dịch vụ và ContentPage.

Điều này phù hợp với tôi như sự quyến rũ.

Hy vọng điều này sẽ hữu ích cho bạn.

Các vấn đề liên quan