自定义界面如下:
1、App.xaml.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//注册对话服务
containerRegistry.RegisterSingleton<IDialogService, DialogService>();
containerRegistry.RegisterDialog<MotionLoadView, MotionLoadViewModel>();
containerRegistry.RegisterDialog<WarningMsgView, WarningMsgViewModel>("warning");
containerRegistry.RegisterDialog<InfoMsgView, InfoMsgViewModel>("info");
containerRegistry.RegisterDialog<ErrorMsgView, ErrorMsgViewModel>("error");
containerRegistry.RegisterDialog<InfoToastView, InfoToastViewModel>("toastInfo");
}
2、弹出界面布局xaml
<UserControl
x:Class="Views.Pages.IDialogs.MsgView.WarningMsgView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:YOUI="clr-namespace:MotionWindows.Styles.Control"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Views.Pages.IDialogs.MsgView"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
Width="400"
Height="200"
prism:ViewModelLocator.AutoWireViewModel="True"
Background="{DynamicResource SecundaryBackgroundColor}"
MouseMove="MsgView_MouseMove"
mc:Ignorable="d">
<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="Width" Value="400" />
<Setter Property="Height" Value="200" />
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="WindowState" Value="Normal" />
<Setter Property="WindowChrome.CornerRadius" Value="5" />
<Setter Property="Topmost" Value="True" />
<Setter Property="ShowInTaskbar" Value="False" />
<Setter Property="WindowChrome.GlassFrameThickness" Value="-1" />
<Setter Property="prism:Dialog.WindowStartupLocation" Value="CenterScreen" />
</Style>
</prism:Dialog.WindowStyle>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DockPanel Grid.Row="0" VerticalAlignment="Center">
<TextBlock
Margin="18,0,0,0"
HorizontalAlignment="Left"
FontSize="16"
FontWeight="Bold"
Foreground="{DynamicResource TextSecundaryColor}"
Text="{Binding Title}" />
<Button
HorizontalAlignment="Right"
VerticalAlignment="Center"
HorizontalContentAlignment="Right"
Command="{Binding CancelCommand}"
Content=""
Cursor="Hand"
Style="{DynamicResource MouseOverButton}"
Tag="IsBtnClose" />
</DockPanel>
<StackPanel
Grid.Row="1"
VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock
Margin="18,5,5,5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="{DynamicResource iconfont}"
FontSize="48"
FontWeight="Bold"
Foreground="{DynamicResource PrimaryYellowColor}"
Text="" />
<TextBlock
Margin="15,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="16"
Foreground="{DynamicResource TextPrimaryColor}"
Text="{Binding Content}" />
</StackPanel>
<StackPanel
Grid.Row="2"
HorizontalAlignment="Right"
Orientation="Horizontal">
<YOUI:UButton
x:Name="btn1"
Width="80"
Height="35"
Margin="0,20,0,5"
Background="{DynamicResource PrimaryBlueColor}"
BorderBrush="{DynamicResource PrimaryBlueColor}"
Command="{Binding ConfirmCommand}"
Content="确定"
Cursor="Hand"
Foreground="White" />
<YOUI:UButton
x:Name="btn2"
Width="80"
Height="35"
Margin="20,20,15,5"
Background="{DynamicResource PrimaryGreen}"
BorderBrush="{DynamicResource PrimaryGreen}"
Command="{Binding CancelCommand}"
Content="取消"
Cursor="Hand"
Foreground="White" />
</StackPanel>
</Grid>
</UserControl>
3 、弹窗model 引用IDialogAware 接口
public class WarningMsgViewModel : NewBindableBase, IDialogAware
{
private string title = "警告信息标题";
public string Title
{
get { return title; }
set { title = value; BeginInvokeRaisePropertyChanged(); }
}
private string content = "信息内容";
public event Action<IDialogResult> RequestClose;
public string Content
{
get { return content; }
set { content = value; BeginInvokeRaisePropertyChanged(); }
}
private bool isOpenMask = false;
public bool IsOpenMask
{
get { return isOpenMask; }
set { isOpenMask = value; BeginInvokeRaisePropertyChanged(); }
}
public DelegateCommand ConfirmCommand { get; set; }
public DelegateCommand CancelCommand { get; set; }
public WarningMsgViewModel()
{
ConfirmCommand = new DelegateCommand(Ok);
CancelCommand = new DelegateCommand(Cancel);
}
private void Cancel()
{
OnDialogClosed();
}
private void Ok()
{
IsClosedMask();
DialogParameters param = new DialogParameters();
param.Add("resultMsg", "ok");
RequestClose?.Invoke(new DialogResult(ButtonResult.OK, param));
}
public void IsClosedMask()
{
if (IsOpenMask)
{
DialogsEx.DialogMask(false);
}
}
public bool CanCloseDialog()
{
return true;
}
public void OnDialogClosed()
{
IsClosedMask();
DialogParameters param = new DialogParameters();
param.Add("resultMsg", "no");
RequestClose?.Invoke(new DialogResult(ButtonResult.No, param));
}
public void OnDialogOpened(IDialogParameters parameters)
{
if (parameters.ContainsKey("IsOpenMask"))
IsOpenMask = parameters.GetValue<bool>("IsOpenMask");
if (IsOpenMask == true)
{
DialogsEx.DialogMask(true);
}
if (parameters.ContainsKey("Title"))
Title = parameters.GetValue<string>("Title");
if (parameters.ContainsKey("Content"))
Content = parameters.GetValue<string>("Content");
}
}
4、调用
private async void WindowCloseAsync()
{
var dialogs = await DialogsEx.AlertBOXAsync(_dialogService, "warning", "系统提示", "是否要退出软件...", true);
if (dialogs.Result == ButtonResult.OK)
{
Environment.Exit(0);
}
}
总结:其中要注意的是请先理解prism框架 IDialogService 服务和IDialogAware接口的使用
附上:prism框架的示例Demo文章来源:https://www.toymoban.com/news/detail-585878.html
github.com/PrismLibrary/Prism-Samples-Wpf文章来源地址https://www.toymoban.com/news/detail-585878.html
到了这里,关于Prism框架自定义弹出提示窗的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!