首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可重用的xamarin控件提供“无法强制转换类型为‘xamarin.forms.xaml.memode’的对象”.

可重用的xamarin控件提供“无法强制转换类型为‘xamarin.forms.xaml.memode’的对象”.
EN

Stack Overflow用户
提问于 2018-06-15 00:01:40
回答 1查看 447关注 0票数 0

我是一个新手类型的编码器,这是我第一次尝试在Xamarin.Forms中使用可重用控件(实际上也是我在所有编程中的第一次尝试)。在研究中,我发现它说我在给一个事件赋值.我很确定代码的哪一部分-- means...but --我不知道该怎么做才能使它正确。我正在尝试制作一个带有标签的ImageCircle,这个标签是一个可重用的控件。

我的Xaml:

代码语言:javascript
复制
       <?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>

我的代码背后是:

代码语言:javascript
复制
    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 ();
    }
}
}

我很感激你的帮助,如果你有更好的方法来做这件事,请告诉我。我是来学习的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-15 08:58:30

代码语言:javascript
复制
<controls:CircleImage.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=TapGestureCommand}" />
            </controls:CircleImage.GestureRecognizers>

在XAML中使用命令来绑定而不是点击事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50867493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档