MvvmCross在自定义IosPresenter

时间:2018-04-17 02:05:40

标签: ios xamarin mvvmcross

我无法从主视图导航到MvxTabBarViewController,我认为我的IosViewPresenter缺少一些东西,它使用带有NavigationBar的单根视图和ContentView中的页面。

public class ContainerPresenter : MvxIosViewPresenter
{
    public static MvxNavigationController NavigationController = null;
    public static UIView MenuView = null;

    private UIView _containerView;
    private MvxViewController _rootController;

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

    public override void Show(MvxViewModelRequest request)
    {
        if (NavigationController == null)
        {
            // First controller called from main hamburger menu.  Must have decorator: WrapInNavigationController = true
            base.Show(request);
        }

        else
        {
            // Subsequent controllers called from main menu or other views 
            MvxViewController c = _rootController.CreateViewControllerFor(request) as MvxViewController;
            string val = "";
            if (request.PresentationValues != null)
            {
                request.PresentationValues.TryGetValue("NavigationMode", out val);
            }
            if (val.Equals("Push", StringComparison.Ordinal))
            {
                // Push new controller onto navigation stack
                PushViewControllerIntoStack(NavigationController, c, true);
            }
            else
            {

                // Replace stack in existing navigation controller with new controller
                UIViewController[] controllers = { c };

                // SetViewControllers unloads all the navigation bar buttons
                // Don't animate to avoid visible refresh when switching root menu items
                NavigationController.SetViewControllers(controllers, false);
                NavigationController.NavigationBar.TopItem.SetRightBarButtonItems(AlertItem.GetAlertItems(), false);
                NavigationController.NavigationBar.TopItem.SetLeftBarButtonItem(HamburgerItem.Button, false);

            }
        }
    }

    protected override MvxNavigationController CreateNavigationController(UIViewController viewController)
    {
        // One NavigationController instance for all views
        NavigationController = base.CreateNavigationController(viewController);
        NavigationController.NavigationBarHidden = false;
        //NavigationController.NavigationBar.TintColor = UIColor.FromRGB(15, 79, 140);
        NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB(33, 33, 33);
        NavigationController.NavigationBar.Translucent = false;

        NavigationController.NavigationBar.TopItem.SetLeftBarButtonItem(HamburgerItem.Button, false);

        return NavigationController;
    }



    protected override void SetWindowRootViewController(UIViewController controller, MvxRootPresentationAttribute attribute = null)
    {
        if (_window.RootViewController == null)
        {
            // Insert MainView as root controller, subsequent root menu_item controllers will use MainView.ContainerView as root
            base.SetWindowRootViewController(controller, attribute);
            _rootController = controller as MvxViewController;

            // Hack to get around: 'MainView.ContainerView' is inacessible due to its protection level
            // _containerView = (_window.RootViewController as MainView).ContainerView;
            _containerView = _window.RootViewController.View.ViewWithTag(1);
            MenuView = _window.RootViewController.View.ViewWithTag(2);
        }
        else
        {
            // Move root menu_item controller to ContainerView
            controller.View.Frame = _containerView.Bounds;
            controller.WillMoveToParentViewController(_window.RootViewController);
            _containerView.AddSubview(controller.View);
            _window.RootViewController.AddChildViewController(controller);
            controller.DidMoveToParentViewController(_window.RootViewController);
        }
    }
}

我正在关注https://github.com/MvvmCross/MvvmCross/tree/develop/Projects/Playground游乐场中提供的标签示例,我调用的基本标签视图看起来像这样

[MvxRootPresentation(WrapInNavigationController = true)]
public partial class ItemDetailBaseView : MvxTabBarViewController<ItemDetailBaseViewModel>
{


    private bool _isPresentedFirstTime = true;

    public ItemDetailBaseView(IntPtr handle) : base(handle)
    {
        Console.WriteLine("Loaded"); //<----Never called
    }


    public override void ViewDidLoad()
    {
        base.ViewDidLoad(); //<----Never called

        // Perform any additional setup after loading the view, typically from a nib.
    }


    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated); //<----Never called

        if (ViewModel != null && _isPresentedFirstTime)
        {
            _isPresentedFirstTime = false;
            ViewModel.ShowInitialViewModelsCommand.ExecuteAsync(null);
        }
    }

虽然我的标签视图看起来像这样

public class ItemDetailTabViewModel : MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;

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

    public override void ViewAppeared()
    {
        base.ViewAppeared();
    }

    public override async Task Initialize()
    {
        await Task.Delay(3000);
    }
}

到目前为止没什么好看的,我用

导航到它
_navigationService.Navigate<ItemDetailBaseViewModel>(new MvxBundle(new Dictionary<string, string> { { "TabbedPage", "Push" } }));

我得到“导航请求”但没有其他任何事情发生,没有视图更改,没有闪烁。正如我上面评论的那样,ItemDetailBaseView中没有任何内容被调用。根据示例,应在导航请求后调用ViewWillAppear,然后命令应填充,但我没有得到任何东西。我甚至尝试在Presenter中捕获请求,并使控制器成为根视图,但没有成功,我没有想法。

谢谢

0 个答案:

没有答案