WPF实战学习笔记04-菜单导航

菜单导航

添加文件与文件夹

  • 添加文件夹

​ ./Extensions

  • 添加文件[类型:用户控件]

    ./Views/IndexView.xaml

    ./Views/MemoView.xaml

    ./Views/TodoView.xaml

    ./Views/SettingsView.xaml

    ./ViewModels/IndexViewModel.cs

    ./ViewModels/IndexViewModel.cs

    ./ViewModels/IndexViewModel.cs

    ./ViewModels/IndexViewModel.cs

    ./Extensions/PrismManager.cs

建立View与ViewModel关联

  • App.xaml.cs

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterForNavigation();
        containerRegistry.RegisterForNavigation();
        containerRegistry.RegisterForNavigation();
        containerRegistry.RegisterForNavigation();
    }
    

添加区域

定义区域名字

  • PrismManager.cs

    namespace Mytodo.Extensions
    {
        public static class PrismManager
        {
            public static readonly string MainViewRegionName = "MainViewRegion"; //定义Nanme变量 
        }
    }
    

注册区域,绑定名字

  • MainView.xaml

    • 定义命名空间

      xmlns:ext="clr-namespace:Mytodo.Extensions"
      
    • 注册控件区域

      
      
      	
      	 
      
      

添加导航

添加导航命令与变量

  • MainView.xaml.cs
private readonly IRegionManager regionManager;  //导航变量
public DelegateCommand NavigateCmd { get; private set; } //导航命令

初始化导航命令

  • MainView.xaml.cs

    public MainViewModel(IRegionManager regm)
    {
        MenuBars=new ObservableCollection();
        CreatMenuBar();
    
        //
        this.regionManager = regm;
        NavigateCmd = new DelegateCommand(Navigate);
    }
    

    注意:初始化应在构造函数中

绑定导航命令

添加行为命名空间
  • main.xaml
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
添加行为,绑定导航命令
  • MainView.xmal

    
        
    

​ 完整代码为:


    
        
            
        
    
    
        
            
                
                
            
        
    

添加导航历史功能

添加命令定义

  • MainviewModel.cs
public DelegateCommand GoBackCmd { get; private set; }
public DelegateCommand GoFwrdCmd { get; private set; }

添加导航历史变量

  • MainviewModel.cs
private  IRegionNavigationJournal journal;		//导航历史

初始化命令

  • MainviewModel.cs的构造函数中添加
    //实例化命令
    GoBackCmd = new DelegateCommand(() =>
    {
        if (journal != null && journal.CanGoBack)
            journal.GoBack();
    });
    GoFwrdCmd = new DelegateCommand(() =>
    {
        if (journal != null && journal.CanGoForward)
            journal.GoForward();
    });

绑定命令

  • MainView.xaml

添加自动关掉侧栏代码

  • MainView.xaml.cs

    menuBar.SelectionChanged += (s, e) =>
    {
        drawerHost.IsLeftDrawerOpen = false;
    };
    

你可能感兴趣的:(WPF实战学习笔记,wpf,学习,笔记)