Prism框架 如何在主程序中合理的弹出子窗体
说起子窗体,大家都会想到ChildWindow,多熟悉的一个控件。不错,Sliverlight中已经提供了子窗体的具体实现,而在WPF中却没有这么好的事情(有的第三方控件商已经提供此控件)。最常见的实现方法就是在ViewModel中,直接New ChildWindow,然后直接Show。这样的方法也达到的要求。但是它不符合MVVM分层思想,再就是代码不美观,难以维护,今天我就给大家介绍一种美观又实用的方法。
通过Prism中提供的InteractionRequestTrigger事件触发器,实现点击按钮或者用户的某种操作弹出对话框的效果。另外,不要忘了引用此命名空间:
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
<Window x:Class="ChildWindowDemo.ChildWindow.ChildWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
Width="" Height=""
Title="{Binding Title}"
x:Name="confirmationWindow" Topmost="True" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen">
<Grid x:Name="LayoutRoot" Margin="">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions> <ContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="" Content="{Binding Content}"/> <Button Content="Cancel" Width="" Height="" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding ElementName=confirmationWindow}" MethodName="Close"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="OK" Width="" Height="" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:ChangePropertyAction PropertyName="Confirmed" TargetObject="{Binding}" Value="True"/>
<ei:CallMethodAction TargetObject="{Binding ElementName=confirmationWindow}" MethodName="Close"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Window>
创建ChildWindow的基类
新建类:ChildWindowActionBase 并从TriggerAction<T>派生,代码如下:
public class ChildWindowActionBase : TriggerAction<FrameworkElement>
{
protected override void Invoke(object parameter)
{
var arg = parameter as InteractionRequestedEventArgs;
if (arg == null)
return; var windows = this.GetChildWindow(arg.Context); var callback = arg.Callback;
EventHandler handler = null;
handler =
(o, e) =>
{
windows.Closed -= handler;
callback();
};
windows.Closed += handler; windows.ShowDialog(); } Window GetChildWindow(Notification notification)
{
var childWindow = this.CreateDefaultWindow(notification);
childWindow.DataContext = notification; return childWindow;
} Window CreateDefaultWindow(Notification notification)
{
return (Window)new ChildWindow.ChildWindow();
}
}
到此子窗体已经完成
<Window x:Class="ChildWindowDemo.MainWindow"
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:prism="http://www.codeplex.com/prism"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:ChildWindowDemo"
Title="MainWindow" Height="" Width="">
<i:Interaction.Triggers>
<prism:InteractionRequestTrigger SourceObject="{Binding ConfirmationRequest, Mode=OneWay}">
<local:ChildWindowActionBase/>
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
<Grid>
<Button Command="{Binding RaiseConfirmation}" Content="Click Me !" HorizontalAlignment="Left" Margin="29,31,0,0" VerticalAlignment="Top" Width="" Height=""/>
<TextBlock HorizontalAlignment="Left" Margin="29,106,0,0" TextWrapping="Wrap" Text="{Binding ConfirmationResult}" VerticalAlignment="Top"/>
</Grid>
</Window>
对之对应的ViewModel:
public class MainWindowViewModel : NotificationObject
{
public MainWindowViewModel()
{
this.RaiseConfirmation = new DelegateCommand(this.OnRaiseConfirmation);
this.ConfirmationRequest = new InteractionRequest<Confirmation>();
} public InteractionRequest<Confirmation> ConfirmationRequest { get; private set; } public DelegateCommand RaiseConfirmation { get; private set; } private string result;
public string ConfirmationResult
{
get { return result; }
set
{
result = value;
this.RaisePropertyChanged(() => this.ConfirmationResult);
}
} private void OnRaiseConfirmation()
{
this.ConfirmationRequest.Raise(
new Confirmation { Content = "是否确认", Title = "子窗体" },
(cb) => { ConfirmationResult = cb.Confirmed ? "确认" : "取消"; });
}
}
这样的写法比较符合MVVM的分层思想,子窗体可以随心定制,而不需要去改逻辑层的代码。
http://www.cnblogs.com/sunthx/p/3539900.html
Prism框架 如何在主程序中合理的弹出子窗体的更多相关文章
- [Prism框架实用分享]如何在主程序中合理的弹出子窗体
大家好 说起子窗体,大家都会想到ChildWindow,多熟悉的一个控件.不错,Sliverlight中已经提供了子窗体的具体实现,而在WPF中却没有这么好的事情(有的第三方控件商已经提供此控件).最 ...
- bootstrap中popover.js(弹出框)使用总结+案例
bootstrap中popover.js(弹出框)使用总结+案例 *转载请注明出处: 作者:willingtolove: http://www.cnblogs.com/willingtolove/p/ ...
- 在DLL动态链接库中封装VCL的MDI子窗体
在DLL动态链接库中封装VCL的MDI子窗体不多说了,看代码就应该明白了,曾经我遇到的问题,现在放出来大家共享! 这里是工程文件的部分: 在DLL中封装MDI子窗体需要重写DLL入口函数,具体代码如下 ...
- 在没有界面的类中,实现弹出UIAlertView || 在没有界面的类中,刷新程序界面 思路
+(DisplayErrorMsg *)sharedDisplayErrorMsg { static DisplayErrorMsg *instance = nil; @synchronized(in ...
- 在IOS11中position:fixed弹出框中的input出现光标错位的问题
问题出现的背景: 在IOS11中position:fixed弹出框中的input出现光标错位的问题 解决方案 一.设计交互方面最好不要让弹窗中出现input输入框: 二.前端处理此兼容性的方案思路: ...
- firefox浏览器中 bootstrap 静态弹出框中select下拉框不能弹出(解决方案)
问题出现场景1: 在firefox浏览器中在bootstrap弹出的modal静态框中再次弹出一个静态框时 select下拉框不能弹出选项 解决方案:去掉最外层静态框的 tabindex=" ...
- Jquery和Javascript 实际项目中写法基础-弹出窗和弹出层 (4)
一.实际项目中有很多如下界面效果. 二.该效果可以归结为弹出窗或者弹出层来实现的,为什么这么说?看如下代码: <!DOCTYPE html> <html> & ...
- asp.net中的窗口弹出实现,包括分支窗口 . ASP.NET返回上一页面实现方法总结 .
返回上一页的这个东东在我们做项目的时候一般是用于填写完表单后确认的时候,有对原来输入的数据进行修改或者更新时用的,或者是因为网站为了方便浏览者而有心添加的一个东东,一般这种功能的实现在ASP.NET中 ...
- iOS下Html页面中input获取焦点弹出键盘时挡住input解决方案—scrollIntoView()
问题描述 iOS系统下,移动web页面,inpu获取焦点弹出系统虚拟键盘时,偶尔会出现挡住input的情况,尽管概率不大,但是十分影响用户体验. 问题重现 原始页面:页面中有header.main.f ...
随机推荐
- CTAP: Complementary Temporal Action Proposal Generation论文笔记
主要观点:基于sliding window(SW)类的方法,如TURN,可以达到很高的AR,但定位不准:基于Group的方法,如TAG,AR有明显的上界,但定位准.所以结合两者的特长,加入Comple ...
- Linux用户组管理及用户权限4
权限管理: ls -l rwxrwxrwx: 左三位:定义user(owner)的权限 中三位:定义group的权限 ...
- 中国剩余定理(crt)和扩展中国剩余定理(excrt)
数论守门员二号 =.= 中国剩余定理: 1.一次同余方程组: 一次同余方程组是指形如x≡ai(mod mi) (i=1,2,…,k)的同余方程构成的组 中国剩余定理的主要用途是解一次同余方程组,其中m ...
- USRP B210 更改A通道或B通道
USRP B210 更改A通道或B通道: 默认是A通道进行发射/接收,或设置 Mb0:Subdev Spec: A:A 设置B通道进行发射/接收,设置 Mb0:Subdev Spec: A:B
- poj3691 DNA repair[DP+AC自动机]
$给定 n 个模式串,和一个长度为 m 的原串 s,求至少修改原串中的几个字符可以使得原串中不包含任一个模式串.模式串总长度 ≤ 1000,m ≤ 1000.$ 先建出模式串的AC自动机,然后考虑怎么 ...
- linux系统常用命令(一)
管理 在UNIX/linux系统中,一切皆为文件:若非文件,则为进程.首先认识文件系统: linux文件系统 /var - 经常变化的(variable)文件,诸如日志或数据库等 /usr - 包含绝 ...
- Eclipse 的 CheckStyle 插件
Eclipse 的 CheckStyle 插件 1.简介 Checkstyle 是 SourceForge 下的一个开源项目,提供了一个帮助 JAVA 开发人员遵守某些编码规范的工具.它能进行自动化代 ...
- 【Winform-自定义控件】ImageButton 支持鼠标正常、悬停、按下更改图片,支持文本
原文地址:https://www.codeproject.com/Articles/29010/WinForm-ImageButton 自定义winfrom图片按钮:支持鼠标正常.悬停.按下更改图片, ...
- B/S上传超大文件解决方案
4GB以上超大文件上传和断点续传服务器的实现 随着视频网站和大数据应用的普及,特别是高清视频和4K视频应用的到来,超大文件上传已经成为了日常的基础应用需求. 但是在很多情况下,平台运营方并没有大文件上 ...
- monkey test——学习资料
出处: http://www.testwo.com/blog/6107 http://www.testwo.com/blog/6146 http://www.testwo.com/blog/6188 ...