首页 文章

MvvmCross选项卡控制器包装到UINavigation控制器?

提问于
浏览
1

我正在使用Xamarin.iOS和MvvmCross 5.x.我的根视图是Tabs Controller,我希望任何显示子项的请求都会强制在标签之外(不在内部)进行堆栈导航 .

所以我定义了我的根视图如下:

[MvxFromStoryboard]
[MvxRootPresentation(WrapInNavigationController = true)]
public partial class HomeView : MvxTabBarViewController<HomeViewModel>

不幸的是,根导航控制器不是UINavigationController(正如我所期望的那样基于属性),而是HomeView本质上是标签视图而我无法实现我的目标 .

然后我重写ios presenter以手动创建根UINavigationController:

public class MySuperCoolIosViewPresenter : MvxIosViewPresenter
{
    private UINavigationController _rootViewController; 

    public MySuperCoolIosViewPresenter(IMvxApplicationDelegate applicationDelegate, UIWindow window)
        : base(applicationDelegate, window)
    {
    }

    protected override void SetWindowRootViewController(UIViewController controller)
    {
        _rootViewController = new UINavigationController(controller);
        base.SetWindowRootViewController(_rootViewController);
    }

    protected override void ShowChildViewController(UIViewController viewController, MvvmCross.iOS.Views.Presenters.Attributes.MvxChildPresentationAttribute attribute, MvxViewModelRequest request)
    {
        _rootViewController.ShowViewController(viewController, _rootViewController);
    }
}

但是在尝试使用以下错误设置控制器 base.SetWindowRootViewController(_rootViewController) 时,此代码在MvvmCross深处某处失败:

System.NullReferenceException: Object reference not set to an instance of an object\n  
at MvvmCross.iOS.Views.Presenters.MvxIosViewPresenter.CloseTabBarViewController () [0x00036] in <861dee92d7924acc93d876339b4b95f9>:0  
at MvvmCross.iOS.Views.MvxTabBarViewController.ViewWillDisappear (System.Boolean animated) [0x0001f] in <861dee92d7924acc93d876339b4b95f9>:0 
at (wrapper managed-to-native) ObjCRuntime.Messaging:void_objc_msgSend_IntPtr (intptr,intptr,intptr)  
at UIKit.UIWindow.set_RootViewController (UIKit.UIViewController value)

如何在不创建额外的ViewModel和View for Root视图的情况下实现我的目标(我不喜欢这种方法,因为在这种情况下Android会吓坏)

2 回答

  • 3

    我创建了这个样本,其中包含您需要的内容:https://github.com/rrispoli/SampleTabs

  • 0

    当我在寻找解决方案时,来自MvvmCross的人更新了框架以准确解决版本 5.1.1 的问题(我使用的是 5.1.0 ) . Ios presenter逻辑已经更新了 WrapInNavigationController 正确(在标签被忽略之前) . 不幸的是 ShowChildViewControllerInvalidCastException 之后失败但是我能够通过优先选择我的根导航控制器而不是Tabs Controller来解决这个问题

    protected override void ShowChildViewController(UIViewController viewController, MvxChildPresentationAttribute attribute, MvxViewModelRequest request)
        {
            if (viewController is IMvxSplitViewController)
                throw new MvxException("A SplitViewController cannot be presented as a child. Consider using Root instead");
    
            if (ModalViewControllers.Any())
            {
                if (ModalViewControllers.LastOrDefault() is UINavigationController modalNavController)
                {
                    PushViewControllerIntoStack(modalNavController, viewController, attribute.Animated);
                    return;
                }
    
                throw new MvxException($"Trying to show View type: {viewController.GetType().Name} as child, but there is currently a plain modal view presented!");
            }
    
            // this logic goes first for the root controller 
            if (MasterNavigationController != null)
            {
                PushViewControllerIntoStack(MasterNavigationController, viewController, attribute.Animated);
                return;
            }
    
            // then try to show child for the tabs
            if (TabBarViewController != null && TabBarViewController.ShowChildView(viewController))
            {
                return;
            }
    
            base.ShowChildViewController(viewController, attribute, request);
        }
    

    我希望它能节省一些人的时间 .

相关问题