所以我有一个 Windows 8 应用程序,其中我将一些数据设置为 defaultViewModel。我的问题是,创建页面后,我向数据中添加了一些内容,如何刷新页面并显示所做的更改?
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
//inital load
var DataGroups = SampleDataSource.GetGroups((String)navigationParameter);
this.DefaultViewModel["Items"] = DataGroups;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//when the page is navigated back to after making changes to sampledatasource
base.OnNavigatedTo(e);
this.DefaultViewModel.Clear();
var DataGroups = SampleDataSource.GetGroups("AllGroups");
this.DefaultViewModel["Items"] = DataGroups;
}
直到下次我打开应用程序并重新加载页面时,我所做的更改才会反映出来。这是视图模型:
protected IObservableMap<String, Object> DefaultViewModel
{
get
{
return this.GetValue(DefaultViewModelProperty) as IObservableMap<String, Object>;
}
set
{
this.SetValue(DefaultViewModelProperty, value);
}
}
这是我要更新的列表视图:
<ListView
x:Name="itemListView"
AutomationProperties.AutomationId="ItemsListView"
AutomationProperties.Name="Items"
TabIndex="1"
Grid.Row="1"
Visibility="Collapsed"
Margin="0,-10,0,0"
Padding="10,0,0,60"
ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
ItemTemplate="{StaticResource Standard80ItemTemplate}"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick"/>
绑定到这个:
<CollectionViewSource
x:Name="itemsViewSource"
Source="{Binding Items}"
d:Source="{Binding AllGroups[0].Items, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}"/>
msdn 功能:
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}