C# 使用自定义控件绑定不起作用
我正在尝试使用绑定创建自定义控件,但ImageSource的绑定不起作用。这是我的密码: Customcontrol.xaml:C# 使用自定义控件绑定不起作用,c#,xamarin.forms,C#,Xamarin.forms,我正在尝试使用绑定创建自定义控件,但ImageSource的绑定不起作用。这是我的密码: Customcontrol.xaml: 来自 可绑定属性的命名约定是,可绑定属性标识符必须与Create方法中指定的属性名称匹配,并附加“property” 因此,您应该将LeftSideIconProperty重命名为IconProperty或Icon重命名为LeftSideIcon您可以修改自定义视图的代码,如下所示 在xaml中 仍然不起作用,我现在用我的代码编辑了这篇文章。我将LeftSide
来自
可绑定属性的命名约定是,可绑定属性标识符必须与Create方法中指定的属性名称匹配,并附加“property”
因此,您应该将
LeftSideIconProperty
重命名为IconProperty
或Icon
重命名为LeftSideIcon
您可以修改自定义视图的代码,如下所示
在xaml中
仍然不起作用,我现在用我的代码编辑了这篇文章。我将LeftSideIconProperty重命名为IconProperty,仍然得到一个空白屏幕。我重新启动了VS,现在是的,我得到了这个错误。System.MissingFieldException:'未找到字段:Xamarin.Forms.BindableProperty Custom.CustomEntry.IconProperty原因:无法找到它看起来您的CustomEntry
和CustomControl
之间不匹配。我试图在其他项目中使用相同的名称和相同的错误执行相同的操作。我不知道我做错了什么。你在Customcontrol.xaml
中有一个声明x:Class=“Custom.CustomEntry
,但是你的类本身被命名为Customcontrol
。这个例子是打字错误吗?谢谢,它起作用了,但我有一个问题。为什么对其他人来说,绑定只在Source=“{binding Icon}下工作“我需要编写所有这些源=“{Binding Source={x:Reference CustomView},Path=Icon}”?我们需要直接在自定义视图中设置绑定路径。在contentpage中,它将起作用。
<?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>