在MVVMCross中,是否可以关闭视图模型并将值传递回导航堆栈中的上一个视图模型?

时间:2017-05-01 22:52:45

标签: c# xamarin mvvm mvvmcross

考虑以下示例。我有三种视图模型,ViewModel_AViewModel_BViewModel_Values

我希望能够从ViewModel_ValuesViewModel_A导航到ViewModel_B,从ViewModel_Values中选择一个值,然后将该值返回到调用视图模型。

有没有办法将参数传递给导航堆栈中的先前视图模型,以便我可以简单地调用ViewModel_Values.Close(this),从而确保ViewModels_Values与任何其他视图模型分离并可以使用使用任意“父”视图模型?

3 个答案:

答案 0 :(得分:6)

MvvmCross 5以后

从MvvmCross 5,您可以使用新的IMvxNavigationService,让您拥有更丰富的导航。其中一个新功能是可以在导航到另一个ViewModel后等待来自另一个ViewModel的值,并且应该是在MvvmCross 5而不是Messenger之后采取的方法,例如:

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

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

    public async Task SomeMethod()
    {
        var result = await _navigationService.Navigate<ViewModel_Values, MyObject, MyReturnObject>(new MyObject());
        //Do something with the result MyReturnObject that you get back
    }
}

public class ViewModel_Values : MvxViewModel<MyObject, MyReturnObject>
{
    private readonly IMvxNavigationService _navigationService;
    public ViewModel_Values(IMvxNavigationService navigation)
    {
        _navigationService = navigationService;
    }

    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
    }

    public async Task SomeMethodToClose()
    {
        // here you returned the value
        await _navigationService.Close(this, new MyReturnObject());
    }
}

更多信息here HIH

答案 1 :(得分:4)

使用消息中心。这是示例代码。

//for trigger
MessagingCenter.Send<object> (this, "Hi");

//put this where you want to receive your data
MessagingCenter.Subscribe<object> (this, "Hi", (sender) => {
    // do something whenever the "Hi" message is sent
});

答案 2 :(得分:1)

安装&amp;使用MvxMessenger插件是在MvvmCross中解耦视图模型通信的好方法 -

在您的情况下,您可以设置新消息 -

public class ValuesChangedMessage : MvxMessage
{      
    public ValuesChangedMessage(object sender, int valuea, string valueb)
        : base(sender)
    {
        Valuea = valuea;
        Valueb = valueb;        
    }

    public int Valuea { get; private set; }
    public string Valueb { get; private set; }
}

在ViewModel_Values中,您将使用 -

执行/发布您的UX更改
_mvxMessenger.Publish<ValuesChangedMessage>(new ValuesChangedMessage(this, 1, "boo!"));

在ViewModel_A,ViewModel_B中,您可以订阅并对其进行操作(因为当您从ViewModel_Values中推送ViewModel_Values时,您的ViewModel A / B仍会在导航堆栈中,因此他们可以收到消息) -

private MvxSubscriptionToken _messageToken;              

_messageToken = _mvxMessenger.Subscribe<ValuesChangedMessage>(async message =>
        {
            // use message.Valuea etc ..
        });

此处有更多信息 -

https://www.mvvmcross.com/documentation/plugins/messenger?scroll=644 https://www.youtube.com/watch?feature=player_embedded&v=HQdvrWWzkIk