gpt4 book ai didi

c# - MVVMCross Messenger 插件安卓

转载 作者:行者123 更新时间:2023-11-30 03:17:22 25 4
gpt4 key购买 nike

引用 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;
.
.
.

}

我不能让它工作。我需要了解发布/订阅的交互和生命周期。两个viewmodel怎么理解就会怎么沟通。我需要以某种方式告诉我的 Messenger 服务我想通过 Messenger 类将参数从 ViewModel A 发送到 ViewModel B。这是我第一次使用该插件,请原谅我的愚蠢问题。

最佳答案

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

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


更新在您评论之后:

when I publish the message I'am expecting my debugger to fire the constructor of the PurchasesDataEntryViewModel class

这是不正确的。

任何信使(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 已经提供了一个跨平台的导航服务,您可能会发现它更容易使用并使用自定义演示器对其进行调整 - 这在以下内容中进行了讨论:

关于c# - MVVMCross Messenger 插件安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19799048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com