首页 > 其他分享 >WINUI 模态框或模态窗口

WINUI 模态框或模态窗口

时间:2023-07-21 11:24:05浏览次数:37  
标签:模态 ModalDialogHost Popup 窗口 string get WINUI set public

WINUI中是没有类似Winform里的模态框的,为了实现同样的需求,小子借助于popup进行了相应的实现。

 

思路:自定义控件实现一个窗体,进行信息展示与信息选择;这个窗体作为弹出窗口的展示页面;

在页面上进行相应的选择进行什么样的操作,则通过通过委托在实例化这个窗口时传递相应的参数,选择后执行相应的委托即可。

 

 

<?xml version="1.0" encoding="utf-8"?>
<UserControl
    x:Class="Jedi.JV.WinUI.Controls.ModalDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Jedi.JV.WinUI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid  HorizontalAlignment="Center" VerticalAlignment="Center">
        <Border Background="#000" Opacity="0.2" Width="2560" Height="1440" />
        <Grid  Width="700" Height="400" Background="AliceBlue"  CornerRadius="12" Margin="530 0 930 150" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Grid.RowDefinitions>
                    <RowDefinition  Height="70"/>
                    <RowDefinition  Height="*"/>
                    <RowDefinition  Height="100"/>
                </Grid.RowDefinitions>
                <Grid  Grid.Row="0" Background="#546EEF" >
                    <TextBlock   Text="{x:Bind Title,Mode=TwoWay}" HorizontalAlignment="Left"  Margin="10" Foreground="White"  FontSize="30"/>
                    <Button HorizontalAlignment="Right"  Width="44"
                       Height="44"
                       Margin="0,0,26,0"
                       Background="#768BF2" Click="BtnClose_Click">
                        <Button.Content>
                            <Image
                               Width="20"
                               Height="20"
                               Source="../../Images/关闭窗口@2x.png"
                               Stretch="Uniform"  />
                        </Button.Content>
                    </Button>
                </Grid>
                <StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"  Orientation="Horizontal">
                    <Image
                      Grid.Column="1"
                      Width="74"
                      Height="66"
                      Source="../../Images/报错提示@2x.png"
                      Stretch="Uniform" />
                    <TextBlock  Margin="10" Text="{x:Bind Message,Mode=TwoWay}" FontSize="38" />
                </StackPanel>
                <StackPanel Grid.Row="2" Orientation="Horizontal"  HorizontalAlignment="Center">
                    <Button  Content="取消" x:Name="BtnCancel" Click="BtnClose_Click" Background="#546EEF" Width="90" Height="80" Foreground="White" FontSize="36" Margin="10"/>
                    <Button  Content="确认" x:Name="BtnComfirm" Click="BtnComfirm_Click" Background="#546EEF" Width="90" Height="80" Foreground="White" FontSize="36" Margin="10"/>
                </StackPanel>
            </Grid>

    </Grid>

</UserControl>

 

窗体的相应后台代码:

public sealed partial class ModalDialog : UserControl
{
    public ModalDialog()
    {
        this.InitializeComponent();
    }
    public Action<int> Action  { get; set; }
    public int id { get; set; }
    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("Message", typeof(string), typeof(ModalDialog), new PropertyMetadata(string.Empty));


    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("MeTitlessage", typeof(string), typeof(ModalDialog), new PropertyMetadata(string.Empty));


    public bool Result { get; private set; }
    private void BtnClose_Click(object sender, RoutedEventArgs e)
    {
        ModalDialogHost.Instance.CloseModal();
        Result = false;
     
    }

    private void BtnComfirm_Click(object sender, RoutedEventArgs e)
    {
        Result = true;
        ModalDialogHost.Instance.CloseModal();
        Task.Run(() =>
        {
            Action?.Invoke(id);
        });
        
    }

}

 

 

Popup逻辑:

internal class ModalDialogHost
{
    private ModalDialogHost()
    {
            
    }
    public static ModalDialogHost Instance
    {
        get
        {
            return Nested.instance;
        }
    }
    class Nested
    {
        static Nested() { }
        internal static readonly ModalDialogHost instance = new();
    }

    public Popup Popup { get; private set; }

    public void ShowModal(UIElement content, Window window)
    {
        if (Popup == null)
        {
            Popup = new Popup();
        }
        Popup.Child = content;

        if (window != null)
        {
            if (window.Content is Panel Conatiner)
            {
                if (Conatiner.Children.Contains(Popup))
                {
                    Popup.IsOpen = true;
                }
                else
                {
                    Conatiner.Children.Add(Popup);
                    Popup.IsOpen = true;
                }
            }
        }

    }

    public void CloseModal()
    {
        Popup.IsOpen = false;
    }
}

 

标签:模态,ModalDialogHost,Popup,窗口,string,get,WINUI,set,public
From: https://www.cnblogs.com/chengcanghai/p/17570772.html

相关文章

  • WINUI 后台代码绑定
    以image为例 前端进行绑定时哪下,注意下述代码中用的是x:Bind,用它进行绑定时需要标明其绑定ViewModel的key值;用Bingding时则不需要。<Imagex:Name="CTCoronalCImage"Width="1010"Height="442"HorizontalAlignment="Stretch"VerticalAlignm......
  • python最大化窗口命令
    Python最大化窗口命令在使用Python编写图形界面应用程序时,经常需要控制窗口的大小和位置。对于某些情况,我们可能需要最大化窗口以提供更好的用户体验。本文将介绍如何使用Python实现最大化窗口的命令。什么是最大化窗口最大化窗口是指将窗口的大小调整为屏幕的最大尺寸,以充分利......
  • python自动化基于Excel的关键字驱动类如何使窗口最大化
    Python自动化基于Excel的关键字驱动类如何使窗口最大化在编写基于Excel的关键字驱动脚本时,有时需要使窗口最大化以确保脚本的稳定性和可靠性。本文将介绍如何使用Python自动化基于Excel的关键字驱动类来实现窗口最大化。1.安装所需的库在开始之前,我们需要确保已经安装了所需的......
  • WPF调试软件窗口顶部工具栏的开起与关闭
    WPF调试软件窗口顶部工具栏的开起与关闭应用内工具栏可以实现元素选取,XAML热重载,绑定异常检测等功能工具栏->选项......
  • python隐藏窗口
    Python隐藏窗口在使用Python编写程序时,有时我们希望在程序运行时隐藏窗口,以便不会打扰用户或在后台运行。本文将介绍如何使用Python隐藏窗口,并提供相关的代码示例。为什么隐藏窗口?隐藏窗口的主要目的是不打扰用户或在后台运行。有时,我们编写的程序只需要完成特定的任务,而不需要......
  • Windows Intelnet 属性中的隐私弹出窗口阻止程序设置设置为高级别
    要通过批处理将WindowsIntelnet属性中的隐私弹出窗口阻止程序设置设置为高级别,你可以使用以下命令:REGADD"HKCU\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Zones\3"/v1406/tREG_DWORD/d3/f这个命令会将注册表键值1406设置为3,表示阻止级别设置......
  • java怎么设置窗口大小
    Java怎么设置窗口大小在Java中设置窗口大小可以通过设置窗口的宽度和高度来实现。下面将介绍两种常见的方法来设置窗口大小。方法一:使用setSize方法设置窗口大小使用setSize方法可以直接设置窗口的宽度和高度。以下是一个示例代码:importjavax.swing.JFrame;publicclassWin......
  • 仿微信聊天程序 - 08. 聊天窗口
    本文是仿微信聊天程序专栏的第八篇文章,主要记录了【聊天窗口】的界面实现。界面设计聊天窗口是整个聊天程序的核心控件,比较复杂,大致可以分为上中下三个部分,上面显示用户昵称以及一些操作菜单,中间是聊天内容显示区域,下面的信息发送的区域,总体界面设计如下:界面布局根据界面设计......
  • dwm.exe 是 Windows 操作系统中的一个进程,它代表桌面窗口管理器 (Desktop Window Mana
    dwm.exe是Windows操作系统中的一个进程,它代表桌面窗口管理器(DesktopWindowManager)。桌面窗口管理器是Windows中负责处理图形渲染和用户界面效果的组件之一。具体来说,dwm.exe负责管理桌面环境的显示、窗口合成、窗口动画、透明效果等任务。它使用硬件加速技术来提供流......
  • jna获取windos所有窗口
    <dependency><groupId>net.java.dev.jna</groupId><artifactId>jna</artifactId><version>5.6.0</version></dependency><dependency>......