N. *_*ing 3 c# xaml xamarin xamarin.forms
我的图像按钮有问题我希望我的功能Categorie_Onclick可以看到按钮被点击.我尝试添加标签,但它不起作用.有没有办法做到这一点?
XAML:
<!-- Button1 -->
<Image
x:Name="CityGuideButton1"
Source="shopping_gray.png"
Aspect="Fill"
HorizontalOptions="FillAndExpand"
VerticalOptions ="FillAndExpand"
Tag="01">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="Categorie_Onclick"
NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
<!-- Button2 -->
<Image
x:Name="CityGuideButton2"
Source="secrets.png"
Aspect="Fill"
HorizontalOptions="FillAndExpand"
VerticalOptions ="FillAndExpand"
Tag="02">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="Categorie_Onclick"
NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
Run Code Online (Sandbox Code Playgroud)
按钮处理程序
private async void Categorie_Onclick(Object sender, EventArgs args)
{
Image cmd = sender as Image;
string txt = TapGestureRecognizer.tag.ToString();
await Navigation.PushAsync(new CategoriePage());
}
Run Code Online (Sandbox Code Playgroud)
你可能会滥用StyleId
,ClassId
或者AutomationId
为此,但这很糟糕,所以不要.
最简单的方法是定义一个附加的BindableProperty.您可以在所需的类中定义它.
public class Foo {
public static readonly BindableProperty TagProperty = BindableProperty.Create("Tag", typeof(string), typeof(Foo), null);
public static string GetTag(BindableObject bindable)
{
return (string)bindable.GetValue(TagProperty);
}
public static void SetTag(BindableObject bindable, string value)
{
bindable.SetValue(TagProperty, value);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在Xaml中设置此标记
<Image ... local:Foo.Tag="button1" />
Run Code Online (Sandbox Code Playgroud)
并从事件处理程序中获取该标记
async void Categorie_Onclick(Object sender, EventArgs args)
{
Image cmd = sender as Image;
string txt = Foo.GetTag(cmd);
await Navigation.PushAsync(new CategoriePage());
}
Run Code Online (Sandbox Code Playgroud)
您可能想知道为什么它没有内置到平台中.好吧,这可能是因为使用正确的Mvvm,这不是必需的.
归档时间: |
|
查看次数: |
3000 次 |
最近记录: |