本文参考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框架使用(下)的更多相关文章

  1. Prism.WPF -- Prism框架使用(上)

    本文参考Prism官方示例 创建Prism项目 将App.xaml中的WPF标准Application替换为PrismApplication,移除StartupUri属性: 将App.xaml.cs中 ...

  2. .NET Core 3 WPF MVVM框架 Prism系列之命令

    本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的命令的用法 一.创建DelegateCommand命令 我们在上一篇.NET Core 3 WPF MVVM框架 Prism系列之 ...

  3. .NET Core 3 WPF MVVM框架 Prism系列之模块化

    本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的应用程序的模块化 前言  我们都知道,为了构成一个低耦合,高内聚的应用程序,我们会分层,拿一个WPF程序来说,我们通过MVVM模式 ...

  4. .NET Core 3 WPF MVVM框架 Prism系列之事件聚合器

    本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的使用事件聚合器实现模块间的通信 一.事件聚合器  在上一篇 .NET Core 3 WPF MVVM框架 Prism系列之模块化 ...

  5. .NET Core 3 WPF MVVM框架 Prism系列之导航系统

    本文将介绍如何在.NET Core3环境下使用MVVM框架Prism基于区域Region的导航系统 在讲解Prism导航系统之前,我们先来看看一个例子,我在之前的demo项目创建一个登录界面: 我们看 ...

  6. .NET Core 3 WPF MVVM框架 Prism系列之对话框服务

     本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的对话框服务,这也是prism系列的最后一篇完结文章,下面是Prism系列文章的索引: .NET Core 3 WPF MVVM框 ...

  7. [Windows] Prism 8.0 入门(下):Prism.Wpf 和 Prism.Unity

    1. Prism.Wpf 和 Prism.Unity 这篇是 Prism 8.0 入门的第二篇文章,上一篇介绍了 Prism.Core,这篇文章主要介绍 Prism.Wpf 和 Prism.Unity ...

  8. .NET Core 3 WPF MVVM框架 Prism系列文章索引

    .NET Core 3 WPF MVVM框架 Prism系列之数据绑定 .NET Core 3 WPF MVVM框架 Prism系列之命令 .NET Core 3 WPF MVVM框架 Prism系列 ...

  9. Prism(WPF) 拐着尝试入门

    原文:Prism(WPF) 拐着尝试入门 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/s261676224/article/details/852 ...

随机推荐

  1. charles(1)解决charles抓包乱码问题

    前言 当使用Charles抓包时,发现数据都是乱码,这时需要安装证书 解决办法 1.点击charles窗口,点击左上角Help-> SSL Proxying -> Install Char ...

  2. Pytest(9)skip跳过用例

    前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 Skip和xfail: 处理那些不会成功的测试用例 你可以对那些在某些特定平台上不能运行的测试用 ...

  3. 深入理解Js中的this

    深入理解Js中的this JavaScript作用域为静态作用域static scope,但是在Js中的this却是一个例外,this的指向问题就类似于动态作用域,其并不关心函数和作用域是如何声明以及 ...

  4. 2019牛客暑期多校训练营(第五场)G-subsequence 1

    >传送门< 题意:给你两个数字字符串s,t,求字符串s的子序列比字符串t大的个数 思路:他的题解上写的就是dp的基础练习题,好像的确是这么回事,既然是dp,那么对于定义的状态不同得到的转移 ...

  5. Codeforces Round #622 (Div. 2) A. Fast Food Restaurant(全排列,DFS)

    Codeforces Round #622 (Div. 2) A. Fast Food Restaurant 题意: 你是餐馆老板,虽然只会做三道菜,上菜时还有个怪癖:一位客人至少上一道菜,且一种菜最 ...

  6. P1541 乌龟棋(DP)

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行NNN个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第NNN格是终点,游戏要求玩家控制一个乌龟 ...

  7. Codeforces Round #521 (Div. 3) E. Thematic Contests (离散化,二分)

    题意:有\(n\)个话题,每次都必须选取不同的话题,且话题数必须是上次的两倍,第一次的话题数可以任意,问最多能选取多少话题数. 题解:我们首先用桶来记录不同话题的数量,因为只要求话题的数量,与话题是多 ...

  8. Qt内部的d指针和q指针手把手教你实现

    Qt内部的d指针和q指针 在讲Qt的D指针之前让我们来简单的解释一下D指针出现的目的,目的是什么呢?保证模块间的二进制兼容. 什么是二进制兼容呢,简单说就是如果自己的程序使用了第三方模块,二进制兼容可 ...

  9. Bing壁纸-20200416

  10. Ubuntu——常用命令

    一.进入文件夹 1.cd .. # 进入上一个文件夹 2.cd ../.. # 进入上两个文件夹 3.cd - # 去到进入当前文件夹之前的那个文件夹 二.复制文件 cp 目标文件 复制文件名 三.移 ...