Button 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
按鈕 View,它會回應觸控事件。
public ref class Button : Microsoft::Maui::Controls::View, Microsoft::Maui::Controls::IBorderElement, Microsoft::Maui::Controls::IButtonController, Microsoft::Maui::Controls::IElementConfiguration<Microsoft::Maui::Controls::Button ^>, Microsoft::Maui::Controls::IImageController, Microsoft::Maui::Controls::IImageElement, Microsoft::Maui::Controls::Internals::IFontElement, Microsoft::Maui::Controls::IPaddingElement, Microsoft::Maui::IImageButton, Microsoft::Maui::ITextButton
public class Button : Microsoft.Maui.Controls.View, Microsoft.Maui.Controls.IBorderElement, Microsoft.Maui.Controls.IButtonController, Microsoft.Maui.Controls.IElementConfiguration<Microsoft.Maui.Controls.Button>, Microsoft.Maui.Controls.IImageController, Microsoft.Maui.Controls.IImageElement, Microsoft.Maui.Controls.Internals.IFontElement, Microsoft.Maui.Controls.IPaddingElement, Microsoft.Maui.IImageButton, Microsoft.Maui.ITextButton
type Button = class
inherit View
interface IFontElement
interface IBorderElement
interface IButtonController
interface IViewController
interface IVisualElementController
interface IElementController
interface IElementConfiguration<Button>
interface IPaddingElement
interface IImageController
interface IImageElement
interface IButton
interface IView
interface IElement
interface ITransform
interface IPadding
interface IButtonStroke
interface ITextButton
interface IText
interface ITextStyle
interface IImageButton
interface IImage
interface IImageSourcePart
Public Class Button
Inherits View
Implements IBorderElement, IButtonController, IElementConfiguration(Of Button), IFontElement, IImageButton, IImageController, IImageElement, IPaddingElement, ITextButton
- 繼承
- 實作
-
IBorderElement IButtonController IElementConfiguration<Button> IElementController IImageController IImageElement IFontElement IPaddingElement IViewController IVisualElementController IButton IButtonStroke IElement IImage IImageButton IImageSourcePart IPadding IText ITextButton ITextStyle ITransform IView
備註
按鈕外觀
除了所有 View 物件通用的外觀屬性之外,Button 類別還提供 BorderColor、 BorderWidth和 ImageSource 屬性。
下列 XAML 代碼段示範如何使用這些屬性來建立包含文字 “Press me!” 的基本按鈕 按鈕顯示的影像會出現在版面配置的開頭端,這是由左至右語言的左側。
<Button Text="Press me!"
Image="icon.png"
BackgroundColor="White"
BorderWidth="2"
BorderColor="Black"
HorizontalOptions="Center"/>
若要使用上述 XAML 來顯示 icon.png
影像,開發人員必須將它放在適當的位置,以及每個平台專案特定的適當建置動作。 影像主題說明這一點和其他有關在 Microsoft.Maui.Controls 中使用影像的重要詳細數據。
按鈕事件
按鈕會分別通知 Pressed開發人員按下、放開和按下 Released、 和 Clicked 事件的事件。
下列 XAML 會將處理程式指派給下列兩個按鈕事件:
<Button x:Name="PressMeButton"
Text="Press Me!"
Pressed="PressMeButton_Pressed"
Clicked="PressMeButton_Clicked"/>
使用上述 XAML 時,程式代碼後置檔案中的下列事件處理程式會在引發這些事件時變更按鈕的文字:
private void PressMeButton_Pressed(object sender, EventArgs e)
{
(sender as Button).Text = "You pressed me!";
}
private void PressMeButton_Clicked(object sender, EventArgs e)
{
(sender as Button).Text = "I was just clicked!";
}
命令
按鈕的命令 API 可讓開發人員執行命令,以回應 Clicked 事件的內容。
下列 XAML 會指定按下按鈕時要執行的命令。 命令會更新名為的 modelUpdateCount
檢視模型屬性,按鈕接著會使用此屬性來更新其文字。 (顯示文字系結是否完整。只有命令程式代碼相關。) :
<Button Text="{Binding modelUpdateCount, StringFormat='Number of updates to the model: {0:D}'}"
Command="{Binding IncrementLabelCommand}" />
使用上述 XAML 時,開發人員會先定義 IncrementLabelCommand
檢視模型上的屬性,並實作方法,以便稍後具現化命令:
public ICommand IncrementLabelCommand { get; private set; }
void IncrementLabel()
{
modelUpdateCount++;
}
其次,開發人員會使用 方法 (或動作來初始化 命令) :
public ViewModelDemo()
{
IncrementLabelCommand = new Command(IncrementLabel);
}
最後,開發人員必須在主頁面的建構函式中,將系結內容設定為實作 屬性之檢視模型的系結內容:
public MainPage()
{
InitializeComponent();
BindingContext = new ViewModelDemo();
}
建構函式
Button() |
初始化 Button 類別的新執行個體。 |
欄位
BorderColorProperty |
BorderColor 可繫結屬性的備份存放區。 |
BorderWidthProperty |
BorderWidth 可繫結屬性的備份存放區。 |
CharacterSpacingProperty |
CharacterSpacing 可繫結屬性的備份存放區。 |
CommandParameterProperty |
CommandParameter 可繫結屬性的備份存放區。 |
CommandProperty |
Command 可繫結屬性的備份存放區。 |
ContentLayoutProperty |
ContentLayout 可繫結屬性的備份存放區。 |
ControlsButtonMapper |
已淘汰.
將抽象屬性對應至平臺特定方法的屬性對應程式,以便進一步處理。 |
CornerRadiusProperty |
CornerRadius 可繫結屬性的備份存放區。 |
FontAttributesProperty |
FontAttributes 可繫結屬性的備份存放區。 |
FontAutoScalingEnabledProperty |
FontAutoScalingEnabled 可繫結屬性的備份存放區。 |
FontFamilyProperty |
FontFamily 可繫結屬性的備份存放區。 |
FontSizeProperty |
FontSize 可繫結屬性的備份存放區。 |
ImageSourceProperty |
ImageSource 可繫結屬性的備份存放區。 |
IsPressedProperty |
IsPressed 可繫結屬性的備份存放區。 |
LineBreakModeProperty |
LineBreakMode 可繫結屬性的備份存放區。 |
PaddingProperty |
Padding 可繫結屬性的備份存放區。 |
propertyMapper |
表示檢視的內部 PropertyMapper。 (繼承來源 View) |
TextColorProperty |
TextColor 可繫結屬性的備份存放區。 |
TextProperty |
Text 可繫結屬性的備份存放區。 |
TextTransformProperty |
TextTransform 可繫結屬性的備份存放區。 |
屬性
AnchorX |
取得或設定相對於專案界限之任何轉換作業之中心點的 X 元件。 這是可繫結屬性。 (繼承來源 VisualElement) |
AnchorY |
取得或設定相對於專案界限的任何轉換作業之中心點的 Y 元件。 這是可繫結屬性。 (繼承來源 VisualElement) |
AutomationId |
取得或設定值,允許自動化架構尋找此項目並與之互動。 (繼承來源 Element) |
Background |
取得或設定 Brush 用來填滿專案背景的 。 這是可繫結屬性。 (繼承來源 VisualElement) |
BackgroundColor |
取得或設定 Color ,其會填滿專案的背景。 這是可繫結屬性。 (繼承來源 VisualElement) |
Batched |
取得值,這個值表示已針對這個項目進行批次變更。 (繼承來源 VisualElement) |
Behaviors |
取得與這個專案相關聯的物件清單 Behavior 。 這是唯讀的可繫結屬性。 (繼承來源 VisualElement) |
BindingContext |
取得或設定 對象,這個物件包含系結屬性所屬之系結屬性的目標屬性 BindableObject。 這是可繫結屬性。 (繼承來源 BindableObject) |
BorderColor |
取得或設定描述按鈕框線筆觸色彩的色彩。 這是可繫結屬性。 |
BorderWidth |
取得或設定框線的寬度,以裝置無關的單位。 這是可繫結屬性。 |
Bounds |
取得與裝置無關單位的專案界限。 (繼承來源 VisualElement) |
CharacterSpacing |
取得或設定在按鈕上顯示時,每個字元 Text 之間的間距。 這是可繫結屬性。 |
class |
按鈕 View,它會回應觸控事件。 (繼承來源 NavigableElement) |
ClassId |
取得或設定值,用來識別語意類似項目的集合。 (繼承來源 Element) |
Clip |
指定專案的裁剪區域。 這是可繫結屬性。 (繼承來源 VisualElement) |
Command |
取得或設定啟動按鈕時要叫用的命令。 這是可繫結屬性。 |
CommandParameter |
取得或設定要傳遞至 Command 屬性的參數。
預設值是 |
ContentLayout |
取得或設定物件,控制按鈕影像位置和按鈕影像與按鈕文字之間的間距。 這是可繫結屬性。 |
CornerRadius |
取得或設定按鈕的圓角半徑,其單位與裝置無關。 這是可繫結屬性。 |
DesiredSize |
取得這個項目在版面配置處理序的測量傳遞期間所計算的大小。 (繼承來源 VisualElement) |
DisableLayout |
取得值,這個值表示已停用這個專案的配置。 (繼承來源 VisualElement) |
Dispatcher |
取得建立這個可系結物件時可用的發送器,否則會嘗試尋找最接近的可用發送器, (可能是視窗/應用程式的) 。 (繼承來源 BindableObject) |
EffectControlProvider |
供 .NET MAUI 內部使用。 (繼承來源 Element) |
Effects |
取得或設定將在運行時間套用至項目的樣式和屬性。 (繼承來源 Element) |
FlowDirection |
取得或設定配置流程方向。 這是可繫結屬性。 (繼承來源 VisualElement) |
FontAttributes |
取得或設定值,這個值表示此按鈕的文字字型是否為粗體、斜體或兩者都不是。 這是可繫結屬性。 |
FontAutoScalingEnabled |
判斷此專案的字型是否應該根據操作系統設定自動調整。 預設值為 |
FontFamily |
取得或設定這個專案文字的字型系列。 這是可繫結屬性。 |
FontSize |
取得或設定這個專案文字的字型大小。 這是可繫結屬性。 |
Frame |
取得或設定這個項目位於畫面上的框架。 (繼承來源 VisualElement) |
GestureController |
按鈕 View,它會回應觸控事件。 (繼承來源 View) |
GestureRecognizers |
與此檢視建立關聯的筆勢辨識器集合。 (繼承來源 View) |
Handler |
取得或設定 IViewHandler 與這個項目相關聯的 。 (繼承來源 VisualElement) |
Height |
取得此項目的目前呈現高度。 這是唯讀的可繫結屬性。 (繼承來源 VisualElement) |
HeightRequest |
取得或設定此項目所需的高度覆寫項。 這是可繫結屬性。 (繼承來源 VisualElement) |
HorizontalOptions |
取得或設定 , LayoutOptions 定義如何在版面配置週期中配置專案。 這是可繫結屬性。 (繼承來源 View) |
Id |
取得值,這個值可用來在應用程式執行期間唯一識別專案。 (繼承來源 Element) |
ImageSource |
允許您在按鈕上顯示點陣圖影像。 這是可繫結屬性。 |
InputTransparent |
取得或設定值,指出這個專案是否會在用戶互動期間響應點擊測試。 這是可繫結屬性。 (繼承來源 VisualElement) |
IsEnabled |
取得或設定值,這個值指出使用者介面中是否已啟用此項目。 這是可繫結屬性。 (繼承來源 VisualElement) |
IsEnabledCore |
按鈕 View,它會回應觸控事件。 |
IsEnabledCore |
這個值代表累計 IsEnabled 值。 如果值將會變更,則覆寫這個屬性的所有類型也需要叫用 RefreshIsEnabledProperty () 方法。 (繼承來源 VisualElement) |
IsFocused |
取得值,這個值指出此項目目前是否為焦點。 這是可繫結屬性。 (繼承來源 VisualElement) |
IsInPlatformLayout |
取得或設定值,這個值表示這個專案目前正在執行平臺配置週期。 (繼承來源 VisualElement) |
IsLoaded |
指出專案是否連接到主物件樹狀結構。 (繼承來源 VisualElement) |
IsPlatformEnabled |
取得或設定值,這個值表示是否啟用這個項目的平臺對等專案。 (繼承來源 VisualElement) |
IsPlatformStateConsistent |
取得或設定值,這個值表示這個專案目前與平臺對等項目狀態一致。 (繼承來源 VisualElement) |
IsPressed |
取得目前是否按下按鈕。 |
IsVisible |
取得或設定值,這個值會決定此元素是否會在畫面上顯示,並佔用版面配置的空間。 這是可繫結屬性。 (繼承來源 VisualElement) |
LineBreakMode |
決定當長度溢位此按鈕的大小時,如何 Text 顯示。 這是可繫結屬性。 |
LogicalChildren |
已淘汰.
供 Microsoft.Maui.Controls 平台內部使用。 (繼承來源 Element) |
Margin |
取得或設定檢視的邊界。 (繼承來源 View) |
MaximumHeightRequest |
取得或設定專案在版面配置期間要求的最大高度。 這是可繫結屬性。 (繼承來源 VisualElement) |
MaximumWidthRequest |
取得或設定專案在版面配置期間要求的最大寬度。 這是可繫結屬性。 (繼承來源 VisualElement) |
MinimumHeightRequest |
取得或設定元素在版面配置期間要求的最低高度。 這是可繫結屬性。 (繼承來源 VisualElement) |
MinimumWidthRequest |
取得或設定元素在版面配置期間要求的最低寬度。 這是可繫結屬性。 (繼承來源 VisualElement) |
Navigation |
按鈕 View,它會回應觸控事件。 (繼承來源 NavigableElement) |
NavigationProxy |
按鈕 View,它會回應觸控事件。 (繼承來源 NavigableElement) |
Opacity |
取得或設定套用至項目 (呈現時) 的不透明度值。 此值的範圍是 0 到 1;超出此範圍的值會設定為最接近的有效值。 這是可繫結屬性。 (繼承來源 VisualElement) |
Padding |
取得或設定按鈕的邊框間距。 這是可繫結屬性。 |
Parent |
取得或設定這個專案的父 Element 代。 (繼承來源 Element) |
RealParent |
供 .NET MAUI 內部使用。 (繼承來源 Element) |
Resources |
取得或設定本機資源字典。 (繼承來源 VisualElement) |
Rotation |
取得或設定呈現元素時繞著 Z 軸 (仿射旋轉) 的旋轉 (度)。 這是可繫結屬性。 (繼承來源 VisualElement) |
RotationX |
取得或設定呈現元素時繞著 X 軸 (透視旋轉) 的旋轉 (度)。 這是可繫結屬性。 (繼承來源 VisualElement) |
RotationY |
取得或設定呈現元素時繞著 Y 軸 (透視旋轉) 的旋轉 (度)。 這是可繫結屬性。 (繼承來源 VisualElement) |
Scale |
取得或設定套用至項目的縮放比例。 這是可繫結屬性。 (繼承來源 VisualElement) |
ScaleX |
取得或設定要套用至 X 方向的小數位數值。 這是可繫結屬性。 (繼承來源 VisualElement) |
ScaleY |
取得或設定要套用至 Y 方向的小數位數值。 這是可繫結屬性。 (繼承來源 VisualElement) |
Shadow |
取得或設定 元素所轉換的陰影效果。 這是可繫結屬性。 (繼承來源 VisualElement) |
Style |
按鈕 View,它會回應觸控事件。 (繼承來源 NavigableElement) |
StyleClass |
按鈕 View,它會回應觸控事件。 (繼承來源 NavigableElement) |
StyleId |
取得或設定使用者定義值來唯一識別項目。 (繼承來源 Element) |
Text |
取得或設定顯示為按鈕內容的文字。
預設值是 |
TextColor |
取得或設定按鈕文字的 Color。 這是可繫結屬性。 |
TextTransform |
將文字轉換套用到 Text 此按鈕上顯示的 。 這是可繫結屬性。 |
TranslationX |
取得或設定項目的 X 轉譯差異。 這是可繫結屬性。 (繼承來源 VisualElement) |
TranslationY |
取得或設定項目的 Y 轉譯差異。 這是可繫結屬性。 (繼承來源 VisualElement) |
Triggers |
取得與這個專案相關聯的物件清單 TriggerBase 。 這是唯讀的可繫結屬性。 (繼承來源 VisualElement) |
VerticalOptions |
取得或設定 , LayoutOptions 定義如何在版面配置週期中配置專案。 這是可繫結屬性。 (繼承來源 View) |
Visual |
取得或設定會 IVisual 覆寫項目視覺外觀的實作。 這是可繫結屬性。 (繼承來源 VisualElement) |
Width |
取得這個專案的目前寬度。 這是唯讀的可繫結屬性。 (繼承來源 VisualElement) |
WidthRequest |
取得或設定此項目所需的寬度覆寫項。 這是可繫結屬性。 (繼承來源 VisualElement) |
Window |
Window取得與項目相關聯的 。 這是唯讀的可繫結屬性。 (繼承來源 VisualElement) |
X |
取得此項目的目前 X 位置。 這是唯讀的可繫結屬性。 (繼承來源 VisualElement) |
Y |
取得此項目的目前 Y 位置。 這是唯讀的可繫結屬性。 (繼承來源 VisualElement) |
ZIndex |
取得或設定版面配置中專案的前至后 z 索引。 這是可繫結屬性。 (繼承來源 VisualElement) |
方法
事件
明確介面實作
擴充方法
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應