【问题标题】:Bindings not working using custom control使用自定义控件绑定不起作用
【发布时间】:2021-02-18 01:48:11
【问题描述】:

我正在尝试使用 Binding 制作自定义控件,但我的 ImageSource 绑定不起作用。这是我的代码:

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" 在 MainPage 中设置图标。知道为什么不起作用吗?

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    您可以修改自定义视图的代码,如下所示。

    在xml中

    <?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>
    

    【讨论】:

    • 谢谢,它成功了,但我有一个问题。为什么对其他人来说绑定仅适用于 Source="{Binding Icon}" 而我需要编写所有这些 Source="{Binding Source={x:Reference CustomView},Path=Icon}" ?
    • 我们需要在自定义视图中直接设置绑定路径。在 contentpage 中它会起作用。
    【解决方案2】:

    来自documentation

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

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

    【讨论】:

    • 还是不行,我现在用我的代码编辑了帖子。我将 LeftSideIconProperty 重命名为 IconProperty,但仍然出现空白屏幕。
    • 我重新启动了 VS,现在是的,我收到了这个错误。 System.MissingFieldException:'找不到字段:Xamarin.Forms.BindableProperty Custom.CustomEntry.IconProperty 由于:找不到
    • 您的CustomEntryCustomControl 似乎不匹配
    • 我尝试在其他具有相同名称和相同错误的项目中执行相同的操作。我不知道我做错了什么。
    • 你在Customcontrol.xaml 中有一个声明x:Class="Custom.CustomEntry,但是你的类本身被命名为CustomControl。是不是这个例子的错字?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 2012-12-12
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    相关资源
    最近更新 更多