如何使用 mvvmcross UWP 将 Viewmodel 中的值传递给其他 ViewModel

How to pass value in Viewmodel to other ViewModel with mvvmcross UWP

我想知道如何使用 mvvcross 和 uwp 将视图模型的值发送到另一个视图模型

有人知道怎么做吗?

谢谢,

您可以使用 IMvxNavigationService 传递和 return 对象。完整文档位于:https://www.mvvmcross.com/documentation/fundamentals/navigation?scroll=26

在您的 ViewModel 中,这可能看起来像:

public class MyViewModel : MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;
    public MyViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;
    }

    public override void Prepare()
    {
        //Do anything before navigating to the view
    }

    public async Task SomeMethod()
    {
        _navigationService.Navigate<NextViewModel, MyObject>(new MyObject());
    }
}

public class NextViewModel : MvxViewModel<MyObject>
{
    public override void Prepare(MyObject parameter)
    {
        //Do anything before navigating to the view
        //Save the parameter to a property if you want to use it later
    }

    public override async Task Initialize()
    {
        //Do heavy work and data loading here
    }
}

使用 IMvxMessenger 您可以在没有连接的情况下发送值:https://www.mvvmcross.com/documentation/plugins/messenger?scroll=1446

public class LocationViewModel
    : MvxViewModel
{
    private readonly MvxSubscriptionToken _token;

    public LocationViewModel(IMvxMessenger messenger)
    {
        _token = messenger.Subscribe<LocationMessage>(OnLocationMessage);
    }

    private void OnLocationMessage(LocationMessage locationMessage)
    {
        Lat = locationMessage.Lat;
        Lng = locationMessage.Lng;
    }

    // remainder of ViewModel
}