您必须做两件事,创建新的 MetroWindow 对话框,然后覆盖 PopupWindowAction 以使用它们。听起来很啰嗦,但只需要 10 分钟:
首先,创建您自己的继承自MetroWindow 的确认和通知窗口,就像任何其他MetroWindow 一样。您可以从 prism 源复制原始的确认和通知窗口,然后按照mahapps quick start 的建议进行更改。
所以确认窗口会是这样的:
<Controls:MetroWindow x:Class="MyApp.DefaultPopupWindows.DefaultConfirmationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
MinWidth="300" MinHeight="150"
Title="{Binding Title}"
BorderBrush="{DynamicResource AccentColorBrush}"
BorderThickness="1">
<Grid x:Name="LayoutRoot" Margin="5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" Content="{Binding Content}"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="OkButton" Content="OK" Width="75" Height="25" HorizontalAlignment="Right" Margin="0,10,0,0" Click="OkButton_Click" />
<Button x:Name="CancelButton" Content="Cancel" Width="75" Height="25" HorizontalAlignment="Right" Margin="20,10,0,0" Click="CancelButton_Click" />
</StackPanel>
</Grid>
</Controls:MetroWindow>
以及后面的相关代码:
using MahApps.Metro.Controls;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using System.Windows;
namespace MyApp.DefaultPopupWindows
{
/// <summary>
/// Interaction logic for ConfirmationChildWindow.xaml
/// </summary>
public partial class DefaultConfirmationWindow : MetroWindow
{
/// <summary>
/// Creates a new instance of ConfirmationChildWindow.
/// </summary>
public DefaultConfirmationWindow()
{
InitializeComponent();
}
/// <summary>
/// Sets or gets the <see cref="IConfirmation"/> shown by this window./>
/// </summary>
public IConfirmation Confirmation
{
get
{
return this.DataContext as IConfirmation;
}
set
{
this.DataContext = value;
}
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
this.Confirmation.Confirmed = true;
this.Close();
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.Confirmation.Confirmed = false;
this.Close();
}
}
}
在此之后,您可以创建自己的 PopupWindowAction 来扩展 prism 中的操作。在该类中,您覆盖 GetWindow 函数:
protected override Window GetWindow(INotification notification)
{
MetroWindow wrapperWindow;
if (this.WindowContent != null)
{
wrapperWindow = new MetroWindow();
// If the WindowContent does not have its own DataContext, it will inherit this one.
wrapperWindow.DataContext = notification;
wrapperWindow.Title = notification.Title;
this.PrepareContentForWindow(notification, wrapperWindow);
}
else
{
wrapperWindow = this.CreateDefaultWindow(notification);
}
return wrapperWindow;
}
您还必须为“CreateDefaultWindow”提供您自己的实现,这将创建相应窗口的新 MetroWindow 版本:
protected new MetroWindow CreateDefaultWindow(INotification notification)
{
MetroWindow window = null;
if (notification is IConfirmation)
{
window = new DefaultPopupWindows.DefaultConfirmationWindow() { Confirmation = (IConfirmation)notification };
}
else
{
window = new DefaultPopupWindows.DefaultNotificationWindow() { Notification = notification };
}
return window;
}
最后,在您自己的视图/窗口的 InteractionRequest 中,指定这个新的 PopupWindowAction,而不是 prism 版本。