Prism.WPF -- Prism框架使用(下)
本文参考Prism官方示例
命令使用
Prism提供了两种命令:DelegateCommand和CompositeCommand。
DelegateCommand
DelegateCommand封装了两个委托:Execute和CanExecute,使用如下:
// view
<Button Command="{Binding ExecuteDelegateCommand}" Content="DelegateCommand"/>
// viewmodel
public DelegateCommand ExecuteDelegateCommand { get; private set; }
public MainWindowViewModel()
{
ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute);
}
private void Execute()
{
UpdateText = $"Updated: {DateTime.Now}";
}
private bool CanExecute()
{
return IsEnabled;
}
CompositeCommand
CompositeCommand为复合命令,由多个子命令构成。当调用CompositeCommand时,将依次调用每个子命令。默认情况下,当所有子命令CanExecute均返回true时才会执行CompositeCommand。使用方法如下:
// Project.Core中定义接口及实现
public interface IApplicationCommands
{
CompositeCommand SaveCommand { get; }
}
public class ApplicationCommands : IApplicationCommands
{
private CompositeCommand _saveCommand = new CompositeCommand();
public CompositeCommand SaveCommand
{
get { return _saveCommand; }
}
}
// App.xaml.cs中注册单例对象
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<
IApplicationCommands, ApplicationCommands>();
}
// viewmodel中添加子命令
public TabViewModel(IApplicationCommands applicationCommands)
{
_applicationCommands = applicationCommands;
UpdateCommand = new DelegateCommand(Update).ObservesCanExecute(
() => CanUpdate);
_applicationCommands.SaveCommand.RegisterCommand(UpdateCommand);
}
// view中执行命令(需在对应的viewmodel的构造函数中传入IApplicationCommands实例)
<Button Content="Save" Command="{Binding ApplicationCommands.SaveCommand}"/>
EventAggregator
EventAggregator是一种事件机制,解决了松耦合模块间的通信问题。使用方法如下:
// Project.core中声明事件类型
public class MessageSentEvent : PubSubEvent<string>
{
}
// viewmodel中发布事件
IEventAggregator _ea;
public MessageViewModel(IEventAggregator ea)
{
_ea = ea;
// 发布事件的命令
SendMessageCommand = new DelegateCommand(SendMessage);
}
private void SendMessage()
{
_ea.GetEvent<MessageSentEvent>().Publish(Message);
}
// viewmodel中订阅事件
IEventAggregator _ea;
public MessageListViewModel(IEventAggregator ea)
{
_ea = ea;
_ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived);
// 如下方式可以过滤事件,可通过第二个参数指定处理线程
// _ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived,
ThreadOption.PublisherThread, false,
(filter) => filter.Contains("Brian"));
}
private void MessageReceived(string message)
{
// hava a message
}
RegionNavigation
区别于View Discovery和View Injection,RegionNavigation可通过region名称与要导航的视图名称实现更通用的视图导航功能,使用如下:
// 模块类中注册导航视图
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>();
}
// xaml导航命令
<Button Command="{Binding NavigateCommand}" CommandParameter="ViewA" >Navigate to View A</Button>
// viewmodel实现导航
public DelegateCommand<string> NavigateCommand { get; private set; }
public MainWindowViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
NavigateCommand = new DelegateCommand<string>(Navigate);
}
private void Navigate(string navigatePath)
{
if (navigatePath != null)
_regionManager.RequestNavigate("ContentRegion",
navigatePath, NavigationCompleted);
}
// 可指定导航完成回调
private void NavigationCompleted(NavigationResult result)
{
// ...
}
INavigationAware接口
INavigationAware接口包含三个方法:OnNavigatedFrom、OnNavigatedTo、IsNavigationTarge。当ViewAViewModel及ViewBViewModel均实现了INavigationAware接口,ViewA导航到ViewB时,先调用ViewA的OnNavigatedFrom方法,然后调用ViewB的IsNavigationTarge,当其返回true时,调用OnNavigatedTo方法,若IsNavigationTarge返回false,创建新ViewB。示例如下:
public class ViewAViewModel : BindableBase, INavigationAware
{
public void OnNavigatedTo(NavigationContext navigationContext)
{
// ...
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
// ...
}
}
IConfirmNavigationRequest接口
IConfirmNavigationRequest接口继承了INavigationAware接口,并添加了ConfirmNavigationRequest方法。若ViewAViewModel实现了IConfirmNavigationRequest接口,当ViewA导航到ViewB时,先调用ConfirmNavigationRequest方法,若continuationCallback()参数为true,将继续执行导航,执行OnNavigatedFrom方法;若continuationCallback()参数为false,停止导航。示例如下:
public class ViewAViewModel : BindableBase, IConfirmNavigationRequest
{
public void ConfirmNavigationRequest(NavigationContext navigationContext,
Action<bool> continuationCallback)
{
bool result = true;
if (MessageBox.Show("Do you to navigate?", "Navigate?",
MessageBoxButton.YesNo) == MessageBoxResult.No)
result = false;
continuationCallback(result);
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
}
}
IRegionMemberLifetime接口
IRegionMemberLifetime接口只包含一个KeepAlive只读属性。其默认值为true,若其为false,则当该region导航到其它视图时,实现了该接口的当前视图将从IRegion.Views集合中移除并回收。若为true,即使导航到其它视图,该视图依然存在于IRegion.Views集合。示例如下:
public class ViewAViewModel : BindableBase, IRegionMemberLifetime
{
public bool KeepAlive
{
get
{
return false;
}
}
}
参数传递
可使用NavigationParameters实现导航时的参数传递,使用方法如下:
// 导航命令
private void PersonSelected(Person person)
{
var parameters = new NavigationParameters();
parameters.Add("person", person);
if (person != null)
_regionManager.RequestNavigate("PersonDetailsRegion",
"PersonDetail", parameters);
}
// 参数处理
public void OnNavigatedTo(NavigationContext navigationContext)
{
var person = navigationContext.Parameters["person"] as Person;
// ...
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
var person = navigationContext.Parameters["person"] as Person;
// ...
}
Navigation Journal
Navigation Journal可以记录导航的过程,其通过IRegionNavigationJournal接口实现。通过Navigation Journal,可以实现向前/向后导航。示例如下:
// GoForward
public class PersonListViewModel : BindableBase, INavigationAware
{
IRegionNavigationJournal _journal;
public DelegateCommand GoForwardCommand { get; set; }
public PersonListViewModel(IRegionManager regionManager)
{
...
GoForwardCommand = new DelegateCommand(GoForward, CanGoForward);
}
// IRegionNavigationJournal.GoBack到行至此
public void OnNavigatedTo(NavigationContext navigationContext)
{
_journal = navigationContext.NavigationService.Journal;
GoForwardCommand.RaiseCanExecuteChanged();
}
private void GoForward()
{
_journal.GoForward();
}
private bool CanGoForward()
{
return _journal != null && _journal.CanGoForward;
}
}
// GoBack
public class PersonDetailViewModel : BindableBase, INavigationAware
{
IRegionNavigationJournal _journal;
public DelegateCommand GoBackCommand { get; set; }
public PersonDetailViewModel()
{
GoBackCommand = new DelegateCommand(GoBack);
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
_journal = navigationContext.NavigationService.Journal;
}
private void GoBack()
{
_journal.GoBack();
}
}
InvokeCommandAction
Prism提供了InvokeCommandAction以使ViewModel处理View的事件,示例如下:
// view xaml
<ListBox ItemsSource="{Binding Items}" SelectionMode="Single">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<prism:InvokeCommandAction Command="{Binding SelectedCommand}"
TriggerParameterPath="AddedItems" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
// viewmodel
public DelegateCommand<object[]> SelectedCommand { get; private set; }
public MainWindowViewModel()
{
...
SelectedCommand = new DelegateCommand<object[]>(OnItemSelected);
}
private void OnItemSelected(object[] selectedItems)
{
if (selectedItems != null && selectedItems.Count() > 0)
SelectedItemText = selectedItems.FirstOrDefault().ToString();
}
Prism.WPF -- Prism框架使用(下)的更多相关文章
- Prism.WPF -- Prism框架使用(上)
本文参考Prism官方示例 创建Prism项目 将App.xaml中的WPF标准Application替换为PrismApplication,移除StartupUri属性: 将App.xaml.cs中 ...
- .NET Core 3 WPF MVVM框架 Prism系列之命令
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的命令的用法 一.创建DelegateCommand命令 我们在上一篇.NET Core 3 WPF MVVM框架 Prism系列之 ...
- .NET Core 3 WPF MVVM框架 Prism系列之模块化
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的应用程序的模块化 前言 我们都知道,为了构成一个低耦合,高内聚的应用程序,我们会分层,拿一个WPF程序来说,我们通过MVVM模式 ...
- .NET Core 3 WPF MVVM框架 Prism系列之事件聚合器
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的使用事件聚合器实现模块间的通信 一.事件聚合器 在上一篇 .NET Core 3 WPF MVVM框架 Prism系列之模块化 ...
- .NET Core 3 WPF MVVM框架 Prism系列之导航系统
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism基于区域Region的导航系统 在讲解Prism导航系统之前,我们先来看看一个例子,我在之前的demo项目创建一个登录界面: 我们看 ...
- .NET Core 3 WPF MVVM框架 Prism系列之对话框服务
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的对话框服务,这也是prism系列的最后一篇完结文章,下面是Prism系列文章的索引: .NET Core 3 WPF MVVM框 ...
- [Windows] Prism 8.0 入门(下):Prism.Wpf 和 Prism.Unity
1. Prism.Wpf 和 Prism.Unity 这篇是 Prism 8.0 入门的第二篇文章,上一篇介绍了 Prism.Core,这篇文章主要介绍 Prism.Wpf 和 Prism.Unity ...
- .NET Core 3 WPF MVVM框架 Prism系列文章索引
.NET Core 3 WPF MVVM框架 Prism系列之数据绑定 .NET Core 3 WPF MVVM框架 Prism系列之命令 .NET Core 3 WPF MVVM框架 Prism系列 ...
- Prism(WPF) 拐着尝试入门
原文:Prism(WPF) 拐着尝试入门 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/s261676224/article/details/852 ...
随机推荐
- 深入了解JavaScript中基于原型(prototype)的继承机制
原型 前言 继承是面向对象编程中相当重要的一个概念,它对帮助代码复用起到了很大的作用. 正文 Brendan Eich在创建JavaScript时,没有选择当时最流行的类继承机制,而是借鉴Self,用 ...
- Codeforces Round #627 (Div. 3) D - Pair of Topics(双指针)
题意: 有长为n的a,b两序列,问满足ai+aj>bi+bj(i<j)的i,j对数. 思路: 移项得:(ai-bi)+(aj-bj)>0,i<j即i!=j,用c序列保存所有ai ...
- python爬取QQVIP音乐
QQ音乐相比于网易云音乐加密部分基本上没有,但是就是QQ音乐的页面与页面之间的联系太强了,,导致下载一个音乐需要分析前面多个页面,找数据..太繁琐了 1.爬取链接:https://y.qq.com/ ...
- GCD HDU - 1695 容斥原理(复杂度低的版本)
题意: 让你从区间[a,b]里面找一个数x,在区间[c,d]里面找一个数y.题目上已经设定a=b=1了.问你能找到多少对GCD(x,y)=k.x=5,y=7和y=5,x=7是同一对 题解: 弄了半天才 ...
- hdu3506 Monkey Party
Problem Description Far away from our world, there is a banana forest. And many lovely monkeys live ...
- AtCoder Beginner Contest 173 C - H and V (二进制枚举)
题意:有一张图,.表示白色,#表示黑色,每次可以将多行和多列涂成红色(也可不涂),问有多少种方案,使得剩下黑点的个数为\(k\). 题解:又学到了新的算法qwq,因为这题的数据范围很小,所以可以用二进 ...
- 牛客编程巅峰赛S1第5场 - 黄金&钻石&王者 B.牛牛的字符串 (DP)
题意:有一个字符串\(s\),我们可以选择\(s_{i}\),如果\(s_{i+k}>s_{i}\),那么就可以交换\(s_{i}\)和\(s_{i+k}\),问最多能够交换多少次. 题解:因为 ...
- Cobalt Strike特征隐藏
前言 首先红蓝对抗的时候,如果未修改CS特征.容易被蓝队溯源. 前段时间360公布了cobalt strike stage uri的特征,并且紧接着nmap扫描插件也发布了.虽说这个特征很早就被发现了 ...
- Gym 101464C - 计算几何+二分(uva1463)
不是很难,但是我觉得对代码能力的要求还是挺高的. 注意模块化. 因为是浮点数,所以二分用的很多很多. 参考 https://blog.csdn.net/njupt_lyy/article/detail ...
- sdut2878 环形依赖的DP(高斯消元,剪枝后的模板
这题的状态是循环依赖的有环.. 之前一道概率DP,类似有环..但是它是可以消掉的 比如dp[i]=0.3*dp[i+1]+0.2*dp[i+2]+0.5*dp[i]; 完全可以变成,0.5*dp[i] ...