简体   繁体   中英

Xamarin.iOS, MVVMCross. Can I Access NavigationItem from TabBarController's root ViewController?

I use Xamarin.iOS and MVVMCross. My app's architecture is the following:

              +-----------------------+
              | RootViewModel         |
              | RootViewController    |
              | [MvxRootPresentation] |
              |                       |
              +--------+----+---------+
                       |    |
           +-----------+    +------------+
           |                             |
+----------+----------+      +-----------+---------+
|Tab1ViewModel        |      |Tab2ViewModel        |
|Tab1ViewController   |      |Tab2ViewController   |
|[MvxTabPresentation] |      |[MvxTabPresentation] |
|                     |      |                     |
+---------------------+      +---------------------+

The problem is, that the Navigation Bars are set up in the Tab ViewControllers. I have to display some information on the NavigationItem.Title, which is stored in the RootViewModel. Can I have one, common Navigation Bar, which is initialize in the RootViewController? Or if it's not possible, can I access RootViewModel from a TabViewController?

  1. In RootViewController add the decoration to the class.
[MvxRootPresentation(WrapInNavigationController = true)]
public partial class RootViewController : MvxTabBarViewController<RootViewModel>
  1. In your tab controllers add the decoration:
[MvxTabPresentation(WrapInNavigationController = false, TabName = "Tab1Title", TabIconName = "Tab1Icon")]
public partial class Tab1ViewController : ViewController<Tab1ViewModel>
  1. In your tab controllers also add this in ViewDidLoad to hide the indivdual navigation bars:
public override void ViewDidLoad()
{
    base.ViewDidLoad();
    if (NavigationController != null && NavigationController.NavigationBar != null)
    {
        NavigationController.NavigationBar.Hidden = true;
    }
}

Bonus. You can change the tab title translation in the RootViewController

protected override void SetTitleAndTabBarItem(UIViewController viewController, MvxTabPresentationAttribute attribute)
{
    attribute.TabName = _yourLocalizationService.Translate(attribute.TabName);
    base.SetTitleAndTabBarItem(viewController, attribute);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM