【发布时间】:2017-11-18 06:37:40
【问题描述】:
假设我有一个非常基本的类,其中包含一些属性。例如:
public class MyClass
{
public MyClass()
{
Something = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";
OrOther = "Proin dignissim, nunc non tincidunt imperdiet, magna urna malesuada enim";
}
public string Something { get; set; }
public string OrOther { get; set; }
}
我想在 Xaml 中对此进行数据绑定,我该怎么做? 我试过直接绑定到对象,所以在我的 Xaml 页面代码后面:
public partial class MainPage : ContentPage
{
public MyClass anInstance;
public MainPage()
{
InitializeComponent();
anInstance = new MyClass();
BindingContext = this;
}
}
然后在我的 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"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<Label Text="{Binding anInstance.Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
我还尝试在父控件上设置 BindingContext:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<StackLayout BindingContext="anInstance">
<Label Text="{Binding Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
但这只是行不通。我也尝试将页面的 bindingcontext 设置为 anInstance:
public partial class MainPage : ContentPage
{
public MyClass anInstance;
public MainPage()
{
InitializeComponent();
anInstance = new MyClass();
BindingContext = anInstance;
}
}
并且只是绑定到它在 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"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<Label Text="{Binding Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</ContentPage>
但同样,我得到的只是空白页。 似乎至少其中一个应该起作用。 像这样绑定到自定义类的属性的推荐方法是什么?
编辑
加入@jason cmets,我也试过这个:
public MyClass anInstance
{
get
{
return _anInstance;
}
set
{
_anInstance = value;
}
}
private MyClass _anInstance { get; set; }
public MainPage()
{
InitializeComponent();
anInstance = new MyClass();
BindingContext = this;
}
还有 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"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<StackLayout BindingContext="anInstance">
<Label Text="{Binding Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Label Text="{Binding OrOther}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
但又是一个空白页...
结果相同:
private MyClass anInstance { get; set; }
public MainPage()
{
InitializeComponent();
anInstance = new MyClass();
BindingContext = this;
}
还有
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingTest"
x:Class="DataBindingTest.MainPage">
<StackLayout BindingContext="anInstance">
<Label Text="{Binding Something}"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Label Text="{Binding OrOther}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
【问题讨论】:
-
您的第一个示例不起作用,因为“anInstance”不是属性。您的最后一个示例应该可以工作。
-
显示的代码都不起作用。您的 anInstance 对象必须是一个属性。这意味着它必须声明为
public MyClass anInstance { get; set; }。 -
在您附加的两个示例中,您都在 XAML 和后面的代码中设置了 BindingContext。选择一个或另一个,而不是两者兼而有之。充其量是令人困惑的。我一般觉得在后面的代码中设置会更清楚。
-
另外,刚刚注意到您的 anInstance 属性是私有的 - 我认为它需要是公共的或受保护的
标签: xaml xamarin data-binding xamarin.forms