Xamarin.forms 自定义控件上的BindableProperty存在问题
将文本绑定到自定义控件内的标签时遇到问题 我已进行了以下控制:Xamarin.forms 自定义控件上的BindableProperty存在问题,xamarin.forms,Xamarin.forms,将文本绑定到自定义控件内的标签时遇到问题 我已进行了以下控制: <?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Frida3.Components.Te
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Frida3.Components.TestView">
<ContentView.Content>
<StackLayout>
<Label Text="{Binding Text}" />
</StackLayout>
</ContentView.Content>
</ContentView>
使用控件和绑定设置Text属性时,不会显示任何内容。下面是显示结果的一些示例代码:
<!-- Shows that the LoginText binding contains value -->
<Label Text="{Binding LoginText}" BackgroundColor="BurlyWood"/>
<!-- Nothing shown with same binding -->
<components:TestView Text="{Binding LoginText}" BackgroundColor="Aqua" />
<!-- Works without binding -->
<components:TestView Text="This is showing" BackgroundColor="Yellow" />
结果如下:
我找到了解决办法。基本上,我给我的控件一个名称(
x:name=“this”
),然后向数据绑定添加一个源,就像这样:source={x:Reference this}
。
通过这些更改,我可以删除BindingContext=this代码>
因此,最终的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"
x:Class="Frida3.Components.TestView"
x:Name="this">
<ContentView.Content>
<StackLayout>
<Label Text="{Binding Path=Text, Source={x:Reference this}}" />
</StackLayout>
</ContentView.Content>
</ContentView>
我想知道是否有更简单的方法,这样我就不必为每个使用BindableProperty的子级添加源代码了。我找到了一个解决方案。基本上,我给我的控件一个名称(x:name=“this”
),然后向数据绑定添加一个源,就像这样:source={x:Reference this}
。
通过这些更改,我可以删除BindingContext=this代码>
因此,最终的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"
x:Class="Frida3.Components.TestView"
x:Name="this">
<ContentView.Content>
<StackLayout>
<Label Text="{Binding Path=Text, Source={x:Reference this}}" />
</StackLayout>
</ContentView.Content>
</ContentView>
我想知道是否有更简单的方法,这样我就不必向每个使用BindableProperty的子级添加源代码了。XAML
让我们使用自定义控件的XAML,如下所示:
向自定义控件添加绑定会增加应用程序的另一个复杂性级别,如果过度使用,可能会妨碍应用程序的性能。最好将标签重写为:
现在,可以在代码隐藏中访问标签
代码隐藏
您已在问题中正确设置属性,如下所示:
公共静态只读BindableProperty TextProperty=
BindableProperty.Create(
“文本”,
类型(字符串),
类型(TestView),
默认值(字符串));
公共字符串文本
{
get=>(字符串)GetValue(TextProperty);
set=>SetValue(TextProperty,value);
}
您可以删除BindingContext=this因为您将不使用任何绑定,所以代码>将从构造函数中删除
在TextProperty
中,我们将为propertyChanged
参数添加一个事件,并定义该事件:
公共静态只读BindableProperty TextProperty=
BindableProperty.Create(
propertyName:nameof(文本),
returnType:typeof(字符串),
declaringType:typeof(TestView),
默认值:默认值(字符串),
属性更改:HandleTextPropertyChanged);//属性已更改处理程序!!
公共字符串文本
{
get=>(字符串)GetValue(TextProperty);
set=>SetValue(TextProperty,value);
}
//文本属性更改时的处理程序。
私有静态无效HandleTextPropertyChanged(
bindable对象可绑定、对象旧值、对象新值)
{
var-control=(TestView)可绑定;
if(控件!=null)
{
control.MyLabel.Text=(字符串)newValue;
}
}
这里发生了什么事?基本上,你已经告诉了应用程序
“当TestView
的Text
属性更改时,设置Text
将MyLabel
的属性设置为新字符串值”
您的代码现在应该如下所示:
//文本属性的BindableProperty
公共静态只读BindableProperty TextProperty=
BindableProperty.Create(
propertyName:nameof(文本),
returnType:typeof(字符串),
declaringType:typeof(TestView),
默认值:默认值(字符串),
属性更改:HandleTextPropertyChanged);//属性已更改处理程序!!
//TestView的文本属性
公共字符串文本
{
get=>(字符串)GetValue(TextProperty);
set=>SetValue(TextProperty,value);
}
//建造师
公共测试视图()
{
初始化组件();
}
//文本属性更改时的处理程序。
私有静态无效HandleTextPropertyChanged(
bindable对象可绑定、对象旧值、对象新值)
{
var-control=(TestView)可绑定;
if(控件!=null)
{
control.MyLabel.Text=(字符串)newValue;
}
}
您现在可以这样调用自定义视图:
给你!没有绑定,只有好的老式事件。XAML
让我们使用自定义控件的XAML,如下所示:
向自定义控件添加绑定会增加应用程序的另一个复杂性级别,如果过度使用,可能会妨碍应用程序的性能。最好将标签重写为:
现在,可以在代码隐藏中访问标签
代码隐藏
您已在问题中正确设置属性,如下所示:
公共静态只读BindableProperty TextProperty=
BindableProperty.Create(
“文本”,
类型(字符串),
类型(TestView),
默认值(字符串));
公共字符串文本
{
get=>(字符串)GetValue(TextProperty);
set=>SetValue(TextProperty,value);
}
您可以删除BindingContext=this因为您将不使用任何绑定,所以代码>将从构造函数中删除
在TextProperty
中,我们将为propertyChanged
参数添加一个事件,并定义该事件:
公共静态只读BindableProperty TextProperty=
BindableProperty.Create(
propertyName:nameof(文本),
returnType:typeof(字符串),
declaringType:typeof(TestView),
默认值:默认值(字符串),
属性更改:HandleTextPropertyChanged);//属性已更改处理程序!!
公共字符串文本
{
get=>(字符串)GetValue(TextProperty);
set=>SetValue(TextProperty,value);
}
//文本属性更改时的处理程序。
私有静态无效HandleTextPropertyChanged(
BindableObject绑定