我是一个新手类型的编码器,这是我第一次尝试在Xamarin.Forms中使用可重用控件(实际上也是我在所有编程中的第一次尝试)。在研究中,我发现它说我在给一个事件赋值.我很确定代码的哪一部分-- means...but --我不知道该怎么做才能使它正确。我正在尝试制作一个带有标签的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 ();
}
}
}我很感激你的帮助,如果你有更好的方法来做这件事,请告诉我。我是来学习的。
发布于 2018-06-15 08:58:30
<controls:CircleImage.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=TapGestureCommand}" />
</controls:CircleImage.GestureRecognizers>在XAML中使用命令来绑定而不是点击事件。
https://stackoverflow.com/questions/50867493
复制相似问题