Atu*_*uka 27 xaml android ios xamarin xamarin.forms
我是xamarin.forms的新手.请帮我解释如何在Stack Layout或Frame中添加click事件
<Frame Grid.Column="0" BackgroundColor="#ffffff" Grid.Row="0" HasShadow="true" OutlineColor = "Black">
</Frame>
<StackLayout Grid.Column="0" BackgroundColor="#313FA0" Grid.Row="0">
</StackLayout>
Run Code Online (Sandbox Code Playgroud)
pna*_*avk 57
您可以将TapGestureRecognizer添加到XAML中的StackLayout,如下所示:
<StackLayout Grid.Column="0" Grid.Row="0" BackgroundColor="#313FA0">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTapped"/>
</StackLayout.GestureRecognizers>
</StackLayout>
Run Code Online (Sandbox Code Playgroud)
然后你可以在后面的代码中实现OnTapped方法:
void OnTapped(object sender, EventArgs e)
{
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您正在使用MVVM模式并且希望将点击绑定到ViewModel中的ICommand,则可以这样实现:
<StackLayout Grid.Column="0" Grid.Row="0" BackgroundColor="#313FA0">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}"/>
</StackLayout.GestureRecognizers>
</StackLayout>
Run Code Online (Sandbox Code Playgroud)
在您的ViewModel中,您将拥有:
ICommand tapCommand = new Command(OnTapped);
void OnTapped()
{
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
Xamarin网站上有一些非常好的指南:
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/gestures/#Using_Xaml
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/gestures/tap/
好吧,谢谢@ pnavk,根据我所见,我也可以分享一下,没有内置OnClick或Click Events 的Views (布局,框架,图像等)具有解决click Event的相同方法。
如下 :
对于图像:
<Image>
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="onImageCitizenReporterTapped" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
Run Code Online (Sandbox Code Playgroud)
对于框架:
<Frame>
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="onFrameCitizenReporterTapped" NumberOfTapsRequired="1" />
</Frame.GestureRecognizers>
</Frame>
Run Code Online (Sandbox Code Playgroud)
对于StackLayout:
<StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="onStackCitizenReporterTapped" NumberOfTapsRequired="1" />
</StackLayout.GestureRecognizers>
</StackLayout >
Run Code Online (Sandbox Code Playgroud)
干杯。
| 归档时间: |
|
| 查看次数: |
20138 次 |
| 最近记录: |