Windows Phone 8.1 Hub 模板项目 DataContext 绑定
Windows Phone 8.1 Hub Template Project DataContext Binding
这可能是一个愚蠢的问题,但我无法在我自己的 Windows Phone 应用程序中重新创建 "Hub App" 模板使用的中心页面数据绑定方法。
我的 XAML 绑定到一个视图模型 class ,它被定义为我的 Page 对象的 public 属性 并且只要我包含它就可以正常工作该行:
this.DataContext = *viewmodel object here*
在 OnNavigatedTo() 方法中。
如果我注释掉这一行,运行时不会加载任何数据。这听起来很明显但是(这是我的问题),"Hub App" 模板从不将对象分配给任何 .xaml.cs 文件中的 "this.DataContext"。绑定只在 XAML 中定义过。我错过了什么?
更新:添加了 xaml 和 xaml.cs
XAML
<Page
DataContext="{Binding Subject, RelativeSource={RelativeSource Mode=Self}}"
d:DataContext="{d:DesignData Source=SampleData/SubjectSampleData.xaml}">
XAML.CS
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
this.InitializeComponent();
}
public Subject Subject { get; set; }
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null)
{
var subjectName = e.Parameter as string;
var subject = App.MainViewModel.Subjects.Single(item => item.Name == subjectName);
if (subject != null)
{
this.Subject = subject;
}
//this.DataContext = this.Subject;
}
}
}
HubApp 模板定义内部绑定 xaml:
<Page x:Class="App1.HubPage"
<!-- some namespaces -->
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
<!-- rest of code -->
这与您在构造函数中输入的效果相同:this.DataContext = DefaultViewModel;
。
编辑 - 评论后
您的情况略有不同 - 您绑定到 'normal' 属性 而没有 INotifyPropertyChanged、ICollectionChanged 或其他。查看模板,您会发现那里的绑定定义为 ObservableDictionary - 当项目是来自此类词典的 added/removed 时,它会引发合适的事件,允许更新 UI.在你的情况下没有这样的地方。
你的程序是这样工作的——当页面被创建时,你已经绑定了 DataContext,你的 属性 getter 被调用(放在那里 Debug.WriteLine("Getter");
)但是还没有任何东西在您的 属性 中,因此 UI 是空的。然后稍后调用 OnNavigatedTo(放在那里 Debug.WriteLine("Navigation event");
),您在其中填充 属性,但是 UI 没有收到通知,所以没有更新。
这可能是一个愚蠢的问题,但我无法在我自己的 Windows Phone 应用程序中重新创建 "Hub App" 模板使用的中心页面数据绑定方法。
我的 XAML 绑定到一个视图模型 class ,它被定义为我的 Page 对象的 public 属性 并且只要我包含它就可以正常工作该行:
this.DataContext = *viewmodel object here*
在 OnNavigatedTo() 方法中。
如果我注释掉这一行,运行时不会加载任何数据。这听起来很明显但是(这是我的问题),"Hub App" 模板从不将对象分配给任何 .xaml.cs 文件中的 "this.DataContext"。绑定只在 XAML 中定义过。我错过了什么?
更新:添加了 xaml 和 xaml.cs
XAML
<Page
DataContext="{Binding Subject, RelativeSource={RelativeSource Mode=Self}}"
d:DataContext="{d:DesignData Source=SampleData/SubjectSampleData.xaml}">
XAML.CS
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
this.InitializeComponent();
}
public Subject Subject { get; set; }
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null)
{
var subjectName = e.Parameter as string;
var subject = App.MainViewModel.Subjects.Single(item => item.Name == subjectName);
if (subject != null)
{
this.Subject = subject;
}
//this.DataContext = this.Subject;
}
}
}
HubApp 模板定义内部绑定 xaml:
<Page x:Class="App1.HubPage"
<!-- some namespaces -->
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
<!-- rest of code -->
这与您在构造函数中输入的效果相同:this.DataContext = DefaultViewModel;
。
编辑 - 评论后
您的情况略有不同 - 您绑定到 'normal' 属性 而没有 INotifyPropertyChanged、ICollectionChanged 或其他。查看模板,您会发现那里的绑定定义为 ObservableDictionary - 当项目是来自此类词典的 added/removed 时,它会引发合适的事件,允许更新 UI.在你的情况下没有这样的地方。
你的程序是这样工作的——当页面被创建时,你已经绑定了 DataContext,你的 属性 getter 被调用(放在那里 Debug.WriteLine("Getter");
)但是还没有任何东西在您的 属性 中,因此 UI 是空的。然后稍后调用 OnNavigatedTo(放在那里 Debug.WriteLine("Navigation event");
),您在其中填充 属性,但是 UI 没有收到通知,所以没有更新。