簡體   English   中英

在Xamarin.Forms中以編程方式引發控件事件

[英]Raise control event programmatically in Xamarin.Forms

我在XAML中定義了一個按鈕:

<Button x:Name="loginButton" Text="Login" Clicked="OnLoginButtonClicked" />

是否可以通過編程引發Clicked事件? 當然,我可以調用OnLoginButtonClicked ,但是我對如何引發Clicked事件本身很感興趣。

如果您只想調用Clicked操作,則可以執行以下操作:

    var b = new Button();

    b.Clicked += (x, y) =>
    {
        //Your method here
    };

    var t = b as IButtonController;

    t.SendClicked(); //This will call the action

重要的是要注意這不是正確的選擇。 如前所述,首選使用實際方法。

您可以通過事件處理程序或代碼中的任何其他位置調用DoSomething

void OnLoginButtonClicked(object sender, EventArgs e)
{
    DoSomething ();
}

private void DoSomething()
{
    //Click code here.
}
  1. 關聯委托方法

     testButton3.TouchUpInside += HandleTouchUpInside; 
  2. 添加方法

     void HandleTouchUpInside (object sender, EventArgs ea) { new UIAlertView("Touch3", "TouchUpInside handled", null, "OK", null).Show(); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM