可重复使用的 xamarin 控件提供 "unable to cast object of type 'Xamarin.forms.xaml.elementnode'..."
reuseable xamarin control giving "unable to cast object of type 'Xamarin.forms.xaml.elementnode'..."
我是一个菜鸟类型的编码员,这是我第一次尝试 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 ();
}
}
}
感谢您的帮助,如果您发现更好的方法,请告诉我。我是来学习的。
<controls:CircleImage.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=TapGestureCommand}" />
</controls:CircleImage.GestureRecognizers>
在 XAML 中使用 Command 而非 Tapped 事件进行绑定。
我是一个菜鸟类型的编码员,这是我第一次尝试 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 ();
}
}
}
感谢您的帮助,如果您发现更好的方法,请告诉我。我是来学习的。
<controls:CircleImage.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=TapGestureCommand}" />
</controls:CircleImage.GestureRecognizers>
在 XAML 中使用 Command 而非 Tapped 事件进行绑定。