【发布时间】:2016-09-04 15:39:36
【问题描述】:
我想让 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 的属性?
【问题讨论】:
标签: xaml xamarin.forms