WPF 蒙版弹窗(背景层是一个半透明的遮罩蒙版)

蒙版弹窗

        蒙版弹窗是背景层是一个半透明的遮罩蒙版,作用是给用户提供信息的时候,强调弹窗信息,用户需要在关掉弹窗后才能操作其他界面。

代码实现

新建蒙版窗体

<Window x:Class="MaskPoumpDemo.MaskMessagePoump"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MaskPoumpDemo"
        mc:Ignorable="d"
        Title="MaskMessagePoump" Height="450" Width="800"
        ShowInTaskbar="False" WindowStyle="None" AllowsTransparency="True" Background="{x:Null}" 
        Loaded="Window_Loaded" >
    <Grid>
        <Border x:Name="backBorder" Background="#181818" Opacity="0.3" CornerRadius="6" />
        <Border Height="300" Width="400" CornerRadius="6" Background="PaleGreen">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Text="欢迎来到新世界!" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <Button x:Name="btnGo" Content="开启" Click="btnGo_Click" FontSize="18" Width="100" Height="30" Grid.Row="1"/>
            </Grid>
        </Border>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MaskPoumpDemo
{
    /// <summary>
    /// MaskMessagePoump.xaml 的交互逻辑
    /// </summary>
    public partial class MaskMessagePoump : Window
    {
        public MaskMessagePoump()
        {
            InitializeComponent();
        }

        public MaskMessagePoump(Window win)
        {
            InitializeComponent();
            this.Width = win.ActualWidth;
            this.Height = win.ActualHeight;
            this.Left = win.Left;
            this.Top = win.Top;
        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
           DialogResult = true;
        }

    }
}

主窗体使用蒙版弹窗

<Window x:Class="MaskPoumpDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MaskPoumpDemo"
        mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True" Background="{x:Null}" 
        Title="MainWindow" Height="450" Width="800" WindowStartupLocation="CenterScreen">
    <Border Background="LightSkyBlue" CornerRadius="8" MouseLeftButtonDown="Border_MouseLeftButtonDown">
        <Grid>
            <TextBlock Text="主窗体上显示蒙版弹窗" FontSize="30" Foreground="White"  Margin="20"/>
            <Button x:Name="btnMessage" Content="显示蒙版弹窗" Click="btnMessage_Click" Width="120" Height="40"/>
        </Grid>
    </Border>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MaskPoumpDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnMessage_Click(object sender, RoutedEventArgs e)
        {
            new MaskMessagePoump(this).ShowDialog();
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
    }
}

实例链接:https://download.csdn.net/download/lvxingzhe3/88683471?spm=1001.2014.3001.5503

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF(Windows Presentation Foundation)是一种用于创建Windows应用程序的开发框架。而Prism是一种用于构建可扩展、模块化和可重用的WPF应用程序的开发框架。在WPF Prism中,弹窗可以通过对话框的方式来实现。 在WPF Prism中,可以使用对话框服务(DialogService)来创建和管理弹窗。DialogService提供了一系列用于显示、关闭和传递参数给弹窗的方法。可以通过注册DialogService服务来在整个应用程序中使用。 要创建一个弹窗,首先需要定义一个弹窗的View和ViewModel。View通常是一个UserControl,用于定义弹窗的界面。ViewModel负责处理弹窗的逻辑和与数据的交互。 在需要显示弹窗的地方,可以使用DialogService的Show方法来显示弹窗。Show方法接收一个字符串参数来指定要显示的弹窗的名称,该名称应与弹窗的View名称相对应。还可以通过Show方法传递要传递给弹窗的参数。 在ViewModel中,可以通过实现INavigationAware接口来获取传递给弹窗的参数。这样,在弹窗显示后,ViewModel就可以使用这些参数来进行必要的操作。 当需要关闭弹窗时,可以使用DialogService的Close方法来关闭弹窗。Close方法接收一个字符串参数来指定要关闭的弹窗的名称,该名称应与弹窗的View名称相对应。 总之,WPF Prism提供了弹窗的管理和控制的机制,通过DialogService可以创建、显示和关闭弹窗,使得应用程序具有更好的用户体验和交互性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无熵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值