C# WINDOWS MAUI專案 Popup & Thread整合應用
C# WINDOWS MAUI專案 Popup & Thread整合應用
GITHUB: https://github.com/jash-git/MAUI_WinAPI_Object_test/tree/main/Code/14
test_PopupPage.xaml code:
<?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="VPOS.Views.test_PopupPage" CanBeDismissedByTappingOutsideOfPopup="False" Color="#0BE3FA"> <VerticalStackLayout BackgroundColor="Transparent" VerticalOptions="Center" HorizontalOptions="Center"> <Label x:Name="labtime" Text="test_PopupPage" VerticalOptions="Center" HorizontalOptions="Center" /> <ActivityIndicator Color="{StaticResource Primary}" IsRunning="True" HeightRequest="100" WidthRequest="100" IsVisible="True" /> <Button x:Name="CloseBtn" Text="Close App" Clicked="CloseBtn_Clicked" HorizontalOptions="Center" ClassId="" /> </VerticalStackLayout> </mct:Popup>
test_PopupPage.xaml.cs code:
using CommunityToolkit.Maui.Views; namespace VPOS.Views; public partial class test_PopupPage : Popup//: ContentPage { public static String m_StrResult; public Thread thread; public bool blnStop = false; private void ThreadStopClear() { if (thread != null) { blnStop = true; thread = null; } } [Obsolete] private void ThreadFun() { int intCount = 0; do { // 更新使用者介面 (UI) 元素 Device.BeginInvokeOnMainThread(() => { // 在這裡更新 UI 控制項 labtime.Text = DateTime.Now.ToString("HH:mm:ss"); }); intCount++; Thread.Sleep(1000); if(intCount>=3) { blnStop = true; } } while (!blnStop); m_StrResult = "CloseBtn_Clicked"; ThreadStopClear(); // 更新使用者介面 (UI) 元素 Device.BeginInvokeOnMainThread(() => { // 在這裡更新 UI 控制項 Close(); }); } public test_PopupPage() { InitializeComponent(); m_StrResult = ""; //Size = new Size(500, 500);//設定UI大小 //--- //建立執行序 thread = new Thread(ThreadFun); thread.Start(); //---建立執行序 } private void CloseBtn_Clicked(object sender, EventArgs e) { m_StrResult = "CloseBtn_Clicked"; ThreadStopClear(); Close(); } }