C# WINDOWS MAUI專案 抓取螢幕解析度 & 指定Popup顯示位置[置中/左側(0,0)]
C# WINDOWS MAUI專案 抓取螢幕解析度 & 指定Popup顯示位置[置中/左側(0,0)]
GITHUB: https://github.com/jash-git/MAUI_WinAPI_Object_test/tree/main/Code/16
MainPage.xaml code
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MAUI_WinAPI_Object_test.CustomControls" xmlns:drawable="clr-namespace:MAUI_WinAPI_Object_test" x:Class="MAUI_WinAPI_Object_test.MainPage" Title=""> <ContentPage.Resources> <drawable:GraphicsDrawable x:Key="drawable01" /> </ContentPage.Resources> <ScrollView> <VerticalStackLayout Spacing="25" Padding="30,0" VerticalOptions="Center"> <GraphicsView Drawable="{StaticResource drawable01}" HeightRequest="40" WidthRequest="30" /> <local:CustmImageButton HorizontalOptions="Center"/> <Image Source="dotnet_bot.png" SemanticProperties.Description="Cute dot net bot waving hi to you!" HeightRequest="200" /> <Label x:Name="labtime" SemanticProperties.HeadingLevel="Level1" FontSize="32" HorizontalOptions="Center" /> <Label x:Name="labDisplayInfo" Text="Welcome to .NET Multi-platform App UI" SemanticProperties.HeadingLevel="Level2" SemanticProperties.Description="Welcome to dot net Multi platform App U I" FontSize="18" HorizontalOptions="Center" /> <Button x:Name="CounterBtn" Text="Click me" SemanticProperties.Hint="Counts the number of times you click" Clicked="OnCounterClicked" HorizontalOptions="Center" /> <local:CustomButton x:Name="PopupBtn" Text="Show Popup" CustomProperty="這是自訂屬性的值" Clicked="PopupBtn_Clicked" HorizontalOptions="Center" /> <local:CardControl Title="Hello CardControl"/> <Button x:Name="CloseBtn" Text="Close" Clicked="CloseBtn_Clicked" HorizontalOptions="Center" /> </VerticalStackLayout> </ScrollView> </ContentPage>
MainPage.xaml.cs code [抓取解析度]
using CommunityToolkit.Maui.Views; using System.Threading; using MAUI_WinAPI_Object_test.Views; using MAUI_WinAPI_Object_test.CustomControls; namespace MAUI_WinAPI_Object_test { public partial class MainPage : ContentPage { public int count = 0; public IDispatcherTimer timmer { get; set; } public Thread thread; public bool blnStop = false; public MainPage() { InitializeComponent(); //* //--- //Timer Mode //https://learn.microsoft.com/en-us/answers/questions/1207012/how-to-stop-device-starttimer timmer = Application.Current.Dispatcher.CreateTimer(); timmer.Interval = new TimeSpan(0, 0, 0, 0, 100);//天/時/分/秒/毫秒 timmer.Tick += Timmer_Tick; timmer.IsRepeating = true;//the timer will keep recurring, you can set false timmer.Start(); //---Timer Mode //*/ // 在新執行緒中執行工作 /* thread = new Thread(ThreadFun); thread.Start(); */ } private void Timmer_Tick(object sender, EventArgs e) { //--- //抓螢幕解析度 var displayInfo = DeviceDisplay.MainDisplayInfo; double width = displayInfo.Width; double height = displayInfo.Height; double density = displayInfo.Density; //---抓螢幕解析度 labDisplayInfo.Text = $"{width} x {height} ; {density}"; labtime.Text = DateTime.Now.ToString("HH:mm:ss"); } [Obsolete] private void ThreadFun() { do { // 更新使用者介面 (UI) 元素 Device.BeginInvokeOnMainThread(() => { // 在這裡更新 UI 控制項 labtime.Text = DateTime.Now.ToString("HH:mm:ss"); }); Thread.Sleep(1000); } while (!blnStop); } private void OnCounterClicked(object sender, EventArgs e) { count++; if (count == 1) CounterBtn.Text = $"Clicked {count} time"; else CounterBtn.Text = $"Clicked {count} times"; SemanticScreenReader.Announce(CounterBtn.Text); } private void CloseBtn_Clicked(object sender, EventArgs e) { if (timmer != null) { timmer.Stop(); } if (thread != null) { blnStop = true; thread = null; } // Close the active window //https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/windows Application.Current.CloseWindow(GetParentWindow()); } private async void PopupBtn_Clicked(object sender, EventArgs e) { //await DisplayAlert("Alert", $"路徑:{AppDomain.CurrentDomain.BaseDirectory}", "OK");//await labtime.IsVisible = !labtime.IsVisible;//元件隱藏/顯示 await this.ShowPopupAsync(new PopupPage1());//this.ShowPopup(new PopupPage1()); await this.ShowPopupAsync(new GridPage()); await DisplayAlert("Alert", $"{PopupBtn.CustomProperty};{PopupPage1.m_StrResult}", "OK");//await } } }
GridPage.xaml code [Start,Start -> 左側(0,0)顯示]
<?xml version="1.0" encoding="utf-8" ?> <mct:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:mct="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui" x:Class="MAUI_WinAPI_Object_test.Views.GridPage" HorizontalOptions="Start" VerticalOptions="Start" CanBeDismissedByTappingOutsideOfPopup="True"> <Grid x:Name="FullGrid" Margin="3" VerticalOptions="Fill" HorizontalOptions="Fill" /> </mct:Popup>
GridPage.xaml.cs code
using CommunityToolkit.Maui.Views; using Jint.Parser; namespace MAUI_WinAPI_Object_test.Views; public partial class GridPage : Popup //: ContentPage { public GridPage() { InitializeComponent(); // 創建一個 Grid 佈局 var grid = FullGrid; grid.BackgroundColor = Colors.AliceBlue; // 定義列和行 grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); // 在網格中添加控制項 var label1 = new Label { Text = "第一行,第一列" }; var label2 = new Label { Text = "第二行,第一列" }; var label3 = new Label { Text = "第一行,第二列" }; var label4 = new Label { Text = "第二行,第二列" }; grid.Add(label1, 0, 0); grid.Add(label2, 1, 0); grid.Add(label3, 0, 1); grid.Add(label4, 1, 1); } }
PopupPage1.xaml code [Center,Center -> 置中顯示]
<?xml version="1.0" encoding="utf-8" ?> <mct:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:mct="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui" x:Class="MAUI_WinAPI_Object_test.Views.PopupPage1" CanBeDismissedByTappingOutsideOfPopup="False"> <VerticalStackLayout BackgroundColor="Blue"> <Label Text="Welcome to .NET MAUI!" VerticalOptions="Center" HorizontalOptions="Center" /> <Button x:Name="CloseBtn" Text="Close App" Clicked="CloseBtn_Clicked" HorizontalOptions="Center" ClassId="" /> </VerticalStackLayout> </mct:Popup>
PopupPage1.xaml.cs code
using CommunityToolkit.Maui.Views; namespace MAUI_WinAPI_Object_test.Views; public partial class PopupPage1 : Popup//: ContentPage { public static String m_StrResult; public PopupPage1() { InitializeComponent(); m_StrResult = ""; //Size = new Size(500, 500);//³]©wUI¤j¤p } private void CloseBtn_Clicked(object sender, EventArgs e) { m_StrResult = "CloseBtn_Clicked"; Close(); } }