注意:创建View时,添加的是用户控件
通俗点理解就是,其他页面的内容需要有控件去展现出来给用户看。然后就用到一个ContentControl 控件来展现内容,并且也需要用到 Prism 框架里面的区域 (并且通过定义一个区域名称来定位到展现的位置)。我是这样理解的。首先这不是教程,是我学习的记录,如果错了,就错了。
建议:通过一个扩展类来管理定义的一些属性名称
例如:当前定义的区域名称,通过建立一个扩展类来进行管理
public static class PrismManager
{
public static readonly string MainViewRegionName = "MainViewReion";
}
xmlns:ext="clr-namespace:MyToDo.Extensions"
- xmlns: 是引入命名空间固定的写法
- ext 是自定义的名称
- = 号后面是扩展类所在的命名空间
x:Static 是引用静态类型的属性的固定前缀的写法
///
/// 依懒注入的方法
///
///
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"
- Interaction.Triggers 行为触发器
- EventTrigger 触发的事件
- EventName 触发事件的名称,这个事件命名,一定是ListBox存在的事件名称,不是随便起的名称
- InvokeCommandAction 绑定后台要触发的导航命令
- CommandParameter 绑定当前选中项。例如。这是传过去后台命令的参数
例如,上面是通过选中ListBox Item的子项来触发导航命令
//菜单选择事件
menuBar.SelectionChanged += (s, e) =>
{
drawerHost.IsLeftDrawerOpen = false;
};
- menuBar 是ListBox 定义的名称
- drawerHost 是左侧弹框定义的名称
///
/// 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();
}
}