绑定无法使用自定义控件

时间:2020-11-05 10:57:49

标签: c# xamarin.forms

我正在尝试使用Binding创建自定义控件,而ImageSource的Binding无法正常工作。那是我的代码:

Customcontrol.xaml:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
    x:Class="Custom.CustomEntry"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentView.Content>
        <StackLayout Orientation="Vertical">

            <ImageButton
                BackgroundColor="Transparent"
                Clicked="Open"
                Source="{Binding Icon}" />



            <Frame
                x:Name="frameemojis"
                BackgroundColor="red"
                IsVisible="False">
                <Label Text="Hello" />
            </Frame>
        </StackLayout>
    </ContentView.Content>
</ContentView>
CustomControl.xaml.cs:

公共局部类CustomControl:ContentView { 公共静态只读BindableProperty IconProperty = BindableProperty.Create(“ Icon”,typeof(ImageSource),typeof(CustomControl));

    public ImageSource Icon
    {
        get { return (ImageSource)GetValue(IconProperty); }
        set { SetValue(IconProperty, value); }
    }



    bool state = true;
    void Open(object o, System.EventArgs e)
    {

        if (state)
        {
            state = false;
            frameemojis.IsVisible = true;
        }
        else
        {
            state = true;
            frameemojis.IsVisible = false;
        }

    }



    public CustomControl()
    {
        InitializeComponent();
    }
}

}

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="App23.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:fav="clr-namespace:Custom;assembly=Custom">

    <StackLayout>
        <fav:CustomControl Icon="icon.png" WidthRequest="200" />
    </StackLayout>

</ContentPage>

MainPage.xaml.cs:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        BindingContext = new ViewModel();
        InitializeComponent();
    }
}

ViewModel现在为空,但是将来我将使用它来控制其他事情。我的想法是使用 Icon =“ icon.png” 在主页中设置图标。知道为什么不起作用吗?

2 个答案:

答案 0 :(得分:0)

来自documentation

可绑定属性的命名约定是,可绑定属性标识符必须与Create方法中指定的属性名称匹配,并在属性名称后附加“ Property”。

因此,您应将LeftSideIconProperty重命名为IconProperty或将Icon重命名为LeftSideIcon

答案 1 :(得分:0)

您可以按照以下步骤修改“自定义视图”的代码。

xaml

<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
    x:Class="Custom.CustomEntry"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     x:Name="CustomView"   //set name of custom view
    >
    <ContentView.Content>
        <StackLayout Orientation="Vertical">

            <ImageButton
                BackgroundColor="Transparent"
                Clicked="Open"
                Source="{Binding Source={x:Reference CustomView},Path=Icon}" />



            <Frame
                x:Name="frameemojis"
                BackgroundColor="red"
                IsVisible="False">
                <Label Text="Hello" />
            </Frame>
        </StackLayout>
    </ContentView.Content>
</ContentView>
相关问题