Android MVVMCross Messenger插件

时间:2013-11-05 21:10:58

标签: c# android mvvmcross

响应N = 9,我想要做的是取代ILocationService我有自己的INavigationService和导航服务类。

  public class NavigationService :INavigationService
{
    private readonly IMvxMessenger _navigator;

    public NavigationService(IMvxMessenger navigator)
    {
        _navigator = navigator;
    }

    public void OnNavigation(PurchasesDataEntryViewModel vm)
    {
        var navigationMessage = new NavigationMessage(this,vm);
        _navigator.Publish(navigationMessage);
    }

}

这是我的Messenger课程:

   public class NavigationMessage : MvxMessage
{
    public string ShopID { private set; get; }
    public int TotalStock { private set; get; }
    public int TotalPurchases { private set; get; }
    public int Stock1 { private set; get; }
    public int Stock2 { private set; get; }
    public int Stock3 { private set; get; }
    public int Purch1 { private set; get; }
    public int Purch2 { private set; get; }
    public int Purch3 { private set; get; }
    public string Name { private set; get; }
    public string CPrice { private set; get; }
    public int CSales { private set; get; }
    public string BrandID { private set; get; }
    public string CatID { private set; get; }
    public int LadgePur { private set; get; }
    public decimal LPrice { private set; get; }
    public int LPurch { private set; get; }
    public int LSales { private set; get; }
    public int LStock { private set; get; }
    public int LStock1 { private set; get; }
    public string MeasureID { private set; get; }


    public NavigationMessage(object sender, PurchasesDataEntryViewModel vm)
        : base(sender)
    {
        ShopID = vm.ShopID;
        TotalStock = vm.TotalStock;
        TotalPurchases = vm.TotalPurchases;
        Stock1 = vm.Stock1;
        Stock2 = vm.Stock2;
        Stock3 = vm.Stock3;
        //Purch1 = vm.Purch1;
        //Purch2 = vm.Purch2;
        //Purch3 = vm.Purch3;
        Name = vm.Name;
        CPrice = vm.CPrice;
      //  CSales = vm.CSales;
        BrandID = vm.BrandID;
        CatID = vm.CatID;
        LadgePur = vm.LadgePur;
        LPrice = vm.LPrice;
        LPurch = vm.LPurch;
        LSales = vm.LSales;
        LStock = vm.LStock;
        LStock1 = vm.LStock1;
        MeasureID = vm.MeasureID;
    }

我想要实现的是将上述值发送给我的订阅者PurchasesDataEntryViewModel

 private readonly IDataService _dataService;
    private readonly MvxSubscriptionToken _token;

    public PurchasesDataEntryViewModel(IDataService dataService,INavigationService service, IMvxMessenger navigator)
    {
        _dataService = dataService;
        _token = navigator.Subscribe<NavigationMessage>(OnNavigationMessage);
    }

    private void OnNavigationMessage(NavigationMessage navigationMessage)
    {
        ShopID = navigationMessage.ShopID;
        .
        .
        .

    }

}

我无法让它发挥作用。我需要了解发布/订阅的交互和生命周期。两个视图模型如何理解如何通信。我需要以某种方式告诉我的Messenger服务我想通过messenger类将参数从ViewModel A发送到ViewModel B.这是我第一次使用这个插件,请原谅我提出的愚蠢问题。

1 个答案:

答案 0 :(得分:1)

https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#Messenger

中引入了MessengerPublishSubscribe方法的使用

您对问题代码中的调用的使用对我来说很好 - 导航服务发布的任何消息都应该由所有现有的PurchasesDataEntryViewModel

接收

更新在您发表评论之后:

  

当我发布消息时,我希望我的调试器触发PurchasesDataEntryViewModel类的构造函数

这是不正确的。

任何信使(MvvmCross与否)都只能在现有对象之间传递消息 - 它无法动态创建新的侦听器。

如果您确实需要为响应消息而创建新对象,那么您需要从Factory对象订阅该消息类型 -​​ 然后Factory对象将需要创建消息处理程序中的新对象。

private readonly MvxSubscriptionToken _token;

public Factory(IMvxMessenger navigator)
{
    _token = navigator.Subscribe<NavigationMessage>(OnNavigationMessage);
}

private void OnNavigationMessage(NavigationMessage navigationMessage)
{
    switch (navigationMessage.NavType)
    {
         case NavType.One:
             var newOne = new One(navigationMessage.Args);
             // use newOne;
             // ...

         // ...
    }
}

对于UI导航,您需要确保您编写的任何Factory对象也会创建View以及ViewModel

实际上,如果你想编写自己的导航服务,那么你需要编写一个特定于你支持的每个平台的导航服务 - 因为每个平台都有不同的导航技术和生命周期(Android有Intent个,WP有Uri s,iOS有基于代码的过渡等。

由于MvvmCross已经提供了跨平台的导航服务,您可能会发现使用它更容易并使用自定义演示器进行调整 - 这将在以下讨论: