<Window x:Class="AVA.Meeting.Client.Text"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:z="clr-namespace:Z.Core.WPF;assembly=Z.Core.WPF"
z:Closed.Command="{Binding OnClosed}">
<StackPanel>
<Button Content="关闭窗体" Command="{Binding OnClosed}" />
</StackPanel>
</Window>
using Microsoft.Practices.Prism.Commands;
using System.Windows;
using System.Windows.Input;namespace AVA.Meeting.Client
{
/// <summary>
/// Text.xaml 的交互逻辑
/// </summary>
public partial class Text : Window
{
public Text()
{
InitializeComponent();
this.DataContext = new TextViewModel();
}
} class TextViewModel
{
public ICommand OnClosed { get; set; } public TextViewModel()
{
this.OnClosed = new DelegateCommand<string>(str =>
{
MessageBox.Show("要关闭窗体了");
});
}
}
}在我的windows的Closed绑定事件后,
运行程序点击Button按钮后并没有关闭窗体
这是因为Button的事件只是当作一个普通事件指行,
而点击窗体的“X”后,程序是先关闭了窗体后再触发当前事件,这地程序会卡住一会(因为报错报不出来,程序已经在关闭了。
问,那如何绑定一个后台事件给Windows
且触发这个命令后可以关闭窗体
谢谢
解决方案 »
- C# MS SQL Server 获取数据库中的图片
- C#字符串和正则表达式参考手册【pdf】下载
- 有哪位用过DevExpress.XtraScheduler.SchedulerControl控件的
- 如何修改已存在的注册表键值
- 紧急求助/???????.net开发问题,求解法
- datagrid我的列是动态生成加上去的,为什么它的items.count总是0?急。。。。。
- winform 地图
- 如何让datagrid 和textbox的数据保持同步?
- 请教关于导出数据库到Excel的程序在生成安装程序后,安装程序不能运行的问题!(各位高人帮忙啊!)
- c#用钩子开发金蝶K3外挂的问题
- 求datagridview和数据库的数据同步更新方法
- |ZYCWPF| notifyIcon_MouseClick事件中当窗体最小化后再点回来时在我的WIN7不会显示到当前窗体,而是还是最小化 谢谢
使用OnClosing。
所以会有异常。建议退出处理还是在CodeBehind 里做。
而MVVM只处理数据?
我也这样做过,这样做的话在MVVM和窗口要进行交互的时候,总得声明事件来进行传递很是麻烦
谢谢
这样感觉就是交差调用啊
谢谢
或者通过 Behavior 拦截窗体的 Closing 事件,交给Command处理。
var vm = this.DataContext as XXXViewModel;
if (vm != null) {
vm.ExitCommand();
}
1:在关闭X时发起
2:在窗体有按钮控件发起(这个按钮是MVVM的)
点击X发起,那么可以用behavior绑定closing事件到command上。
如果是按钮点击,就在CodeBehind里调用this.Close()
然后会触发closing事件,走上面的绑定。
http://pan.baidu.com/share/link?shareid=117997&uk=3224032458
上面是示例代码
出现问题是:
点X关闭正常
点按钮只是执行事件,但不关闭
如是如果在事件中添加this.Close()
的话,点X,就会进行死循环,会多次执行我的OnClosed里面的内容(但程序是可以关掉的,我想是因为第一个Close的时候执行了,所以死循环也会退出)
谢谢
我的意思是在Button_Click里调用this.Close();然后走窗体Closing EventTrigger 调用 VM 的 CloseCommand
MainViewModel: (窗体关闭时通过 Behavior 拦截触发) private RelayCommand _closeCommand;
public RelayCommand CloseCommand
{
get
{
if (_closeCommand == null)
{
_closeCommand = new RelayCommand(() => {
Console.WriteLine("CloseCommand");
});
}
return _closeCommand;
}
}CodeBehind:MainWindow.xaml.cs public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.Close();
}
}Xmal
<Window x:Class="MVVMClosingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
DataContext="{Binding Main, Source={StaticResource Locator}}"
xmlns:gmc="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
Title="MainWindow" Height="350" Width="525">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<gmc:EventToCommand PassEventArgsToCommand="True" Command="{Binding CloseCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="47,213,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/> </Grid>
</Window>
现在可以了
谢谢