1

参考 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。这是我第一次使用该插件,所以请原谅我的愚蠢问题。

4

1 回答 1

1

MessengerPublishSubscribe方法的使用在https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#Messenger中有介绍

您对问题代码中调用的使用对我来说看起来不错 - 您的导航服务发布的任何消息都应该被所有现有PurchasesDataEntryViewModel的 s接收


更新在您对以下内容发表评论后:

当我发布消息时,我期望我的调试器触发 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 有Intents,WP 有Uris,iOS 有代码基于转换等)。

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

于 2013-11-06T07:13:21.723 回答