注意:创建View时,添加的是用户控件
通俗点理解就是,其他页面的内容需要有控件去展现出来给用户看。然后就用到一个ContentControl 控件来展现内容,并且也需要用到 Prism 框架里面的区域 (并且通过定义一个区域名称来定位到展现的位置)。我是这样理解的。首先这不是教程,是我学习的记录,如果错了,就错了。
<ContentControl prism:RegionManager.RegionName=""/>
建议:通过一个扩展类来管理定义的一些属性名称
例如:当前定义的区域名称,通过建立一个扩展类来进行管理
- public static class PrismManager
- {
- public static readonly string MainViewRegionName = "MainViewReion";
- }
xmlns:ext="clr-namespace:MyToDo.Extensions"
- xmlns: 是引入命名空间固定的写法
- ext 是自定义的名称
- = 号后面是扩展类所在的命名空间
<ContentControl prism:RegionManager.RegionName="{x:Static ext:PrismManager.MainViewRegionName}"/>
x:Static 是引用静态类型的属性的固定前缀的写法
- /// <summary>
- /// 依懒注入的方法
- /// summary>
- /// <param name="containerRegistry">param>
- protected override void RegisterTypes(IContainerRegistry containerRegistry)
- {
- containerRegistry.RegisterForNavigation
(); - containerRegistry.RegisterForNavigation
(); - containerRegistry.RegisterForNavigation
(); - containerRegistry.RegisterForNavigation
(); - }
- public class MainViewModel: BindableBase
- {
- public MainViewModel(IRegionManager regionManager)
- {
- NavigateCommand = new DelegateCommand
(Navigate); - this.regionManager = regionManager;
- }
-
- ///
- /// 导航方法
- ///
- /// 菜单
- private void Navigate(MenuBar bar)
- {
- //命名空间为空,不进行导航
- if (bar == null || string.IsNullOrEmpty(bar.NameSpace)) return;
-
- regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(bar.NameSpace);
- }
- ///
- /// 导航命令
- ///
- public DelegateCommand
NavigateCommand { get; private set; } -
- }
通过添加导航日志 IRegionNavigationJournal 来实现,上一步,下一步的导航功能
- ///
- /// 导航方法
- ///
- /// 菜单
- private void Navigate(MenuBar bar)
- {
- //命名空间为空,不进行导航
- if (bar == null || string.IsNullOrEmpty(bar.NameSpace)) return;
-
- regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(bar.NameSpace, back =>
- {
-
- });
- }
- public class MainViewModel: BindableBase
- {
- public MainViewModel(IRegionManager regionManager)
- {
- NavigateCommand = new DelegateCommand
(Navigate); - this.regionManager = regionManager;
- }
-
- ///
- /// 导航方法
- ///
- /// 菜单
- private void Navigate(MenuBar bar)
- {
- //命名空间为空,不进行导航
- if (bar == null || string.IsNullOrEmpty(bar.NameSpace)) return;
-
- regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(bar.NameSpace, back =>
- {
- journal=back.Context.NavigationService.Journal;
- });
- }
-
- ///
- /// 导航命令
- ///
- public DelegateCommand
NavigateCommand { get; private set; } -
- private readonly IRegionManager regionManager;
- ///
- /// 导航日志
- ///
- private IRegionNavigationJournal journal;
-
- }
- public class MainViewModel: BindableBase
- {
- public MainViewModel(IRegionManager regionManager)
- {
- NavigateCommand = new DelegateCommand
(Navigate); - this.regionManager = regionManager;
- GoBackCommand = new DelegateCommand(() =>
- {
- if(journal!=null && journal.CanGoBack) journal.GoBack();
- });
- GoForwardCommand = new DelegateCommand(() =>
- {
- if (journal != null && journal.CanGoForward) journal.GoForward();
- });
- }
-
- ///
- /// 导航方法
- ///
- /// 菜单
- private void Navigate(MenuBar bar)
- {
- //命名空间为空,不进行导航
- if (bar == null || string.IsNullOrEmpty(bar.NameSpace)) return;
-
- regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(bar.NameSpace, back =>
- {
- journal=back.Context.NavigationService.Journal;
- });
- }
-
- ///
- /// 导航命令
- ///
- public DelegateCommand
NavigateCommand { get; private set; } -
- ///
- /// 下一步
- ///
- public DelegateCommand GoBackCommand { get; private set; }
- ///
- /// 上一步
- ///
- public DelegateCommand GoForwardCommand { get; private set; }
-
-
- private readonly IRegionManager regionManager;
- ///
- /// 导航日志
- ///
- private IRegionNavigationJournal journal;
-
- }
引入行为命名空间 xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
- <i:Interaction.Triggers>
- <i:EventTrigger EventName="SelectionChanged">
- <i:InvokeCommandAction Command="{Binding NavigateCommand}" CommandParameter="{Binding ElementName=menuBar,Path=SelectedItem}" />
- i:EventTrigger>
- i:Interaction.Triggers>
- Interaction.Triggers 行为触发器
- EventTrigger 触发的事件
- EventName 触发事件的名称,这个事件命名,一定是ListBox存在的事件名称,不是随便起的名称
- InvokeCommandAction 绑定后台要触发的导航命令
- CommandParameter 绑定当前选中项。例如。这是传过去后台命令的参数
例如,上面是通过选中ListBox Item的子项来触发导航命令
- //菜单选择事件
- menuBar.SelectionChanged += (s, e) =>
- {
- drawerHost.IsLeftDrawerOpen = false;
- };
- menuBar 是ListBox 定义的名称
- drawerHost 是左侧弹框定义的名称
- <Window x:Class="MyToDo.Views.MainView"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:ext="clr-namespace:MyToDo.Extensions"
- xmlns:local="clr-namespace:MyToDo"
- xmlns:prism="http://prismlibrary.com/"
- xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
- prism:ViewModelLocator.AutoWireViewModel="True"
- WindowStyle="None" WindowStartupLocation="CenterScreen" AllowsTransparency="True"
- Style="{StaticResource MaterialDesignWindow}"
- TextElement.Foreground="{DynamicResource MaterialDesignBody}"
- Background="{DynamicResource MaterialDesignPaper}"
- TextElement.FontWeight="Medium"
- TextElement.FontSize="14"
- FontFamily="{materialDesign:MaterialDesignFont}"
- mc:Ignorable="d"
- xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
- Title="MainWindow" Height="768" Width="1280">
- <materialDesign:DialogHost DialogTheme="Inherit"
- Identifier="RootDialog"
- SnackbarMessageQueue="{Binding ElementName=MainSnackbar, Path=MessageQueue}">
-
- <materialDesign:DrawerHost x:Name="drawerHost" IsLeftDrawerOpen="{Binding ElementName=MenuToggleButton, Path=IsChecked}">
-
- <materialDesign:DrawerHost.LeftDrawerContent>
- <DockPanel MinWidth="220" >
-
-
- <StackPanel DockPanel.Dock="Top" Margin="0,20">
- <Image Source="/Images/user.jpg" Width="50" Height="50">
- <Image.Clip>
- <EllipseGeometry Center="25,25" RadiusX="25" RadiusY="25" />
- Image.Clip>
- Image>
- <TextBlock Text="WPF gg" Margin="0,10" HorizontalAlignment="Center" />
- StackPanel>
-
-
- <ListBox x:Name="menuBar" ItemContainerStyle="{StaticResource MyListBoxItemStyle}" ItemsSource="{Binding MenuBars}">
- <i:Interaction.Triggers>
- <i:EventTrigger EventName="SelectionChanged">
- <i:InvokeCommandAction Command="{Binding NavigateCommand}" CommandParameter="{Binding ElementName=menuBar,Path=SelectedItem}" />
- i:EventTrigger>
- i:Interaction.Triggers>
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal" Background="Transparent">
- <materialDesign:PackIcon Kind="{Binding Icon}" Margin="15,0" />
- <TextBlock Text="{Binding Title}" Margin="10,0"/>
- StackPanel>
- DataTemplate>
- ListBox.ItemTemplate>
- ListBox>
-
-
- DockPanel>
- materialDesign:DrawerHost.LeftDrawerContent>
-
- <DockPanel >
-
- <materialDesign:ColorZone Padding="16" x:Name="ColorZone"
- materialDesign:ElevationAssist.Elevation="Dp4"
- DockPanel.Dock="Top"
- Mode="PrimaryMid">
- <DockPanel LastChildFill="False">
-
- <StackPanel Orientation="Horizontal">
- <ToggleButton x:Name="MenuToggleButton"
- AutomationProperties.Name="HamburgerToggleButton"
- IsChecked="False"
- Style="{StaticResource MaterialDesignHamburgerToggleButton}" />
-
- <Button Margin="24,0,0,0"
- materialDesign:RippleAssist.Feedback="{Binding RelativeSource={RelativeSource Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}"
- Command="{Binding GoForwardCommand}"
- Content="{materialDesign:PackIcon Kind=ArrowLeft,
- Size=24}"
- Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"
- Style="{StaticResource MaterialDesignToolButton}"
- ToolTip="Previous Item" />
-
- <Button Margin="16,0,0,0"
- materialDesign:RippleAssist.Feedback="{Binding RelativeSource={RelativeSource Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}"
- Command="{Binding GoBackCommand}"
- Content="{materialDesign:PackIcon Kind=ArrowRight,
- Size=24}"
- Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"
- Style="{StaticResource MaterialDesignToolButton}"
- ToolTip="Next Item" />
- <TextBlock Margin="16,0,0,0"
- HorizontalAlignment="Center"
- VerticalAlignment="Center"
- AutomationProperties.Name="Material Design In XAML Toolkit"
- FontSize="22"
- Text="笔记本" />
- StackPanel>
-
- <StackPanel DockPanel.Dock="Right" Orientation="Horizontal">
- <Image Source="/Images/user.jpg" Width="25" Height="25">
- <Image.Clip>
- <EllipseGeometry Center="12.5,12.5" RadiusX="12.5" RadiusY="12.5" />
- Image.Clip>
- Image>
- <Button x:Name="btnMin" Style="{StaticResource MaterialDesignFlatMidBgButton}">
- <materialDesign:PackIcon Kind="MoveResizeVariant" />
- Button>
- <Button x:Name="btnMax" Style="{StaticResource MaterialDesignFlatMidBgButton}">
- <materialDesign:PackIcon Kind="CardMultipleOutline" />
- Button>
- <Button x:Name="btnClose" Style="{StaticResource MaterialDesignFlatMidBgButton}" Cursor="Hand">
- <materialDesign:PackIcon Kind="WindowClose" />
- Button>
- StackPanel>
- DockPanel>
-
- materialDesign:ColorZone>
-
-
- <ContentControl prism:RegionManager.RegionName="{x:Static ext:PrismManager.MainViewRegionName}"/>
- DockPanel>
- materialDesign:DrawerHost>
- materialDesign:DialogHost>
- Window>
- ///
- /// MainView.xaml 的交互逻辑
- ///
- public partial class MainView : Window
- {
- public MainView()
- {
- InitializeComponent();
- //最小化
- btnMin.Click += (s, e) =>
- {
- this.WindowState = WindowState.Minimized;//窗口设置最小
- };
- //最大化
- btnMax.Click += (s, e) =>
- {
- //判断窗口是否是最小化状态
- if (this.WindowState == WindowState.Maximized)
- {
- this.WindowState = WindowState.Normal; //改成正常状态
- }
- else
- {
- this.WindowState = WindowState.Maximized;//最大化
- }
- };
- //关闭
- btnClose.Click += (s, e) =>
- {
- this.Close();
- };
- //鼠标拖动事件
- ColorZone.MouseMove += (s, e) =>
- {
- //如果鼠标在拖动
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- this.DragMove();//让窗口移动
- }
- };
-
- //导航栏双击事件
- ColorZone.MouseDoubleClick += (s, e) =>
- {
- //双击时,如果是窗口是正常形态,就变成最大化
- if (this.WindowState == WindowState.Normal)
- {
- this.WindowState = WindowState.Maximized;
- }
- else
- {
- this.WindowState = WindowState.Normal;//否则就变成正常的形态
- }
- };
- //菜单选择事件
- menuBar.SelectionChanged += (s, e) =>
- {
- drawerHost.IsLeftDrawerOpen = false;
- };
- }
- }
- public class MainViewModel: BindableBase
- {
- public MainViewModel(IRegionManager regionManager)
- {
- MenuBars=new ObservableCollection
(); - CreateMenuBar();
- NavigateCommand = new DelegateCommand
(Navigate); - this.regionManager = regionManager;
- GoBackCommand = new DelegateCommand(() =>
- {
- if(journal!=null && journal.CanGoBack) journal.GoBack();
- });
- GoForwardCommand = new DelegateCommand(() =>
- {
- if (journal != null && journal.CanGoForward) journal.GoForward();
- });
- }
-
- ///
- /// 导航方法
- ///
- /// 菜单
- private void Navigate(MenuBar bar)
- {
- //命名空间为空,不进行导航
- if (bar == null || string.IsNullOrEmpty(bar.NameSpace)) return;
-
- regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(bar.NameSpace, back =>
- {
- journal=back.Context.NavigationService.Journal;
- });
- }
-
- ///
- /// 导航命令
- ///
- public DelegateCommand
NavigateCommand { get; private set; } -
- ///
- /// 下一步
- ///
- public DelegateCommand GoBackCommand { get; private set; }
- ///
- /// 上一步
- ///
- public DelegateCommand GoForwardCommand { get; private set; }
-
- private ObservableCollection
menuBars; - private readonly IRegionManager regionManager;
- ///
- /// 导航日志
- ///
- private IRegionNavigationJournal journal;
- public ObservableCollection
MenuBars - {
- get { return menuBars; }
- set { menuBars = value; RaisePropertyChanged(); }
- }
- void CreateMenuBar()
- {
- MenuBars.Add(new MenuBar() { Icon="Home",Title="首页",NameSpace="IndexView"});
- MenuBars.Add(new MenuBar() { Icon = "NotebookCheckOutline", Title = "待办事项", NameSpace = "ToDoView" });
- MenuBars.Add(new MenuBar() { Icon = "NotebookPlusOutline", Title = "忘备录", NameSpace = "MemoView" });
- MenuBars.Add(new MenuBar() { Icon = "Cog", Title = "设置", NameSpace = "SettingsView" });
- }
- }
- public partial class App : PrismApplication
- {
- ///
- /// 创建启动页面
- ///
- ///
- protected override Window CreateShell()
- {
- return Container.Resolve
(); - }
- ///
- /// 依懒注入的方法
- ///
- ///
- protected override void RegisterTypes(IContainerRegistry containerRegistry)
- {
- containerRegistry.RegisterForNavigation
(); - containerRegistry.RegisterForNavigation
(); - containerRegistry.RegisterForNavigation
(); - containerRegistry.RegisterForNavigation
(); - }
- }