that_guy
that_guy

Reputation: 2445

How to refresh a page and content in Windows8 application

So I have a windows 8 app, in which I set some data to the defaultViewModel. My question is, after the page is created, and I add something to the data, how do I refresh the page and display the changes made?

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

The changes I make are not reflected untill the next time I open the application and the page is reloaded. Here is the view model:

    protected IObservableMap<String, Object> DefaultViewModel
    {
        get
        {
            return this.GetValue(DefaultViewModelProperty) as IObservableMap<String, Object>;
        }

        set
        {
            this.SetValue(DefaultViewModelProperty, value);
        }
    }

This is the list view I would like to update:

<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"/>

Bound to this:

<CollectionViewSource
    x:Name="itemsViewSource"
    Source="{Binding Items}"
    d:Source="{Binding AllGroups[0].Items, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}"/>

msdn function:

public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }

Upvotes: 4

Views: 5177

Answers (2)

Jerry Nixon
Jerry Nixon

Reputation: 31813

Here's my approach:

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

Best of luck!

Upvotes: 1

Akinwale
Akinwale

Reputation: 1055

Have a look at the BindableBase class code which is usually created as in the Common folder of your Windows 8 Store project if you use the templates (except the Blank App template). If your start point is a Blank App template, you can create a new BasicPage, and Visual Studio will ask if you would like to include the common files.

Basically, the idea goes like so:

  1. You implement the INotifyPropertyChanged interface
  2. Create a custom PropertyChanged event
  3. At the point after you set a new value for the property, you call this.OnPropertyChanged(propertyName) which will emit the change event for that property. The control will then be notified that the property has changed, and will be automatically updated with the new value.

Upvotes: 3

Related Questions