Mvvm 表单将父属性与子属性';以XAML表示的s
我搜索了两天,找不到这个问题的答案。我有一个简单的内容页,具有一个可绑定属性:Mvvm 表单将父属性与子属性';以XAML表示的s,mvvm,xamarin.forms,Mvvm,Xamarin.forms,我搜索了两天,找不到这个问题的答案。我有一个简单的内容页,具有一个可绑定属性: public partial class Page1 : ContentPage { public static readonly BindableProperty MainTextProperty = BindableProperty.Create<Page1 , string>(p => p.MainText, string.Empty); public string Main
public partial class Page1 : ContentPage
{
public static readonly BindableProperty MainTextProperty = BindableProperty.Create<Page1 , string>(p => p.MainText, string.Empty);
public string MainText
{
get
{
return (string)GetValue(MainTextProperty);
}
set
{
SetValue(MainTextProperty, value);
}
}
}
公共部分类第1页:内容页
{
公共静态只读BindableProperty MainTextProperty=BindableProperty.Create(p=>p.MainText,string.Empty);
公共字符串主文本
{
得到
{
返回(字符串)GetValue(MainTextProperty);
}
设置
{
SetValue(MainTextProperty,value);
}
}
}
在XAML中,我有以下代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.Pages.Page1">
<Grid HorizontalOptions="Fill" VerticalOptions="Fill">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Text="{Binding MainText}" Grid.Row="0"/>
<Label Text="{Binding MainText}" Grid.Row="1"/>
<Label Text="{Binding MainText}" Grid.Row="2"/>
<ctrl:CustomControl CustomProperty="{Binding MainText}" />
<Label Text="{Binding MainText}" Grid.Row="3"/>
</Grid>
</ContentPage>
在自定义控件中,是这样的:
<StackLayout>
<Label x:Name="lbl1" />
</StackLayout>
在代码隐藏中,我使用OnPropertyChanged设置CustomProperty和lbl1之间接收的值。CustomProperty的定义方式与我定义MainTextProperty的方式相同
然而,这是行不通的。我寻找任何一种能让我做类似事情的例子,但我没有找到。你们有谁知道我是如何做到这一点的?我收到传递的值与Xamarin.Forms.Binding之间类型不匹配的问题。是否忘记添加
BindingContext
?我在下面试过了,效果很好
public Page1 ()
{
InitializeComponent ();
BindingContext = this;
MainText = "123";
}
我看到您的类被命名为Page1
,那么BindableProperty
中的申报人也应该是Page1
而不是主页
public static readonly BindableProperty MainTextProperty = BindableProperty.Create<Page1, string>(p => p.MainText, string.Empty);
public静态只读BindableProperty MainTextProperty=BindableProperty.Create(p=>p.MainText,string.Empty);
另一种方法是使用x:Reference。在这种情况下,不更新页面的数据上下文
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.Page1"
x:Name="root">
<Grid HorizontalOptions="Fill" VerticalOptions="Fill">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Text="{Binding MainText}" Grid.Row="0"/>
<Label Text="{Binding MainText}" Grid.Row="1"/>
<Label Text="{Binding MainText}" Grid.Row="2" BindingContext="{x:Reference Name=root}"/>
<Label Text="{Binding MainText}" Grid.Row="3"/>
</Grid>
</ContentPage>
我在这个问题上挣扎了一段时间,发现返回的绑定类型不是string,它应该是binding顺便说一句,具有泛型类型的BindableProperties已被弃用,因此您的代码应该是:
public partial class Page1 : ContentPage
{
public static readonly BindableProperty MainTextProperty = BindableProperty.Create("MainText",typeof(Binding), typeof(Page1),null);
public Binding MainText
{
get
{
return (Binding)GetValue(MainTextProperty);
}
set
{
SetValue(MainTextProperty, value);
}
}
}
正如您所说,我添加了绑定上下文,但是我收到了一个类型不匹配的异常。在这里,您可以在Xamarin中找到更多关于视图到视图绑定的信息?