我正在尝试在WPF Prism Desktop应用程序中实现模式对话框。
从Prism指导我可以看到正确的方法应该是使用Interaction:
<i:Interaction.Triggers>
<prism:InteractionRequestTrigger
SourceObject="{Binding ConfirmCancelInteractionRequest}">
<prism:PopupChildWindowAction
ContentTemplate="{StaticResource ConfirmWindowTemplate}"/>
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
但桌面的Microsoft.Practices.Prism.Interactivity.DLL库中没有PopupChildWindowAction
,只有Silverlight?
我可以google在WPF(Prism)中使用Modal Dialog的许多不同实现,但只是想知道为什么Prism Desktop DLL中缺少这个功能并且可以在Silverlight DLL中使用? 我可以使用交互服务,但建议使用交互请求作为更适合MVVM应用的方法。
答案 0 :(得分:6)
这是真的,它只存在于Silverlight棱镜库中,
你可以做的就是自己创造。
CS:
public class OpenPopupWindowAction : TriggerAction<FrameworkElement>
{
protected override void Invoke(object parameter)
{
var popup = (ChildWindow)ServiceLocator.Current.GetInstance<IPopupDialogWindow>();
popup.Owner = PlacementTarget ?? (Window)ServiceLocator.Current.GetInstance<IShell>();
popup.DialogResultCommand = PopupDailogResultCommand;
popup.Show();
}
public Window PlacementTarget
{
get { return (Window)GetValue(PlacementTargetProperty); }
set { SetValue(PlacementTargetProperty, value); }
}
public static readonly DependencyProperty PlacementTargetProperty =
DependencyProperty.Register("PlacementTarget", typeof(Window), typeof(OpenPopupWindowAction), new PropertyMetadata(null));
public ICommand PopupDailogResultCommand
{
get { return (ICommand)GetValue(PopupDailogResultCommandProperty); }
set { SetValue(PopupDailogResultCommandProperty, value); }
}
public static readonly DependencyProperty PopupDailogResultCommandProperty =
DependencyProperty.Register("PopupDailogResultCommand", typeof(ICommand), typeof(OpenPopupWindowAction), new PropertyMetadata(null));
}
XAML:
<i:EventTrigger SourceObject="{Binding}" EventName="NavigatedFrom">
<popup:OpenPopupWindowAction PopupDailogResultCommand="{Binding OnNavigationConfirmed}"/>
</i:EventTrigger>
如果你需要的话,那就是DialogWindow的代码吧。
cs:
public partial class ChildWindow : Window, IPopupDialogWindow
{
public ChildWindow()
{
InitializeComponent();
DataContext = this;
}
public new PopupDialogResult DialogResult
{
get;
set;
}
public System.Windows.Input.ICommand DialogResultCommand
{
get;
set;
}
}
xaml:
<Window x:Class="Utils.ActionPopupWindow.ChildWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="400" WindowStartupLocation="CenterOwner"
xmlns:popup="clr-namespace:Utils.ActionPopupWindow"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Name="popUpWindow"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30">
This is a child window <LineBreak/> launched from the <LineBreak/>main window
</TextBlock>
<StackPanel Grid.Row="1" Background="#FFA6A6A6">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Ok"
MinWidth="100"
Command="{Binding DialogResultCommand}"
CommandParameter="{x:Static popup:PopupDialogResult.OK}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="Close" TargetObject="{Binding ElementName=popUpWindow}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="Cancel"
MinWidth="100"
Command="{Binding DialogResultCommand}"
CommandParameter="{x:Static popup:PopupDialogResult.Cancel}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="Close" TargetObject="{Binding ElementName=popUpWindow}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</StackPanel>
</Grid>
答案 1 :(得分:0)