Xamarin.ios+MvvmCross+ Flyout Menu

颜杰
2023-12-01

add  Flyout Navigation



MenuView


using System;
using System.Linq;
using Foundation;
using UIKit;
using Cirrious.MvvmCross.Touch.Views;
using FlyoutNavigation;
using MonoTouch.Dialog;
using System.Collections.Generic;
using AppSonicboxx.PCL;
using Cirrious.MvvmCross.ViewModels;
using Cirrious.CrossCore;
using Cirrious.MvvmCross.Plugins.Messenger;

namespace AppSonicboxx.iPhone
{
    public partial class MenuView : MvxViewController
    {
        public MenuView () : base ("MenuView", null)
        {
        }

        public override void DidReceiveMemoryWarning ()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning ();
            
            // Release any cached data, images, etc that aren't in use.
        }
        public MenuViewModel MenuViewModel
        {
            get
            {
                return(MenuViewModel)ViewModel;
            }
        }

        private FlyoutNavigationController navigation;

        public MvxSubscriptionToken navigationMenuToggleToken;

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            NavigationController.NavigationBarHidden = true;
            this.View = new UIView { BackgroundColor = UIColor.White };

            navigation = new FlyoutNavigationController();
            View.AddSubview(navigation.View);
            this.AddChildViewController(navigation);
            this.NavigationItem.HidesBackButton = true;


            //names of the views shown in the flyout
            var flyoutMenuElements = new Section();
            var flyoutViewControllers = new List<UIViewController>();

            var view=new UIView();
            view.Frame = new CoreGraphics.CGRect (10, 0, 300, 100);
            view.BackgroundColor = UIColor.Clear;

            var menulable = new UILabel ();
            menulable.Text="Menu";
            menulable.Frame = new CoreGraphics.CGRect (0, 0, 50, 25);
            menulable.TextColor = UIColor.White;
            view.Add (menulable);

            var lable=new UILabel();
            lable.Frame = new CoreGraphics.CGRect (0,28,200,25f);
            lable.Text = "Welcome "+App.UserName;
            lable.TextColor = UIColor.White;
            view.Add (lable);

            var button = new UIButton ();
            button.SetTitle ("log out", UIControlState.Normal);
            button.SetTitleColor (UIColor.Black, UIControlState.Normal); 
            button.BackgroundColor = UIColor.White;
            button.Frame = new CoreGraphics.CGRect (0, 53, 100, 30);
            button.Layer.CornerRadius = 3;
            button.TouchUpInside += (sender, e) => 
            {
                IPlatformService service=Mvx.Resolve<IPlatformService>();
                service.DeleteUser();
                MenuViewModel.LogOutCheck();
            };
            view.Add (button);
            flyoutMenuElements.Add (view);
            //views that will be shown when a menu item is selected

            var menuViewModel = ViewModel as MenuViewModel;
            if (menuViewModel != null)
            {
                var request = new MvxViewModelRequest
                {
                    ViewModelType = menuViewModel.MenuItems.FirstOrDefault().ViewModelType
                };

                flyoutViewControllers.Add(CreateMenuItemController(request));
                //create the ViewModels
                foreach (var viewModel in menuViewModel.MenuItems)
                {
                    var viewModelRequest = new MvxViewModelRequest
                    {
                        ViewModelType = viewModel.ViewModelType
                    };

                    flyoutViewControllers.Add(CreateMenuItemController(viewModelRequest));
                    flyoutMenuElements.Add(new StringElement(viewModel.Title));
                }
                navigation.ViewControllers = flyoutViewControllers.ToArray();
                var rootElement = new RootElement("")
                {
                    flyoutMenuElements
                };
                navigation.NavigationRoot = rootElement;
                navigation.NavigationTableView.BackgroundColor = UIColor.Black;
                foreach (var cell in navigation.NavigationTableView.VisibleCells) 
                {
                    cell.BackgroundColor = UIColor.Black;
                    cell.TextLabel.TextColor = UIColor.White;
                    cell.SelectionStyle = UITableViewCellSelectionStyle.None;
                }
            }

            var messenger = Mvx.Resolve<IMvxMessenger>();
            navigationMenuToggleToken = messenger.SubscribeOnMainThread<ToggleFlyoutMenuMessage>(message => navigation.ToggleMenu());
            //navigationBarHiddenToken = messenger.SubscribeOnMainThread<NavigationBarHiddenMessage>(message => NavigationController.NavigationBarHidden = message.NavigationBarHidden);
        }

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

        public override void ViewWillAppear (bool animated)
        {
            base.ViewWillAppear (animated);
            navigation.View.Frame = UIScreen.MainScreen.Bounds;
            navigation.View.Bounds = UIScreen.MainScreen.Bounds;
            this.NavigationController.NavigationBarHidden = true;
        }

    }
}


MenuViewModel

using System;
using Cirrious.MvvmCross.ViewModels;
using System.Collections.Generic;

namespace AppSonicboxx.PCL
{
    public class MenuViewModel:MvxViewModel
    {
        public MenuViewModel ()
        {

            MenuItems = new List<MenuItemModel> ();

            MenuItems.Add (new MenuItemModel(){
                Title="DashBoard",
                ViewModelType=typeof(DashboardViewModel)
            });
            MenuItems.Add (new MenuItemModel(){
                Title="Search",
                ViewModelType=typeof(SearchTouranmentViewModel)
            });
            MenuItems.Add (new MenuItemModel(){
                Title="My Profile",
                ViewModelType=typeof(MyProfileViewModel)
            });
            MenuItems.Add (new MenuItemModel(){
                Title="Join Tournament",
                ViewModelType=typeof(JoniedTournamentViewModel)
            });
            MenuItems.Add (new MenuItemModel(){
                Title="Friend List",
                ViewModelType=typeof(FriendListViewModel)
            });
        }

     MenuItemModel

        public List<MenuItemModel> MenuItems{ get; set; }
        public void LogOutCheck()
        {
            ShowViewModel<LoginViewModel> ();
        }
    }
}

add  MenuItemModel

using System;

namespace AppSonicboxx.PCL
{
    public class MenuItemModel
    {
        public string Title{ get; set; }
        public Type ViewModelType{ get;set;}
    }
}

add    ToggleFlyoutMenuMessageModel

using System;
using Cirrious.MvvmCross.Plugins.Messenger;

namespace AppSonicboxx.PCL
{
    public class ToggleFlyoutMenuMessage:MvxMessage
    {
        public ToggleFlyoutMenuMessage (object sender):base(sender)
        {
        }
    }
}

 类似资料: