C# WINDOWS MAUI專案 建立增加屬性的按鈕(C# .net maui custom button 新增屬性 example)
C# WINDOWS MAUI專案 建立增加屬性的按鈕(C# .net maui custom button 新增屬性 example)
資料來源: ChatGPT
GITHUB: https://github.com/jash-git/MAUI_WinAPI_Object_test/tree/main/Code/05_net7
C# Code
using Microsoft.Maui.Controls;
namespace MAUI_WinAPI_Object_test.CustomControls
{
public class CustomButton : Button
{
// 新增您的自訂屬性
public static readonly BindableProperty CustomPropertyProperty =
BindableProperty.Create(nameof(CustomProperty), typeof(string), typeof(CustomButton), string.Empty);
public string CustomProperty
{
get { return (string)GetValue(CustomPropertyProperty); }
set { SetValue(CustomPropertyProperty, value); }
}
public CustomButton()
{
// 在此處設置自定義按鈕的外觀和行為
// 例如,您可以設置背景色、文本顏色、字體大小等等
BackgroundColor=Color.FromRgb((byte)0, (byte)0, (byte)255);
TextColor = Color.FromRgb((byte)255, (byte)255, (byte)255);
}
}
}
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"
x:Class="MAUI_WinAPI_Object_test.MainPage">
<ScrollView>
<VerticalStackLayout
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
x:Name="labtime"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
<Label
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" />
<Button
x:Name="CloseBtn"
Text="Close"
Clicked="CloseBtn_Clicked"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>