Avalonia Popup 範例(自製彈出視窗整併成元件基礎範例)
Avalonia Popup 範例(自製彈出視窗整併成元件基礎範例)
資料來源: copilot+doubao+gemini+自己
GITHUB: https://github.com/jash-git/AvaloniaUI_CS_test
XMAL
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:AvaloniaMVVM.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="100%" d:DesignHeight="100%"
x:Class="AvaloniaMVVM.Views.MainWindow"
x:DataType="vm:MainWindowViewModel"
Icon="avares://AvaloniaMVVM/Assets/avalonia-logo.ico">
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainWindowViewModel/>
</Design.DataContext>
<StackPanel Orientation="Vertical" Spacing="5" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock x:Name="GreetingTextBlock" FontSize="30" FontFamily="Arial" FontWeight="Bold" FontStyle="Italic" Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock x:Name="ResolutionTextBlock" FontSize="24"/>
<Button x:Name="BtnSet" Foreground="Green" BorderBrush="Red" Background="AliceBlue" Content="Click Me!" FontSize="24" Click="BtnSet_Click" />
<!-- 彈出層 -->
<Popup Name="MyPopup" PlacementMode="Pointer" >
<Border Background="LightGray" Padding="10" Width="200" Height="150">
<StackPanel>
<TextBlock Text="This is a popup!" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Button Content="Close" Click="ClosePopup_Click" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0"/>
</StackPanel>
</Border>
</Popup>
<Image x:Name="img01" Margin="20" Height="50" Width="50" Source="avares://AvaloniaMVVM/Assets/user.png"/>
<!-- <ListBox x:Name="animals"/> -->
<Border Background="AliceBlue" Width="300" Height="400">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ListBox x:Name="listbox01" ItemsSource="{Binding Items}" Background="White">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Spacing="10">
<TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
<CheckBox IsChecked="{Binding IsCompleted}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Border>
</StackPanel>
</Window>
CODE
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
using System.Text;
using Avalonia.Media;
using System;
using System.Threading;
using System.Timers;
using AvaloniaMVVM.ViewModels;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using static System.Net.Mime.MediaTypeNames;
using Avalonia.Controls.Primitives;
namespace AvaloniaMVVM.Views
{
public partial class MainWindow : Window
{
//---
//ViewModels
public MainWindowViewModel m_MainWindowViewModel = null;
//---ViewModels
//---
//Thread
private Thread m_timerThread;
private bool m_running;
private void InitializeThread()
{
m_running = true;
m_timerThread = new Thread(ThreadFunction);
m_timerThread.Start();
}
private void ThreadStop()
{
m_running = false;
m_timerThread?.Join();
}
private void ThreadFunction()
{
while (m_running)
{
Thread.Sleep(2000);
Dispatcher.UIThread.InvokeAsync(() =>
{
GreetingTextBlock.Text = DateTime.Now.ToString("HH:mm:ss");
});
}
}
//---Thread
//---
//Timer
private bool m_blnMode = true;
private System.Timers.Timer m_timer;
private void InitializeTimer()
{
/*
m_timer.Start();
m_timer.Stop();
//*/
//*
m_timer = new System.Timers.Timer(1000); // 每秒觸發一次
m_timer.Elapsed += OnTimedEvent;
m_timer.AutoReset = true;
m_timer.Start();
//*/
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
if(m_blnMode)
{
m_MainWindowViewModel.Items.Insert(0, new Item(DateTime.Now.ToString("HH:mm:ss"), false));
}
else
{
if(m_MainWindowViewModel.Items.Count>0)
{
m_MainWindowViewModel.Items.RemoveAt(0);
}
}
Dispatcher.UIThread.InvokeAsync(() =>
{
listbox01.SelectedIndex = (0);
GreetingTextBlock.Text = DateTime.Now.ToString("HH:mm:ss");
});
}
//---Timer
public MainWindow()
{
//---
//從APP
m_MainWindowViewModel = new MainWindowViewModel();
DataContext = m_MainWindowViewModel;
//---
InitializeComponent();
FullScreenMode();
SpecifyScreen();
ShowScreenResolution();
//animals.ItemsSource = new string[]{"cat", "camel", "cow", "chameleon", "mouse", "lion", "zebra" }.OrderBy(x => x);
img01.PointerPressed += BtnSet_Click;
InitializeTimer();//InitializeThread();//
}
private void SpecifyScreen(int intIndx=1)
{
if ((intIndx - 1) < 0)
return;
// 獲取所有螢幕
var allScreens = Screens.All;
// 選擇第二個螢幕 (若有)
if (allScreens.Count > (intIndx-1))
{
var targetScreen = allScreens[(intIndx - 1)];
Position = new PixelPoint(targetScreen.WorkingArea.X, targetScreen.WorkingArea.Y);
}
}
private void FullScreenMode()
{
this.WindowState = WindowState.FullScreen;//WindowState.Maximized;
//隱藏工具列
this.ExtendClientAreaToDecorationsHint = true;
this.ExtendClientAreaTitleBarHeightHint = -1;
//---隱藏工具列
this.SystemDecorations = SystemDecorations.None;
}
private void ShowScreenResolution()
{
StringBuilder resolutionInfo = new StringBuilder();
/*
var screens = Screens.Primary.WorkingArea;
var screenWidth = screens.Width;
var screenHeight = screens.Height;
resolutionInfo.AppendLine($"Primary Screen: {screenWidth} x {screenHeight}");
*/
int i = 0;
foreach (var screen in Screens.All)
{
i++;
resolutionInfo.AppendLine($"Screen {i}: {screen.Bounds.Width} x {screen.Bounds.Height}");
}
Dispatcher.UIThread.InvokeAsync(() =>
{
ResolutionTextBlock.Text = resolutionInfo.ToString();
});
}
private async void BtnSet_Click(object sender, RoutedEventArgs e)
{
ResolutionTextBlock.FontWeight = FontWeight.Bold;
BtnSet.Content = "FontWeight.Bold";
//---
//程式切換圖片語法
var uri = new Uri("avares://AvaloniaMVVM/Assets/avalonia-logo.ico", UriKind.RelativeOrAbsolute);
using (var stream = AssetLoader.Open(uri))
{
img01.Source = new Bitmap(stream);
}
//---程式切換圖片語法
m_blnMode = false;//m_timer.Stop();//ThreadStop();//
//---
//簡易對話盒
var messageBox = new Window
{
Title = "Message Box",
Width = 300,
Height = 200,
Content = new StackPanel
{
Children =
{
new TextBlock { Text = "Hello, this is a custom message box!", Margin = new Thickness(10) },
new Button { Content = "OK", Margin = new Thickness(10) }
}
}
};
await messageBox.ShowDialog(this);
//---簡易對話盒
MyPopup.IsOpen = true;
}
private void ClosePopup_Click(object sender, RoutedEventArgs e)
{
MyPopup.IsOpen = false;
}
}
}