簡體   English   中英

xamarin表單-將TapGestureRecognizer綁定到代碼隱藏而不是視圖模型

[英]xamarin forms - bind TapGestureRecognizer to code-behind rather than view model

我意識到這打破了MVVM模式,但是是否可以將TapGestureRecognizer綁定到XAML.cs代碼后面的方法而不是視圖模型?

  <Image.GestureRecognizers>
      <TapGestureRecognizer 
        Command="{Binding Path=BindingContext.SetImageCommand, Source={x:Reference ThePage}}"
        CommandParameter="{Binding .}"/>
  </Image.GestureRecognizers>

當然,只需使用Tapped事件即可。

<TapGestureRecognizer Tapped="Thing_Tapped" />

並在您的代碼背后:

public void Thing_Tapped (object sender, EventArgs args)
{
   // Do your thing
}

也許乍一看,您的意思是如果您可以 Command 綁定到后面代碼中的某些內容上。 我還沒有測試過,但是看起來它應該可以與您的代碼一起工作,並且需要稍作調整,只需執行以下操作:

<Image.GestureRecognizers>
      <TapGestureRecognizer Command="{Binding Path=SetImageCommand, Source={x:Reference ThePage}}" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>

注意我如何刪除BindingContext. 從綁定。 這意味着它已綁定到頁面的BindingContext屬性。 您當然也可以綁定到其他屬性。 現在,您只需將SetImageCommand移到頁面的代碼后面即可,它應該可以工作。

無論哪種方式,您現在都可以從頁面而不是視圖模型中觸發邏輯。

暫無
暫無

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

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