简体   繁体   English

MVVMCross Messenger插件Android

[英]MVVMCross Messenger Plugin Android

Reffering to N=9 what I would like to do is in place of ILocationService I have my own INavigationService and a Navigation Service class. 相对于N = 9,我想代替ILocationService,我有自己的INavigationService和Navigation Service类。

  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);
    }

}

And this is my Messenger class: 这是我的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;
    }

What I want to achieve is to send the above values to my subscriber PurchasesDataEntryViewModel 我要实现的是将上述值发送给我的订户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;
        .
        .
        .

    }

} }

I cannot make it to work. 我无法使其正常工作。 I need to understand the interaction and lifecycle of Publish/Subscribe. 我需要了解发布/订阅的交互作用和生命周期。 How do the two viewmodels will understand how to communicate. 这两个视图模型将如何理解如何进行通信。 I need somehow to tell my Messenger service that I want to send parameters from ViewModel A to ViewModel B through the messenger class. 我需要某种方式告诉Messenger服务,我想通过Messenger类将参数从ViewModel A发送到ViewModelB。 This is the first time I use the plugin so forgive me for the stupid questions. 这是我第一次使用该插件,因此请原谅我的愚蠢问题。

The use of the Messenger class Publish and Subscribe methods is introduced in https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#Messenger https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#Messenger中引入了Messenger类的PublishSubscribe方法的使用。

Your use of the calls in the code in the question looks good to me - any message published by your navigation service should be received by all existing PurchasesDataEntryViewModel s 您对问题代码中的调用的使用对我来说很好-所有现有的PurchasesDataEntryViewModel都应接收导航服务发布的任何消息


Update After your comment about: 更新您的评论后:

when I publish the message I'am expecting my debugger to fire the constructor of the PurchasesDataEntryViewModel class 当我发布消息时,我期望调试器触发PurchasesDataEntryViewModel类的构造函数

This is incorrect. 这是不正确的。

Any messenger (MvvmCross or not) can only pass messages between existing objects - it can't create new listeners dynamically. 任何Messenger(无论是否为MvvmCross)都只能在现有对象之间传递消息-无法动态创建新的侦听器。

If you do want a new object created in response to a message, then you will need to subscribe to that message type from a Factory object - and that Factory object will then need to create the new objects in its message handler. 如果确实要创建一个新对象来响应消息,则需要从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;
             // ...

         // ...
    }
}

For UI navigation, you will need to make sure that whatever Factory objects you write will also create the View as well as the ViewModel . 对于UI导航,您需要确保您编写的任何Factory对象也将创建View以及ViewModel

In practice, if you want to write your own navigation service, then you will need to write a navigation service specific to each platform you support - as each platform has different navigation techniques and lifecycles (Android has Intent s, WP has Uri s, iOS has code-based transitions, etc). 实际上,如果您要编写自己的导航服务,则需要编写特定于所支持的每个平台的导航服务-因为每个平台具有不同的导航技术和生命周期(Android具有Intent ,WP具有Uri ,iOS具有基于代码的过渡等)。

Since MvvmCross already provides a cross-platform navigation service, you may find it easier to use this and to adapt it using a custom presenter - this is discussed in: 由于MvvmCross已经提供了跨平台导航服务,因此您可能会发现使用自定义演示器更容易使用和调整它-在以下内容中进行了介绍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM