Avalonia ShowDialog 主視窗 半透明遮罩 實作

Avalonia ShowDialog 主視窗 半透明遮罩 實作

Avalonia ShowDialog 主視窗 半透明遮罩 實作


資料來源:chatgpt


方案01. 透過主視窗隱藏一個覆蓋全螢幕方式來實現

MainWindow

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="全螢幕主視窗"
        WindowState="Maximized">
  <Grid>
    <!-- 主內容 -->
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
      <Button Content="顯示子視窗" Click="OnShowDialogClick"/>
    </StackPanel>

    <!-- 半透明遮罩 -->
    <Border Name="OverlayMask"
            Background="Black"
            Opacity="0.5"
            IsVisible="False"/>
  </Grid>
</Window>
using Avalonia.Controls;

namespace AvaloniaFullScreenExample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public void ShowOverlayMask(bool show)
        {
            OverlayMask.IsVisible = show;
        }

        private async void OnShowDialogClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
        {
            var dialog = new DialogWindow();
            await dialog.ShowDialog(this);
        }
    }
}

DialogWindow

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="子視窗"
        Width="600" Height="300">
  <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock Text="這是子視窗" Margin="0,0,0,10"/>
    <Button Content="關閉" Click="OnCloseClick"/>
  </StackPanel>
</Window>

using Avalonia.Controls;

namespace AvaloniaFullScreenExample
{
    public partial class DialogWindow : Window
    {
        public DialogWindow()
        {
            InitializeComponent();
        }

        protected override void OnOpened(EventArgs e)
        {
            base.OnOpened(e);

            // 顯示父視窗的遮罩
            if (Owner is MainWindow mainWindow)
            {
                mainWindow.ShowOverlayMask(true);
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // 隱藏父視窗的遮罩
            if (Owner is MainWindow mainWindow)
            {
                mainWindow.ShowOverlayMask(false);
            }
        }
    }
}


方案02. 製作一個半透明Window來實現

OverlayWindow

<Window xmlns="https://github.com/avaloniaui"
        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"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="VPOS_Avalonia.OverlayWindow"
		Background="Gray"
		Opacity="0.3"
        Title="OverlayWindow">
</Window>

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace VPOS_Avalonia;

public partial class OverlayWindow : Window
{
    public OverlayWindow()
    {
        this.WindowState = WindowState.FullScreen;
        //隱藏工具列
        this.ExtendClientAreaToDecorationsHint = true;
        this.ExtendClientAreaTitleBarHeightHint = -1;
        this.SystemDecorations = SystemDecorations.None;
        //---隱藏工具列

        InitializeComponent();
    }
}

ExternalPopup

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using VPOS;

namespace VPOS_Avalonia.Views
{
    public class ExternalPopup
    {
        public static MainWindow mainWindow = null;//0
        public static Payment payment = null;//1
        public static Login login = null;//2
        public static Loading loading = null;//3
        public static InvoiceCarrier invoiceCarrier = null;//4
        public static ClosingHandover closingHandover = null;//7
        public static ExpenseList expenseList = null;//8
        public static OrderInquiry orderInquiry = null;//13
        public static OrderPrint orderPrint = null;//14

        public static async Task ProductSpecification(int index, string StrTitle, List<product_Var> product, bool blnPackaging = false)
        {
            // 創建並顯示遮罩視窗
            OverlayWindow _overlay = new OverlayWindow();

            ProductSpecification ProductSpecificationBuf01 = new ProductSpecification(StrTitle, product, blnPackaging);
            switch (index)
            {
                case 0:
                    _overlay.ShowDialog(mainWindow);
                    await ProductSpecificationBuf01.ShowDialog(mainWindow);
                    break;
                case 1:
                    _overlay.ShowDialog(payment);
                    await ProductSpecificationBuf01.ShowDialog(payment);
                    break;
                case 2:
                    _overlay.ShowDialog(login);
                    await ProductSpecificationBuf01.ShowDialog(login);
                    break;
                case 3:
                    _overlay.ShowDialog(loading);
                    await ProductSpecificationBuf01.ShowDialog(loading);
                    break;
                case 4:
                    _overlay.ShowDialog(invoiceCarrier);
                    await ProductSpecificationBuf01.ShowDialog(invoiceCarrier);
                    break;
                case 7:
                    _overlay.ShowDialog(closingHandover);
                    await ProductSpecificationBuf01.ShowDialog(closingHandover);
                    break;
                case 8:
                    _overlay.ShowDialog(expenseList);
                    await ProductSpecificationBuf01.ShowDialog(expenseList);
                    break;
                case 13:
                    _overlay.ShowDialog(orderInquiry);
                    await ProductSpecificationBuf01.ShowDialog(orderInquiry);
                    break;
                case 14:
                    _overlay.ShowDialog(orderPrint);
                    await ProductSpecificationBuf01.ShowDialog(orderPrint);
                    break;
            }
            _overlay.Close();
        }

        public static async Task InvoiceCarrier(int index)
        {
            // 創建並顯示遮罩視窗
            OverlayWindow _overlay = new OverlayWindow();

            InvoiceCarrier InvoiceCarrier01 = new InvoiceCarrier();
            switch (index)
            {
                case 0:
                    _overlay.ShowDialog(mainWindow);
                    await InvoiceCarrier01.ShowDialog(mainWindow);
                    break;
                case 1:
                    _overlay.ShowDialog(payment);
                    await InvoiceCarrier01.ShowDialog(payment);
                    break;
                case 2:
                    _overlay.ShowDialog(login);
                    await InvoiceCarrier01.ShowDialog(login);
                    break;
                case 3:
                    _overlay.ShowDialog(loading);
                    await InvoiceCarrier01.ShowDialog(loading);
                    break;
                case 4:
                    _overlay.ShowDialog(invoiceCarrier);
                    await InvoiceCarrier01.ShowDialog(invoiceCarrier);
                    break;
                case 7:
                    _overlay.ShowDialog(closingHandover);
                    await InvoiceCarrier01.ShowDialog(closingHandover);
                    break;
                case 8:
                    _overlay.ShowDialog(expenseList);
                    await InvoiceCarrier01.ShowDialog(expenseList);
                    break;
                case 13:
                    _overlay.ShowDialog(orderInquiry);
                    await InvoiceCarrier01.ShowDialog(orderInquiry);
                    break;
                case 14:
                    _overlay.ShowDialog(orderPrint);
                    await InvoiceCarrier01.ShowDialog(orderPrint);
                    break;
            }
            _overlay.Close();
        }
        public static async Task ScanCode(int index, String StrMsg)
        {
            // 創建並顯示遮罩視窗
            OverlayWindow _overlay = new OverlayWindow();

            ScanCode ScanCode01 = new ScanCode(StrMsg);
            switch (index)
            {
                case 0:
                    _overlay.ShowDialog(mainWindow);
                    await ScanCode01.ShowDialog(mainWindow);
                    break;
                case 1:
                    _overlay.ShowDialog(payment);
                    await ScanCode01.ShowDialog(payment);
                    break;
                case 2:
                    _overlay.ShowDialog(login);
                    await ScanCode01.ShowDialog(login);
                    break;
                case 3:
                    _overlay.ShowDialog(loading);
                    await ScanCode01.ShowDialog(loading);
                    break;
                case 4:
                    _overlay.ShowDialog(invoiceCarrier);
                    await ScanCode01.ShowDialog(invoiceCarrier);
                    break;
                case 7:
                    _overlay.ShowDialog(closingHandover);
                    await ScanCode01.ShowDialog(closingHandover);
                    break;
                case 8:
                    _overlay.ShowDialog(expenseList);
                    await ScanCode01.ShowDialog(expenseList);
                    break;
                case 13:
                    _overlay.ShowDialog(orderInquiry);
                    await ScanCode01.ShowDialog(orderInquiry);
                    break;
                case 14:
                    _overlay.ShowDialog(orderPrint);
                    await ScanCode01.ShowDialog(orderPrint);
                    break;
            }
            _overlay.Close();
        }
        public static async Task NumericKeypad(int index, string StrMsg,bool blnMode=true)
        {
            // 創建並顯示遮罩視窗
            OverlayWindow _overlay = new OverlayWindow();

            NumericKeypad NumericKeypad01 = new NumericKeypad(StrMsg, blnMode);
            switch (index)
            {
                case 0:
                    _overlay.ShowDialog(mainWindow);
                    await NumericKeypad01.ShowDialog(mainWindow);
                    break;
                case 1:
                    _overlay.ShowDialog(payment);
                    await NumericKeypad01.ShowDialog(payment);
                    break;
                case 2:
                    _overlay.ShowDialog(login);
                    await NumericKeypad01.ShowDialog(login);
                    break;
                case 3:
                    _overlay.ShowDialog(loading);
                    await NumericKeypad01.ShowDialog(loading);
                    break;
                case 4:
                    _overlay.ShowDialog(invoiceCarrier);
                    await NumericKeypad01.ShowDialog(invoiceCarrier);
                    break;
                case 7:
                    _overlay.ShowDialog(closingHandover);
                    await NumericKeypad01.ShowDialog(closingHandover);
                    break;
                case 8:
                    _overlay.ShowDialog(expenseList);
                    await NumericKeypad01.ShowDialog(expenseList);
                    break;
                case 13:
                    _overlay.ShowDialog(orderInquiry);
                    await NumericKeypad01.ShowDialog(orderInquiry);
                    break;
                case 14:
                    _overlay.ShowDialog(orderPrint);
                    await NumericKeypad01.ShowDialog(orderPrint);
                    break;
            }
            _overlay.Close();
        }
        public static async Task MessageBox(int index, String StrMsg)
        {
            // 創建並顯示遮罩視窗
            OverlayWindow _overlay = new OverlayWindow();
            
            MessageBox MessageBox01 = new MessageBox(StrMsg);
            switch (index)
            {
                case 0:
                    _overlay.ShowDialog(mainWindow);
                    await MessageBox01.ShowDialog(mainWindow);
                    break;
                case 1:
                    _overlay.ShowDialog(payment);
                    await MessageBox01.ShowDialog(payment);
                    break;
                case 2:
                    _overlay.ShowDialog(login);
                    await MessageBox01.ShowDialog(login);
                    break;
                case 3:
                    _overlay.ShowDialog(loading);
                    await MessageBox01.ShowDialog(loading);
                    break;
                case 4:
                    _overlay.ShowDialog(invoiceCarrier);
                    await MessageBox01.ShowDialog(invoiceCarrier);
                    break;
                case 7:
                    _overlay.ShowDialog(closingHandover);
                    await MessageBox01.ShowDialog(closingHandover);
                    break;
                case 8:
                    _overlay.ShowDialog(expenseList);
                    await MessageBox01.ShowDialog(expenseList);
                    break;
                case 13:
                    _overlay.ShowDialog(orderInquiry);
                    await MessageBox01.ShowDialog(orderInquiry);
                    break;
                case 14:
                    _overlay.ShowDialog(orderPrint);
                    await MessageBox01.ShowDialog(orderPrint);
                    break; 
            }
            _overlay.Close();
        }

        public static async Task QuesMessageBox(int index, String StrMsg)
        {
            // 創建並顯示遮罩視窗
            OverlayWindow _overlay = new OverlayWindow();

            QuesMessageBox QuesMessageBox01 = new QuesMessageBox(StrMsg);
            switch (index)
            {
                case 0:
                    _overlay.ShowDialog(mainWindow);
                    await QuesMessageBox01.ShowDialog(mainWindow);
                    break;
                case 1:
                    _overlay.ShowDialog(payment);
                    await QuesMessageBox01.ShowDialog(payment);
                    break;
                case 2:
                    _overlay.ShowDialog(login);
                    await QuesMessageBox01.ShowDialog(login);
                    break;
                case 3:
                    _overlay.ShowDialog(loading);
                    await QuesMessageBox01.ShowDialog(loading);
                    break;
                case 4:
                    _overlay.ShowDialog(invoiceCarrier);
                    await QuesMessageBox01.ShowDialog(invoiceCarrier);
                    break;
                case 7:
                    _overlay.ShowDialog(closingHandover);
                    await QuesMessageBox01.ShowDialog(closingHandover);
                    break;
                case 8:
                    _overlay.ShowDialog(expenseList);
                    await QuesMessageBox01.ShowDialog(expenseList);
                    break; 
                case 13:
                    _overlay.ShowDialog(orderInquiry);
                    await QuesMessageBox01.ShowDialog(orderInquiry);
                    break;
                case 14:
                    _overlay.ShowDialog(orderPrint);
                    await QuesMessageBox01.ShowDialog(orderPrint);
                    break; 
            }
            _overlay.Close();
        }

        public static async Task WaitMessageBox(int index, WaitMessageBox.ThreadFun Fun, String StrMsg)
        {
            // 創建並顯示遮罩視窗
            OverlayWindow _overlay = new OverlayWindow();

            WaitMessageBox WaitMessageBox01 = new WaitMessageBox(Fun, StrMsg);
            switch (index)
            {
                case 0:
                    _overlay.ShowDialog(mainWindow);
                    await WaitMessageBox01.ShowDialog(mainWindow);
                    break;
                case 1:
                    _overlay.ShowDialog(payment);
                    await WaitMessageBox01.ShowDialog(payment);
                    break;
                case 2:
                    _overlay.ShowDialog(login);
                    await WaitMessageBox01.ShowDialog(login);
                    break;
                case 3:
                    _overlay.ShowDialog(loading);
                    await WaitMessageBox01.ShowDialog(loading);
                    break;
                case 4:
                    _overlay.ShowDialog(invoiceCarrier);
                    await WaitMessageBox01.ShowDialog(invoiceCarrier);
                    break;
                case 7:
                    _overlay.ShowDialog(closingHandover);
                    await WaitMessageBox01.ShowDialog(closingHandover);
                    break;
                case 8:
                    _overlay.ShowDialog(expenseList);
                    await WaitMessageBox01.ShowDialog(expenseList);
                    break; 
                case 13:
                    _overlay.ShowDialog(orderInquiry);
                    await WaitMessageBox01.ShowDialog(orderInquiry);
                    break;
                case 14:
                    _overlay.ShowDialog(orderPrint);
                    await WaitMessageBox01.ShowDialog(orderPrint);
                    break; 
            }
            _overlay.Close();
        }
    }
}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *