C# WINDOWS MAUI專案 使用 ContentView(Border+Label)變成自訂按鈕(CustBtn) 利用 XML & Code 方式指定事件函數(event function)
C# WINDOWS MAUI專案 使用 ContentView(Border+Label)變成自訂按鈕(CustBtn) 利用 XML & Code 方式指定事件函數(event function)
GITHUB: https://github.com/jash-git/MAUI_WinAPI_Object_test/tree/main/Code/20
CustBtn.xaml code
<?xml version="1.0" encoding="utf-8" ?> <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauCustBtn.CustBtn"> <Border Stroke="#C49B33" StrokeThickness="4" Background="#2B0B98" Padding="16,8" HorizontalOptions="Center"> <Border.StrokeShape> <RoundRectangle CornerRadius="40,0,0,40" /> </Border.StrokeShape> <Label Text=".NET MAUI" TextColor="White" FontSize="18" FontAttributes="Bold" /> </Border> </ContentView>
CustBtn.xaml.cs code
namespace MauCustBtn; public partial class CustBtn : ContentView { public int m_intSID; public CustBtn() { InitializeComponent(); } }
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:MauCustBtn" x:Class="MauCustBtn.MainPage"> <ScrollView> <VerticalStackLayout x:Name="VS01" Spacing="25" Padding="30,0" VerticalOptions="Center"> <Image Source="dotnet_bot.png" SemanticProperties.Description="Cute dot net bot waving hi to you!" HeightRequest="200" HorizontalOptions="Center" /> <Label Text="Hello, World!" SemanticProperties.HeadingLevel="Level1" FontSize="32" HorizontalOptions="Center"/> <local:CustBtn> <ContentView.GestureRecognizers> <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" /><!--ContentView XML 事件指定--> </ContentView.GestureRecognizers> </local:CustBtn> <Button x:Name="CounterBtn" Text="Click me" SemanticProperties.Hint="Counts the number of times you click" Clicked="OnCounterClicked" HorizontalOptions="Center" /> </VerticalStackLayout> </ScrollView> </ContentPage>
MainPage.xaml.cs code
namespace MauCustBtn; public partial class MainPage : ContentPage { int count = 0; public MainPage() { InitializeComponent(); CustBtn custBtn = new CustBtn(); custBtn.m_intSID = 100; // 创建一个 TapGestureRecognizer var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += TapGestureRecognizer_Tapped1; custBtn.GestureRecognizers.Add(tapGestureRecognizer); VS01.Add(custBtn); } private void TapGestureRecognizer_Tapped1(object sender, EventArgs e) { count--; if(count <0) { count= ((CustBtn)(sender)).m_intSID; } if (count == 1) CounterBtn.Text = $"Clicked {count} time"; else CounterBtn.Text = $"Clicked {count} times"; } 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 TapGestureRecognizer_Tapped(object sender, EventArgs e) { count++; if (count == 1) CounterBtn.Text = $"Clicked {count} time"; else CounterBtn.Text = $"Clicked {count} times"; } }