Show view from non-view/viewmodel in mvvmcross

不问归期 提交于 2019-12-18 12:24:30

问题


What is the correct way to open a View in mvvmcross from a non-view? From within a viewmodel we would use ShowViewModel<>(..).

Specifically we are responding to a push notification opening the app (with a custom payload) which dictates a view that should be loaded.

We have a hackety workaround just for proof of concept, just wanted to get an idea of the correct MVX approach


回答1:


I don't think there is a 'correct way' - I think it depends on your app and what you need it to do.

For some specific cases - e.g. ViewModel->ViewModel and AppStart - MvvmCross provides some convenient methods:

  • you can call ShowViewModel in MvxViewModel
  • the app start can be overridden to use a hint object - see https://speakerdeck.com/cirrious/appstart-in-mvvmcross

But overall, any class can request a ShowViewModel by calling:

         var viewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();
         viewDispatcher.ShowViewModel(new MvxViewModelRequest(
                                                    viewModelType,
                                                    parameterBundle,
                                                    presentationBundle,
                                                    requestedBy));

Further, there is a base class - MvxNavigatingObject.cs - which can help with this (it's a base class of MvxViewModel and MvxAppStart) - so you can easily provide one or more services like INavigateMyselfService who's implementations inherit from MvxNavigatingObject.

  public interface INavigateMyselfService
  {
      void GoWild(string side);
  }

  public class NavigateMyselfService
     : MvxNavigatingObject
     , INavigateMyselfService
  {
      public void GoWild(string side)
      {
          ShowViewModel<WildViewModel>(new { side = side });
      }
  }



回答2:


http://forums.xamarin.com/discussion/4694/conditionally-call-registerappstart-with-mvvmcross

Check the above link and you will get idea

In my case,I want to launch the app from secondary tile.For this,I have to launch specific page for Secondary tile.

My Initial App Start view model is LoginViewModel and my custom app start view model is HomeViewModel.

I controlled this from App.cs (Core) to MyCustomAppStart class.

Refer the above link



来源:https://stackoverflow.com/questions/16662969/show-view-from-non-view-viewmodel-in-mvvmcross

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!