从插件内部显示 ViewController

Show ViewController from inside plugin

我无法从插件内部显示视图模型(在插件内部创建)。例如,我不想像模态视图一样显示它,我想显示它开发的任何应用程序。如果是使用UINavigation开发的就push,如果是tab就设置

所以,我正在尝试的是:

在插件中创建我的 ViewModel

public partial class MyViewModel : MvxViewController
{
    private string Option1;

    public class Nav
    {
        public string option1 { get; set; }
    }

    public void Init(Nav navigation)
    {
        Option1 = navigation.option1;       }

    public MyViewModel () : base ("MyViewModel", null)
    {
    }

    //My View model..
}

并调用它:

public void CallMyViewModel()
{
        var parameterBundle = new MvxBundle((new MyViewModel.Nav 
        {
            option1 = "option1",
        }.ToSimplePropertyDictionary()));

        var mvxViewModelRequest = new MvxViewModelRequest (typeof(MyViewModel), parameterBundle, null, null);
        Mvx.Resolve<IMvxViewDispatcher> ().ShowViewModel (mvxViewModelRequest)
 }

每当调用 CallMyViewModel 时,我都会收到以下异常:

 Could not find view for Thrust.Plugins.Touch.MyViewModel

有了这个堆栈跟踪:

     at Cirrious.MvvmCross.Views.MvxViewsContainer.GetViewType (System.Type viewModelType) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxTouchViewsContainer.CreateView (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxCanCreateTouchViewExtensionMethods.CreateViewControllerFor (IMvxCanCreateTouchView view, Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.Presenters.MvxTouchViewPresenter.Show (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxTouchViewDispatcher+<>c__DisplayClass4.<ShowViewModel>b__3 () [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxTouchUIThreadDispatcher.RequestMainThreadAction (System.Action action) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxTouchViewDispatcher.ShowViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request) [0x00000] in <filename unknown>:0 
  at Thrust.Plugins.Touch.MvxPluginService.CallMyViewModel (T) [0x0004e] in /Users/gbastos/Documents/Gabriel Docs/Development/ThrustPlugins/Plugins/Thrust.Plugins.Touch/MvxPluginService.cs:471 
  at Thrust.Plugins.Test.Core.ViewModels.FirstViewModel.CallMyViewModel () [0x00047] in /Users/gbastos/Documents/Gabriel Docs/Development/ThrustPlugins/Plugins/Thrust.Plugins.Test.Core/ViewModels/FirstViewModel.cs:123 
  at Cirrious.MvvmCross.ViewModels.MvxCommand.Execute (System.Object parameter) [0x00000] in <filename unknown>:0 
  at Thrust.Plugins.Test.Touch.FirstView.ShowMyViewModel (Foundation.NSObject sender) [0x0000d] in /Users/gbastos/Documents/Gabriel Docs/Development/ThrustPlugins/Plugins/Thrust.Plugins.Test.Touch/Views/FirstView.cs:25 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:45 
  at Thrust.Plugins.Test.Touch.Application.Main (System.String[] args) [0x00008] in /Users/gbastos/Documents/Gabriel Docs/Development/ThrustPlugins/Plugins/Thrust.Plugins.Test.Touch/Main.cs:16

如果您的 View 确实可以跨应用重用,那么您可以通过在 IMvxViewsContainer 中注册来使其可访问。为此,您可以在 Touch 插件程序集中放置一些代码 - 例如:

    Mvx.CallbackWhenRegistered<IMvxViewsContainer>(() => {
       var container = Mvx.Resolve<IMvxViewsContainer>();
       container.Add<MyViewModel, MyView>();
    });

请参阅 https://github.com/MvvmCross/MvvmCross/blob/3.5/Cirrious/Cirrious.MvvmCross/Views/IMvxViewsContainer.cs

处的接口定义