Wing Chan
Wing Chan

Reputation: 311

MvvmCross: How to use FlyoutNavigation component with MvvmCross

Is there any example on how to use MvvmCross with FlyoutNavigation component from Xamarin.

I am getting error when I tried to set the ViewControllers using menu views from MainNavigationViewModel. This is the error I got "System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation."

Here is my sample code:

public class MainNavigationViewModel : MvxViewModel
{
    public MainNavigationViewModel()
    {
        MenuOne = new MenuOneViewModel();
    }

    private MenuOne _MenuOne;

    public MenuOneViewModel MenuOne
    {
        get { return _MenuOne; }
        set
        {
            _MenuOne = value;
            RaisePropertyChanged(() => MenuOne);
        }
    }
}


[Register("MainNavigationView")]
public sealed class MainNavigationView : MvxViewController
{
    protected MainNavigationViewModel MainNavViewModel
    { get { return base.ViewModel as MainNavigationViewModel; } }

    public MainNavigationView()
    {
        ViewDidLoad();
    }

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
        NavigationController.NavigationBarHidden = true;
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        if (ViewModel == null)
            return;

        var flyoutNavigationController = new FlyoutNavigationController();

        var menuViewRoot = new RootElement(null);

        var menuItems = new List<StringElement>()
            {
                new StringElement("Menu One")
            };

        var menuSection = new Section("Main Menu") { menuItems };
        menuViewRoot.Add(menuSection);

        flyoutNavigationController.NavigationRoot = menuViewRoot;

        var viewControllers = new UIViewController[1];
        viewControllers[0] = CreateMenuItemController(MainNavViewModel.MainOne);
        flyoutNavigationController.ViewControllers = viewControllers;

        flyoutNavigationController.View.Frame = UIScreen.MainScreen.Bounds;
        flyoutNavigationController.ToggleMenu();
        Add(flyoutNavigationController.View);
    }

    private UIViewController CreateMenuItemController(IMvxViewModel viewModel)
    {
        var controller = new UINavigationController();
        var screen = this.CreateViewControllerFor(viewModel) as UIViewController;
        controller.PushViewController(screen, false);
        return controller;
    }
}

Upvotes: 3

Views: 1024

Answers (1)

Stuart
Stuart

Reputation: 66882

Try this example https://github.com/fcaico/MvxSlidingPanels.Touch which is based on a fork of that component

Upvotes: 4

Related Questions