2013-08-23 81 views
7

Tôi muốn tạo một plugin thực hiện chức năng như dịch vụ thông báo.Sử dụng Thông báo trên Android với MvvmCross

Vì vậy, những gì tôi đang làm vào lúc này là một cái gì đó như thế này:

 var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity; 
     var builder = new NotificationCompat.Builder(activity.ApplicationContext) 
      .SetContentTitle(title) 
      .SetSmallIcon(Resource.Drawable.Icon) 
      .SetContentText(message); 
     var notificationManager = (NotificationManager)activity.ApplicationContext.GetSystemService(Context.NotificationService); 
     notificationManager.Notify(0, builder.Build()); 

này hoạt động tốt và không hiển thị các thông báo như nó sẽ hiển thị. Bước tiếp theo là tôi muốn điều hướng từ thông báo đến hoạt động của mình. Có nghĩa là trong MvvmCross tôi muốn điều hướng đến ViewModel của mình.

Nhưng làm cách nào để đóng gói ShowViewModel < ...>() - Lệnh vào thông báo này? Điều này thậm chí có thể?

Trên Android gốc tôi sẽ tạo một PendingIntent trỏ đến Hoạt động của tôi mà tôi muốn hiển thị.

Bất kỳ ý tưởng nào? Dấu? Tiền boa? :-)

Trả lời

7

Trình hiển thị MvvmCross mặc định trên Android sử dụng Intent giây để điều hướng. Chúng được tạo theo phương thức Intent GetIntentFor(MvxViewModelRequest request) trong giao diện IMvxAndroidViewModelRequestTranslator.

Theo mặc định này được thực hiện trong vòng: MvxAndroidViewsContainer.cs#L117

public virtual Intent GetIntentFor(MvxViewModelRequest request) 
    { 
     var viewType = GetViewType(request.ViewModelType); 
     if (viewType == null) 
     { 
      throw new MvxException("View Type not found for " + request.ViewModelType); 
     } 

     var converter = Mvx.Resolve<IMvxNavigationSerializer>(); 
     var requestText = converter.Serializer.SerializeObject(request); 

     var intent = new Intent(_applicationContext, viewType); 
     intent.PutExtra(ExtrasKey, requestText); 

     AdjustIntentForPresentation(intent, request); 

     intent.AddFlags(ActivityFlags.NewTask); 
     return intent; 
    } 

Nếu bạn cần phải tạo ra Intent s cho các mục đích khác (ví dụ để sau đó tiếp tục và tạo PendingIntent s) sau đó bạn có thể Resolve và gọi giao diện này bản thân bạn.

var request = MvxViewModelRequest<MyViewModel>.GetDefaultRequest(); 
    request.PresentationValues = new Dictionary<string, string>() { 
     { "life", "42" } 
    }; 
    var translator = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>(); 
    var intent = translator.GetIntentFor(request); 
    var pending = PendingIntent.GetActivity (context, 0, intent, 0); 

Để biết thêm thông tin về tạo MvxViewModelRequest đối tượng, xem thêm quá tải ShowViewModel phương pháp trong MvxNavigatingObject.cs

+0

Bạn có biết làm thế nào một cái gì đó như thế này sẽ được thực hiện trong V1? – Jake