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

C# 如何在Windows8应用程序中刷新页面和内容,c#,windows-8,page-refresh,C#,Windows 8,Page Refresh,因此,我有一个Windows8应用程序,其中我将一些数据设置为defaultViewModel。我的问题是,在创建页面并向数据中添加内容后,如何刷新页面并显示所做的更改 protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) { //inital load var Dat

因此,我有一个Windows8应用程序,其中我将一些数据设置为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;           
    }

查看BINDablBASE类代码,如果使用模板(除了空白应用程序模板),通常会在Windows 8存储项目的公用文件夹中创建该类代码。如果您的起点是一个空白的应用程序模板,您可以创建一个新的BasicPage,VisualStudio将询问您是否希望包含公共文件

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

  • 您可以实现INotifyPropertyChanged接口
  • 创建自定义PropertyChanged事件
  • 在为属性设置新值后,调用此.OnPropertyChanged(propertyName),它将为该属性发出更改事件。然后将通知控件该属性已更改,并将使用新值自动更新该控件
  • 以下是我的方法:

    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);
    }
    

    祝你好运

    在深入研究INotifyPropertyChanged之后,它作为一个接口出现了很多,但我不知道如何在这个实例中使用它。。。不确定它是否正确。好吧,在进一步挖掘之后,我发现了一些关于数据绑定的Microsoft文档。。。但仍然不确定如何将其应用于代码。对于#1,我在哪里实现INotifyPropertyChanged接口?BindableBase已经实现了这一点,我更改数据的地方是密封的类,我不确定我是否在遵循。我已经更新了main post以反映我要更新的列表视图。查看您的代码,您的列表视图似乎绑定到名为itemsViewSource的静态资源。这是如何定义的?此外,您不必使用BindableBase。您可以使密封类实现INotifyPropertyChanged,并遵循相同的逻辑。您所要做的就是在对视图模型进行任何更改后引发PropertyChanged事件。itemsViewSource刚刚绑定到一个collectionViewSource(xaml),它内部绑定到DefaultViewModel[“Items”],所以我想我需要为此创建INotifyPropertyChanged接口?如果是这样,我是否应该传入NotifyPropertyChanged(DefaultViewModel[“Items”])?或者将其作为字符串,因为msdn有一个传递字符串的函数(请参阅main post),您应该传入属性的实际名称。在您的例子中,这将是“DefaultViewModel”。好吧,我实现了这个:公共密封的部分类ItemsPage:My_App.Common.LayoutWarePage,INotifyPropertyChanged,然后调用NotifyPropertyChanged(“DefaultViewModel”);当它在更改后被导航回,但仍然没有更新时…很高兴在这里看到您:)
    <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}}"/>
    
    public void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this,
                        new PropertyChangedEventArgs(propertyName));
                }
            }
    
    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);
    }