先看看怎么把一个page 搞成启动业 页
- 在 onLanched 事件里面把,
Window.Current.Content = _rootFrame; Window.Current.Activate(); var _rootFrame = new Frame(); _rootFrame.Navigate(typeof(ItemsPage), sampleData.ItemGroups); ///首先是设置 Window.Current.Content属性,然后.Activate();
然后看看ItemsPage 里面有什么 ,看继承关系吧
- public sealed partial class ItemsPage : Application6.Common.LayoutAwarePage
-
[Windows.Foundation.Metadata.WebHostHidden]
public class LayoutAwarePage : Page///还是从page继承,那就看看他们为什么会有LayoutAwarePage,这个类 - 在new ItemsPage()之前会先new LayoutAwarePage(),也就是先跑到LayoutAwarePage这个里面的构造函数和字段;因为继承关系
- LayoutAwarePage从page 继承的DataContext 是public 的, this.DataContext = _defaultViewModel;,这样就给从LayoutAwarePage继承的page 帮顶了数据上下文
- 在ItemPage 里面,调用这个
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.DefaultViewModel["Items"] = e.Parameter;
}给itemPage 设置datacontext,因为itempage 的datacontext继承自LayoutAwarePage,而且是在指向的_defaultViewModel,引用,当_defaultViewModel只想的对象发生变化的时候,为itempage 的datacontext也会跟着发生变化那为什么LayoutAwarePage 要有
protected IObservableMap<String, Object> DefaultViewModel
{
get
{
return _defaultViewModel;
}
}这个属性呢, 看看它的注视吧
/// <summary>
/// Gets an implementation of <see cref="IObservableMap<String, Object>"/> set as the
/// page's default <see cref="DataContext"/>. This instance can be bound and surfaces
/// property change notifications making it suitable for use as a trivial view model.
/// </summary>默认为每个继承自LayoutAwarePage 设置了datacontext, 这只是为试用的ViewMoDEL,如果想改变的话
this.DefaultViewModel["Items"] = e.Parameter;,设置不同的string 和object ,就行了
8 ItemsPage里面有个CollectionViewSource,对象,,CollectionViewSource。SOURCE ,通过绑定到this.DefaultViewModel["Items"] ,引用到e.Parameter
<!-- Collection of items displayed by this page -->
<CollectionViewSource
x:Name="itemsViewSource"
Source="{Binding Items}"
d:Source="{Binding ItemGroups, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}"/>
然后ItemsPage里的control 就可以 绑定到CollectionViewSource