某些视图中未初始化Xamarin iOS FlyoutNavigation

时间:2017-03-27 11:02:28

标签: ios xamarin xamarin.ios mvvmcross navigationbar

我的iOS项目中的FlyoutNavigation存在问题。我在Android / iOS项目中的第一个视图是MenuViewModel,我有init菜单。 我使用MvvmCross(但旧版本v3.5.1)。

在我在MenuView的iOS项目中,我在ViewDidLoad()中有这个:

var navigationController = new NavigationController();
_navigation = navigationController.Navigation;
View.AddSubview(_navigation.View);
this.AddChildViewController(_navigation);

其中NavigationController初始化FlyoutNavigation,如下所示:

public class NavigationController
{
    private FlyoutNavigationController _navigation;
    public FlyoutNavigationController Navigation { get; private set; }
    private UIViewController _mainView;
    //private UIViewController _purchasesView;
    private UIViewController _ordersView;
    private UIViewController _profileView;
    private UIViewController _writeUsView;
    private UIViewController _aboutView;
    private UIViewController _tradeTermsView;
    private UIViewController _contactView;

    public NavigationController()
    {
        _navigation = new FlyoutNavigationController();
        Initialize();
        Navigation = _navigation;
    }

    private void Initialize()
    {
        var textProvider = Mvx.Resolve<IMvxTextProviderBuilder>().TextProvider;
        var profileName = Mvx.Resolve<IMvxTextProviderBuilder>().TextProvider.GetText("XXX", "MenuViewModel", "Profile");

        _navigation.Position = FlyOutNavigationPosition.Left;
        //_navigation.View.Frame = UIScreen.MainScreen.Bounds;
        _navigation.NavigationTableView.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 240);

        var profileMenuItem = new StyledStringElement(profileName)
        {
            BackgroundColor = UIColor.FromRGBA(0, 0, 0, 240),
            TextColor = UIColor.White,
            Font = UIFont.PreferredBody,
            Image = UIImage.FromBundle("Icons/Profile")
        };

        Mvx.Resolve<IMvxMessenger>().Subscribe<UserProfileChangedMessage>(x =>
        {
            profileMenuItem.Caption = $"{x.Profile.FirstName} {x.Profile.LastName}";
        }, MvxReference.Strong);

        // Create the menu:
        _navigation.NavigationRoot = new RootElement("Navigation")
        {
            new Section("")
            {
                new StyledStringElement (textProvider.GetText("XXX", "MenuViewModel", "MainPage")) {
                  BackgroundColor = UIColor.FromRGBA(0,0,0,240),
                  TextColor = UIColor.White,
                  Font = UIFont.PreferredBody,
                  //Font = UIFont.FromName("Lato-Black", 16f),
                  Image = UIImage.FromBundle("Icons/MainPage")
                },
                  new StyledStringElement (textProvider.GetText("XXX", "MenuViewModel", "Orders")) {
                    BackgroundColor = UIColor.FromRGBA(0,0,0,240),
                    TextColor = UIColor.White,
                    Font = UIFont.PreferredBody,
                    Image = UIImage.FromBundle("Icons/MyShopping")
                },
                profileMenuItem,
                new StyledStringElement (textProvider.GetText("XXX", "MenuViewModel", "WriteUs")) {
                    BackgroundColor = UIColor.FromRGBA(0,0,0,240),
                    TextColor = UIColor.White,
                    Font = UIFont.PreferredBody,
                  Image = UIImage.FromBundle("Icons/WriteUs")
                },
                 new StyledStringElement (textProvider.GetText("XXX", "MenuViewModel", "About")) {
                BackgroundColor = UIColor.FromRGBA(0,0,0,240),
                Font = UIFont.PreferredBody,
                TextColor = UIColor.White
                },
                new StyledStringElement(textProvider.GetText("XXX", "MenuViewModel", "TradeTerms")){
                BackgroundColor = UIColor.FromRGBA(0,0,0,240),
                Font = UIFont.PreferredBody,
                TextColor = UIColor.White
                },
                new StyledStringElement(textProvider.GetText("XXX", "MenuViewModel", "Contact")){
                BackgroundColor = UIColor.FromRGBA(0,0,0,240),
                Font = UIFont.PreferredBody,
                TextColor = UIColor.White
                }
            }
        };

        // Create an array of UINavigationControllers that correspond to your
        // menu items:
        _mainView = ViewHelper.GetViewFromViewModel<MainViewModel>();
        _aboutView = ViewHelper.GetViewFromViewModel<AboutViewModel>();
        _contactView = ViewHelper.GetViewFromViewModel<ContactViewModel>();
        _profileView = ViewHelper.GetViewFromViewModel<ProfileViewModel>();
        //_purchasesView = ViewHelper.GetViewFromViewModel<PurchasesViewModel>();
        _ordersView = ViewHelper.GetViewFromViewModel<OrdersViewModel>();
        _tradeTermsView = ViewHelper.GetViewFromViewModel<TradeTermsViewModel>();
        _writeUsView = ViewHelper.GetViewFromViewModel<WriteUsViewModel>();

        _navigation.ViewControllers = new UIViewController[]
        {
            _mainView, /*_purchasesView,*/ _ordersView, _profileView, _writeUsView, _aboutView, _tradeTermsView, _contactView
        };

        var messenger = Mvx.Resolve<IMvxMessenger>();
        var menuState = Mvx.Resolve<IMenuState>();

        messenger.SubscribeOnMainThread<MenuClickMessage>(x =>
        {
            // Show the navigation view
            if (_navigation.IsOpen)
            {
                _navigation.HideMenu();
                menuState.Switch();
            }
            else
            {
                _navigation.ShowMenu();
                menuState.Switch();
            }
        }, MvxReference.Strong);
    }

}

导航首先初始化ViewModel和View - MainView / MainViewModel ...

我的问题是,当我在导航中使用菜单项时,它的工作很好...但是当我在OrderView按钮中使用ICommand时ShowViewModel(),所以当我点击这个按钮并且我的视图改变时TradeTermsView在这里我的FlyoutNavigation不起作用...导航没有出现像它没有初始化在这里我不知道...导航按钮在这里是正常但当我点击它导航不显示...但当我点击导航中的MainView TradeTerms和TradeTerms Navigation中的工作。它似乎导航只在我从导航点击的视图中工作,而不是从ShowViewModel&lt;&gt;()命令。

FlyoutNavigation的另一个问题是当我点击导航按钮显示时,我点击了CollectionView中的项目并且我移动到此视图,因此在此视图的底部和顶部有一些空闲空间...似乎导航在这里给了一些框架...但是当我再次启动应用程序并点击项目时没有点击导航按钮,所以视图看起来很正常。

此处示例: enter image description here

0 个答案:

没有答案