如何在Windows8应用程序中刷新页面和内容

时间:2012-10-06 00:34:41

标签: c# windows-8 page-refresh

所以我有一个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));
            }
        }

2 个答案:

答案 0 :(得分:3)

如果您使用模板(Blank App模板除外),请查看BindableBase类代码,该代码通常在Windows 8 Store项目的Common文件夹中创建。如果您的起始点是空白应用程序模板,则可以创建新的BasicPage,Visual Studio将询问您是否要包含公用文件。

基本上,这个想法是这样的:

  1. 您实现了INotifyPropertyChanged接口
  2. 创建自定义PropertyChanged事件
  3. 在为属性设置新值后,您将调用this.OnPropertyChanged(propertyName),它将为该属性发出change事件。然后会通知控件属性已更改,并将使用新值自动更新。

答案 1 :(得分:1)

这是我的方法:

public bool Reload()
{
    if (!this.Frame.BackStack.Any())
        return false;
    var current = this.Frame.BackStack.First();
    this.Frame.BackStack.Remove(current);
    return this.Frame.Navigate(current.SourcePageType, current.Parameter);
}

祝你好运!

相关问题