如何使用MvvmCross 6在Xamarin.iOS中的代码中正确创建根视图控制器?

时间:2018-05-14 23:06:22

标签: xamarin xamarin.ios mvvmcross

我试图在代码中编写Xamarin.iOS用户界面,然后我跟随this MS tutorial。不幸的是,我无法设法将MainViewController[Register(nameof(AppDelegate))] public class AppDelegate : MvxApplicationDelegate<Setup, App> { public override void FinishedLaunching(UIApplication application) { base.FinishedLaunching(application); } public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { Window = new UIWindow(UIScreen.MainScreen.Bounds); var controller = new MainViewController(); Window.RootViewController = controller; Window.MakeKeyAndVisible(); Debug.WriteLine("Set root view to MainViewController"); return true; } } )设置为根控制器。使用NullReferenceException实例化控制器结果。

在MvvmCross中将控制器设置为根控制器的正确方法是什么?

我的AppDelegate如下:

[MvxRootPresentation]
public partial class MainViewController : MvxTabBarViewController<MainViewModel>
{
    private UILabel label;
    //public MainViewController(IntPtr handle) : base(handle)
    //{
    //}

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        Debug.WriteLine("Main View Controller loaded");
        AddLabel();
    }

    private void AddLabel()
    {
        label = new UILabel
        {
            Text = "testing",
            TranslatesAutoresizingMaskIntoConstraints = false
        };
        var lblConstraints = new[]
        {
            label.LeadingAnchor.ConstraintEqualTo(anchor:this.View.SafeAreaLayoutGuide.LeadingAnchor, constant:20.0f),
            label.WidthAnchor.ConstraintEqualTo(label.IntrinsicContentSize.Width),
            label.TopAnchor.ConstraintEqualTo(anchor:this.View.SafeAreaLayoutGuide.TopAnchor, constant:20.0f),
            label.HeightAnchor.ConstraintEqualTo(label.IntrinsicContentSize.Height)
            };
        View.AddSubview(label);
    }
}

视图控制器为root:

  at MvvmCross.Mvx.Resolve[TService] () [0x00006] in <8a077b300d9c484ab0471c2d21c3cb26>:0 
  at MvvmCross.Platforms.Ios.Views.MvxBindingViewControllerAdapter..ctor (MvvmCross.Platforms.Ios.Views.Base.IMvxEventSourceViewController eventSource) [0x00034] in <8a077b300d9c484ab0471c2d21c3cb26>:0 
  at MvvmCross.Platforms.Ios.Views.MvxViewControllerAdaptingExtensions.AdaptForBinding (MvvmCross.Platforms.Ios.Views.Base.IMvxEventSourceViewController view) [0x00007] in <8a077b300d9c484ab0471c2d21c3cb26>:0 
  at MvvmCross.Platforms.Ios.Views.MvxBaseTabBarViewController..ctor () [0x00006] in <8a077b300d9c484ab0471c2d21c3cb26>:0 
  at MvvmCross.Platforms.Ios.Views.MvxTabBarViewController..ctor () [0x00000] in <8a077b300d9c484ab0471c2d21c3cb26>:0 
  at MvvmCross.Platforms.Ios.Views.MvxTabBarViewController`1[TViewModel]..ctor () [0x00000] in <8a077b300d9c484ab0471c2d21c3cb26>:0 
  at PushNotifTest.iOS.Views.Main.MainViewController..ctor () <0x13d7f4970 + 0x0004a> in <1d11ff58113e46f6a5a9245eccb8c13f>:0 
  at PushNotifTest.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication application, Foundation.NSDictionary launchOptions) [0x00017] in /Users/dominik/Projekty/PushNotifTest/src/PushNotifTest.iOS/AppDelegate.cs:28 
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.11.0.280/src/Xamarin.iOS/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.11.0.280/src/Xamarin.iOS/UIKit/UIApplication.cs:63 
  at PushNotifTest.iOS.Application.Main (System.String[] args) [0x00001] in /Users/dominik/Projekty/PushNotifTest/src/PushNotifTest.iOS/Main.cs:9 

异常堆栈跟踪:

{{1}}

1 个答案:

答案 0 :(得分:2)

你应该让MvvmCross为你做这件事。通常,您应指定应在IMvxAppStart派生类中首先显示哪个ViewModel。

public class AppStart : IMvxAppStart
{
    private readonly IMvxNavigationService _navigationService;

    public AppStart(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;
    }

    public void Start(object hint = null)
    {
        try
        {
            _navigationService.Navigate<MainViewModel>().GetAwaiter().GetResult();
        }
        catch (System.Exception e)
        {
        }
    }
}

MainViewModel与ViewController关联的ViewModel是MvxTabBarViewController

然后在AppDelegate举行常规仪式,MvvmCross希望你:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    Window = new UIWindow(UIScreen.MainScreen.Bounds);

    var setup = new Setup(this, Window);
    setup.Initialize();

    var startup = Mvx.Resolve<IMvxAppStart>();
    startup.Start();

    Window.MakeKeyAndVisible();

    return true;
}

因此Setup会旋转IoC容器和其他相关的MvvmCross服务。然后你的IMvxAppStart导航到第一个ViewController。

根据您对MvxTabBarViewController的归属方式,它将包含在NavigationViewController中。您可以使用MvxRootPresentationAttribute控制它。

[MvxRootPresentation(WrapInNavigationController = true)]
public partial class MainViewController : MvxTabBarViewController<MainViewModel>

编辑:

MvvmCross 6.0没有举行常规仪式,而是让这部分更容易。因此,删除AppDelegate的主体并改为创建它:

[Register("AppDelegate")]
public partial class AppDelegate : MvxApplicationDelegate<MvxIosSetup<App>, App>
{
}

在你的核心中你有一个App类来实现MvxApplication。在RegisterAppStart<TViewModel>()覆盖中调用Initialize()

public class App : MvxApplication
{
    public override void Initialize()
    {
        RegisterAppStart<RootViewModel>();
    }

然后不要创建自己的AppStart课程。您可以在Startup()类的App中执行UI绑定启动。