[英]Xamarin Forms - TabbedPage platform specific xaml code to code-behind
[英]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.