我想让ViewModel成为View类的一部分(它本身是基于xaml的)。我使用的框架是Xamarian.Forms。
现在,我尝试在xaml中对根对象执行x:Name操作,然后将绑定上下文设置为按名称引用它。
MainPage.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:App"
x:Class="App.MainPage"
x:Name="MainPageRoot">
<Label
BindingContext="{x:Reference Name=MainPageRoot}"
Text="{Binding Path=LabelText}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</ContentPage>我在MainPage.xaml.cs中添加了数据
namespace App
{
public partial class MainPage : ContentPage
{
public string LabelText;
public MainPage()
{
LabelText = "Wow, this works";
InitializeComponent();
}
}
}但是标签仍然是空的。
为什么这不起作用?如何使用this中的属性
发布于 2016-09-05 00:01:10
问题是,LabelText属性没有定义getter,下面的代码可以工作:
public partial class MainPage : ContentPage
{
public string LabelText { get; }
public MainPage()
{
LabelText = "Wow, this works";
InitializeComponent();
}
}https://stackoverflow.com/questions/39318642
复制相似问题