如何在WPF中打开/关闭新窗口而不违反MVVM模式的规则?
我只想模仿ms office Outlook的登录模块。
我已经读过这篇文章,但是传递参数时出错confirmation
我目前正在使用棱镜5.0。
您是否使用Prism 7?
如果是,则立即停止阅读, 如果不是,请转到下面的Prism 7答案
,然后继续阅读
更新
导致我提出另一个答案的原因是无法使用Prism 6在我的项目中应用公认的答案,
但是在放入原始答案(请参阅下文)并在注释中进行讨论之后,我发现核心问题是:该棱镜6改变了一些类的命名空间,这在公认的答案使用的所有类仍然存在于棱镜6,但在另一个DLL和命名空间
因此,如果您使用的棱镜6,你可以申请使用这些修改的接受的答案
首先替换那些名称
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:pi="clr-namespace:Microsoft.Practices.Prism.Interactivity;assembly=Microsoft.Practices.Prism.Interactivity"
xmlns:pit="clr-namespace:Microsoft.Practices.Prism.Interactivity.InteractionRequest;assembly=Microsoft.Practices.Prism.Interactivity"
具有以下名称空间
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:prism="http://prismlibrary.com/"
第二次更新XAML,如下所示
<Button Content="Options" Command="{Binding OpenConnectionOptionsCommand}">
<i:Interaction.Triggers>
<prism:InteractionRequestTrigger SourceObject="{Binding OptionSettingConfirmationRequest, Mode=OneWay}" >
<prism:PopupWindowAction>
<prism:PopupWindowAction.WindowContent>
<views:CustomPopupView />
</prism:PopupWindowAction.WindowContent>
</prism:PopupWindowAction>
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
</Button>
注意1
确保所使用的视图(在上面的示例中<views:CustomPopupWindow>
)不是窗口,否则您将收到异常。
注意2 仅在使用Prism 6时才
需要进行这些修改,因为(如我在下面的原始答案中所述),Prism 6中已弃用了接受的答案所使用的dll 。
注意3
请确保您引用的是Prism.Wpf
dll,可以从Nuget下载。
实际上,您在问题中引用的文章非常有帮助(至少对我而言),并且不会崩溃。
我将尝试总结该文章。
视图模型
public class ViewModel : BindableBase
{
public ViewModel()
{
_showWindowCommand = new DelegateCommand(ShowWindow);
_interactionRequest = new InteractionRequest<Confirmation>();
}
private readonly DelegateCommand _showWindowCommand;
private InteractionRequest<Confirmation> _interactionRequest;
public ICommand ShowWindowCommand
{
get { return _showWindowCommand; }
}
public IInteractionRequest InteractionRequest
{
get { return _interactionRequest; }
}
private void ShowWindow()
{
_interactionRequest.Raise(
new Confirmation(),
OnWindowClosed);
}
private void OnWindowClosed(Confirmation confirmation)
{
if (confirmation.Confirmed)
{
//perform the confirmed action...
}
else
{
}
}
}
XAML
<Button Command="{Binding ShowWindowCommand}" Content="Show Window" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Raised" SourceObject="{Binding InteractionRequest}">
<i:EventTrigger.Actions>
<local:ShowWindowAction></local:ShowWindowAction>
</i:EventTrigger.Actions>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
并且您将需要使用那些名称空间
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:The namespace which contains the ShowWindowAction">
动作触发
using System;
using Prism.Interactivity.InteractionRequest;
using System.Windows.Interactivity;
using System.Windows;
public class ShowWindowAction : TriggerAction<FrameworkElement>
{
protected override void Invoke(object parameter)
{
InteractionRequestedEventArgs args = parameter as InteractionRequestedEventArgs;
if (args != null)
{
Confirmation confirmation = args.Context as Confirmation;
if (confirmation != null)
{
// Replace ParametersWindow with your own window.
ParametersWindow window = new ParametersWindow();
EventHandler closeHandler = null;
closeHandler = (sender, e) =>
{
window.Closed -= closeHandler;
args.Callback();
};
window.Closed += closeHandler;
window.Show();
}
}
}
}
说明
Prism.Core
和Prism.Wpf
dll才能使此代码正常工作。ShowWindow
方法,将触发的Invoke
方法,该方法ShowWindowAction
将真正显示窗口。OnWindowClosed
,我们将其作为回调传递给ShowWindowAction
该类,并在窗口真正关闭时从那里调用它。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句