Avalonia 自訂義通用對話盒UI (MessageBox/WaitMessageBox/QuesMessageBox)範例 ~ Avalonia 分隔線UI(Separator)
Avalonia 自訂義通用對話盒UI (MessageBox/WaitMessageBox/QuesMessageBox)範例 ~ Avalonia 分隔線UI(Separator)
資料來源: 自己
MessageBox.axaml
<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" xmlns:local="clr-namespace:VPOS_Avalonia" x:Class="VPOS_Avalonia.MessageBox" Title="MessageBox"> <Border BorderBrush="LightBlue" BorderThickness="5" CornerRadius="10" Margin="-3"> <Grid RowDefinitions="*,10,3*" ColumnDefinitions="*" > <TextBlock Text="VTEAM MSG" Grid.Row="0" Foreground="Blue" FontSize="32" HorizontalAlignment="Center" VerticalAlignment="Center" /> <Separator Grid.Row="1" Margin="2" Background="White" Height="2"/> <Grid Grid.Row="2" RowDefinitions="2*,*" ColumnDefinitions="2.5*,2*,2.5*" > <TextBlock x:Name="Msg" TextWrapping="Wrap" Grid.Row="0" Foreground="White" Grid.ColumnSpan="5" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center"/> <local:BaseButton x:Name="Close" Grid.Row="1" Grid.Column="1" TextSize="24" Text="確定" TextColor="White" BackgroundColor="#ff00a1e0" BorderColor="White" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="30,5,30,5" /> </Grid> </Grid> </Border> </Window>
MessageBox.axaml.axaml.cs
using Avalonia; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia.Media; using Avalonia.Platform; using Avalonia.Styling; using System; namespace VPOS_Avalonia; public partial class MessageBox : Window { public MessageBox(String StrMsg) { //隱藏工具列 this.ExtendClientAreaToDecorationsHint = true; this.ExtendClientAreaTitleBarHeightHint = -1; this.SystemDecorations = SystemDecorations.None; //---隱藏工具列 WindowStartupLocation = WindowStartupLocation.CenterScreen;//視窗置中 this.Background = new SolidColorBrush(Color.Parse("#ff194a6e"));//設定背景色 InitializeComponent(); //--- //視窗尺寸 Width = 600; Height = 300; //---視窗尺寸 Msg.Text = StrMsg;//指定訊息 //Close.BorderColor = new SolidColorBrush(Color.Parse("#FF0000")); Close.ExternalClicked += Close_Clicked; } private void Close_Clicked(object sender, RoutedEventArgs e) { /* //--- //自訂元件內子元件屬性變更方法 var userControl = (BaseButton)sender; var textBlock = userControl.FindControl<TextBlock>("TB_Base"); textBlock.Text = "Clicked from outside!"; //---自訂元件內子元件屬性變更方法 */ this.Close(); } }
WaitMessageBox.axaml
<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.WaitMessageBox" Title="WaitMessageBox"> <Border BorderBrush="LightBlue" BorderThickness="5" CornerRadius="10" Margin="-3"> <TextBlock x:Name="Msg" TextWrapping="Wrap" Grid.Row="0" Foreground="White" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </Window>
WaitMessageBox.axaml.axaml.cs
using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; using Avalonia.Media; using Avalonia.Threading; using System; using System.Threading; using System.Timers; using VPOS_Avalonia.ViewModels; using static VPOS_Avalonia.WaitMessageBox; using Timer = System.Timers.Timer; namespace VPOS_Avalonia; public partial class WaitMessageBox : Window { public delegate void ThreadFun(); private ThreadFun m_ThreadFun; private Thread m_Thread; private Timer m_Timer; private int m_intTimerCount; public WaitMessageBox(ThreadFun Fun, String StrMsg) { //隱藏工具列 this.ExtendClientAreaToDecorationsHint = true; this.ExtendClientAreaTitleBarHeightHint = -1; this.SystemDecorations = SystemDecorations.None; //---隱藏工具列 WindowStartupLocation = WindowStartupLocation.CenterScreen;//視窗置中 this.Background = new SolidColorBrush(Color.Parse("#ff194a6e"));//設定背景色 InitializeComponent(); //--- //視窗尺寸 Width = 600; Height = 150; //---視窗尺寸 Msg.Text = StrMsg;//指定訊息 m_ThreadFun = Fun; InitializeTimer(); //--- //建立執行序 m_Thread = new Thread(MainThreadFun); m_Thread.Start(); //---建立執行序 } private void InitializeTimer() { /* m_timer.Start(); m_timer.Stop(); //*/ //* m_intTimerCount = 0; m_Timer = new Timer(1000); // 每秒觸發一次 m_Timer.Elapsed += OnTimedEvent; m_Timer.AutoReset = true; m_Timer.Start(); //*/ } private void OnTimedEvent(object sender, ElapsedEventArgs e) { m_intTimerCount++; Dispatcher.UIThread.InvokeAsync(() => { if (m_intTimerCount <= 3) { Msg.Text += "."; } else { m_intTimerCount = 0; Msg.Text = Msg.Text.Substring(0, Msg.Text.Length - 3); } }); } public void DeleteTimmer() { m_Timer.Stop(); m_Timer = null; } private void MainThreadFun() { m_ThreadFun(); Thread.Sleep(10); DeleteTimmer(); Dispatcher.UIThread.InvokeAsync(() => { Close(); }); if (m_Thread != null) { m_Thread = null; } } }
QuesMessageBox.axaml
<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" xmlns:local="clr-namespace:VPOS_Avalonia" x:Class="VPOS_Avalonia.QuesMessageBox" Title="QuesMessageBox"> <Border BorderBrush="LightBlue" BorderThickness="5" CornerRadius="10" Margin="-3"> <Grid RowDefinitions="*,10,3*" ColumnDefinitions="*" > <TextBlock Text="VTEAM MSG" Grid.Row="0" Foreground="Blue" FontSize="32" HorizontalAlignment="Center" VerticalAlignment="Center" /> <Separator Grid.Row="1" Margin="2" Background="White" Height="2"/> <Grid Grid.Row="2" RowDefinitions="2*,*" ColumnDefinitions="1.3*,2*,0.4*,2*,1.3*" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <TextBlock x:Name="Msg" TextWrapping="Wrap" Grid.Row="0" Foreground="White" Grid.ColumnSpan="5" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center"/> <local:BaseButton x:Name="BtnYes" Grid.Row="1" Grid.Column="1" TextSize="24" Text="確定" TextColor="White" BackgroundColor="#ff00a1e0" BorderColor="White" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="30,5,30,5" /> <local:BaseButton x:Name="BtnNo" Grid.Row="1" Grid.Column="3" TextSize="24" Text="取消" TextColor="White" BackgroundColor="#fffd5f00" BorderColor="White" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="30,5,30,5" /> </Grid> </Grid> </Border> </Window>
QuesMessageBox.axaml.cs
using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; using Avalonia.Media; using System; namespace VPOS_Avalonia; public partial class QuesMessageBox : Window { public static bool m_blnResult = false; public QuesMessageBox(String StrMsg) { //隱藏工具列 this.ExtendClientAreaToDecorationsHint = true; this.ExtendClientAreaTitleBarHeightHint = -1; this.SystemDecorations = SystemDecorations.None; //---隱藏工具列 WindowStartupLocation = WindowStartupLocation.CenterScreen;//視窗置中 this.Background = new SolidColorBrush(Color.Parse("#ff194a6e"));//設定背景色 InitializeComponent(); //--- //視窗尺寸 Width = 600; Height = 300; //---視窗尺寸 Msg.Text = StrMsg;//指定訊息 m_blnResult = false; BtnYes.ExternalClicked += BtnYes_Clicked; BtnNo.ExternalClicked += BtnNo_Clicked; } private void BtnYes_Clicked(object sender, EventArgs e) { m_blnResult = true; Close(); } private void BtnNo_Clicked(object sender, EventArgs e) { m_blnResult = false; Close(); } }