我是一个noob'ish类型的编码器,这是我在Xamarin.Forms(我的所有编程中真的是我的第一个相对第一个)中的可重复控制的第一次尝试。在研究中,我发现它说我正在为一个事件分配一个值...我很确定它代码的哪一部分代码......但是我无法弄清楚的是如何使它正确。我正在尝试制作一个带有标签的ImageCircle,它是一个可重复使用的控件。
我的Xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin"
x:Class="MyApp.View.Templates.CircleImageButton"
x:Name="this">
<ContentView.Content>
<StackLayout Margin="{StaticResource Margin}" Padding="{StaticResource Padding}" Spacing="0" Grid.Row="{Binding Source={x:Reference this}, Path=GridRow}" Grid.Column="{Binding Source={x:Reference this}, Path=GridColumn}">
<controls:CircleImage Source="{Binding Source={x:Reference this}, Path=Image}" FillColor="Green" Aspect="AspectFill" Margin="{StaticResource Margin}" >
<!--error is believed to be caused here-->
<controls:CircleImage.GestureRecognizers>
<TapGestureRecognizer Tapped="{Binding Source={x:Reference this}, Path=TapGestureCommand}" />
</controls:CircleImage.GestureRecognizers>
<controls:CircleImage.WidthRequest>
<OnPlatform x:TypeArguments="x:Double">
<On Platform="Android, iOS">40</On>
<On Platform="WinPhone">75</On>
</OnPlatform>
</controls:CircleImage.WidthRequest>
<controls:CircleImage.HeightRequest>
<OnPlatform x:TypeArguments="x:Double">
<On Platform="Android, iOS">40</On>
<On Platform="WinPhone">75</On>
</OnPlatform>
</controls:CircleImage.HeightRequest>
</controls:CircleImage>
<Label Text="{Binding Source={x:Reference this}, Path=LabelCaption}"/>
</StackLayout>
</ContentView.Content>
</ContentView>
我的代码背后:
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MyApp.View.Templates
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CircleImageButton : ContentView
{
public static readonly BindableProperty ImageProperty = BindableProperty.Create("Image", typeof(string), typeof(CircleImageButton), string.Empty);
public static readonly BindableProperty GridRowProperty = BindableProperty.Create("GridRow", typeof(int), typeof(CircleImageButton), default(int));
public static readonly BindableProperty GridColumnProperty = BindableProperty.Create("GridColumn", typeof(int), typeof(CircleImageButton), default(int));
public static readonly BindableProperty LabelCaptionProperty = BindableProperty.Create("LabelCaption", typeof(string), typeof(CircleImageButton), string.Empty);
public static readonly BindableProperty TapGestureCommandProperty = BindableProperty.Create("TapGestureCommand", typeof(string), typeof(CircleImageButton), string.Empty);
public string Image
{
get { return (string)GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
public int GridRow
{
get { return (int)GetValue(GridRowProperty); }
set { SetValue(GridRowProperty, value); }
}
public int GridColumn
{
get { return (int)GetValue(GridColumnProperty); }
set { SetValue(GridColumnProperty, value); }
}
public string LabelCaption
{
get { return (string)GetValue(LabelCaptionProperty); }
set { SetValue(LabelCaptionProperty, value); }
}
public string TapGestureCommand
{
get { return (string)GetValue(TapGestureCommandProperty); }
set { SetValue(TapGestureCommandProperty, value); }
}
public CircleImageButton ()
{
InitializeComponent ();
}
}
}
感谢您的帮助,如果您看到更好的方法,请告诉我。我在这里学习。
答案 0 :(得分:0)
<controls:CircleImage.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=TapGestureCommand}" />
</controls:CircleImage.GestureRecognizers>
在XAML中使用Command进行绑定而不是Tapped事件。