Prism框架自定义弹出提示窗

自定义界面如下:

 

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="&#xed23;"
        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="&#xeb6c;" />

      <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

github.com/PrismLibrary/Prism-Samples-Wpf

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值