【问题标题】:How to pass value in Viewmodel to other ViewModel with mvvmcross UWP如何使用 mvvmcross UWP 将 Viewmodel 中的值传递给其他 ViewModel
【发布时间】:2018-02-03 06:45:36
【问题描述】:

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

有人知道怎么做吗?

谢谢,

【问题讨论】:

    标签: c# xamarin uwp mvvmcross


    【解决方案1】:

    您可以使用IMvxNavigationService 来传递和返回对象。完整文档位于: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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多