none
How to refresh a page that is navigate to when a Ribbon control is pressed RRS feed

  • Question

  •  My app has a ribbon control. From the ribbon control, I have a button that when pressed will cause a navigation to a page in a frame control. The ribbon is always visible and accessible. The handler that is called when the ribbon control is pressed calls the Navigate method with a pack URI with the page to navigate to.

    My problem is that while the page is displayed in the frame, if I press the ribbon control button again, the page now shows up with all controls empty (no data is displayed). I put a breakpoint in the page constructor and in the Loaded handler and neither of these get called in this situation.

    If I click on the navigation back arrow and then the forward arrow, then the Loaded handler is called.

    How can I reload the contents of the page when I press the Ribbon control while the page is currently opened in the frame?
    Tuesday, October 7, 2008 5:44 PM

Answers

  • If you want to refresh the current page, just simply call Frame.Refresh() method as follows:

    private Uri source = new Uri("CalledPageFunction.xaml", UriKind.Relative);
    private void ObButtonClick_CallPage(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        int i = DataManager.myDoc.IntVal;
        DataManager.myDoc.IntVal = ++i;
        button.Content = DataManager.myDoc.IntVal;
        if (source == this.MainFrame.CurrentSource)
        {
            this.MainFrame.Refresh();
        }
        else
        {
            this.MainFrame.Navigate(source);
        }
    }

    Hope this helps
    Monday, October 13, 2008 4:04 AM

All replies

  •  Please, need help with this. I have looked thoughout the internet, documentation, samples and I haven't find any solution for my problem.
    Thursday, October 9, 2008 1:12 PM
  • -> My problem is that while the page is displayed in the frame, if I press the ribbon control button again, the page now shows up with all controls empty (no data is displayed).

    This is weird, could you please provide small, complete and ready-to-run example to demonstrate the issue you are encountering?

    Thanks
    Friday, October 10, 2008 8:07 AM
  • This is the smallest app I could do. I took a msdn sample and modified it to show what I am seeing. This has nothing to do with my real app. This is just to reproduce the issue. If you run this application, You will see the home page and in the home page there is a Frame that startsup as CallingPage. Press the button "Call Page 2" and it will move to CalledPageFunction. Notice that the back button in the navigation UI is now enabled. The data shown in the text box has a counter that should be incremented every time the "Call Page 2" button is pressed. If you click the back arrow to go back to Page 1 and click the button, it will move to Page 2 and then shown the value incremented. Now, instead of pressing the back button to go back to Page 1, just keep pressing the "Call Page 2" button. Notice that the value in the text box no longer shows the incremented value. This is because the Loaded event is not being sent when the Navigate method is called on a URI that is the current URI in the Journal. How can I get this to refresh by pressing the "Call Page 2" button without having to navigate back to page 1??

    File: App.xaml

    <Application

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    x:Class="StructuredNavigationSample.App"

    StartupUri="HomePage.xaml" />

    File: App.xaml.cs

    using
    System;

    using System.Windows;

    namespace StructuredNavigationSample

    {

    public partial class App : Application

    {

    private MyDocument statdoc;

    public MyDocument StatDoc

    {

    get

    {

    return statdoc;

    }

    }

    public App()

    {

    statdoc = new MyDocument();

    }

    }

    }

    File: CalledPageFunction.xaml
    <Page

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:sys="clr-namespace:System;assembly=mscorlib"

    x:Class="StructuredNavigationSample.CalledPageFunction"

    Title="Page Function"

    WindowWidth="250" WindowHeight="150"

    Loaded="Page_Loaded">

    <Grid Margin="10">

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width="Auto" />

    <ColumnDefinition />

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

    <RowDefinition/>

    <RowDefinition Height="Auto"/>

    <RowDefinition />

    </Grid.RowDefinitions>

    <Label Grid.Column="0" Grid.Row="0">This is Page 2</Label>

    <!-- Data -->

    <Label Grid.Column="0" Grid.Row="1">DataItem1:</Label>

    <TextBox Grid.Column="1" Grid.Row="1" Name="dataItem1TextBox"></TextBox>

    <!-- Accept/Cancel buttons -->

    <TextBlock Grid.Column="1" Grid.Row="2" HorizontalAlignment="Right">

    <Button Name="okButton" IsDefault="True" MinWidth="50">OK</Button>

    <Button Name="cancelButton" IsCancel="True" MinWidth="50">Cancel</Button>

    </TextBlock>

    </Grid>

    </Page>

    File: CalledPageFunction.xaml.cs
    using System; // String

    using System.Windows; // RoutedEventArgs, RoutedEventHandler

    using System.Windows.Navigation; // PageFunction

    using System.Windows.Controls;

    namespace StructuredNavigationSample

    {

    public partial class CalledPageFunction : Page

    {

    public CalledPageFunction()

    {

    InitializeComponent();

    this.dataItem1TextBox.Text = "Constructor called";

    }

    public CalledPageFunction(string initialDataItem1Value)

    {

    InitializeComponent();

    // Set initial value

    this.dataItem1TextBox.Text = initialDataItem1Value;

    }

    private void Page_Loaded(object sender, RoutedEventArgs e)

    {

    int counter = DataManager.GetValue();

    this.dataItem1TextBox.Text = "Loaded: " + counter.ToString();

    }

    }

    }

    File: CallingPage.xaml
    <Page

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    x:Class="StructuredNavigationSample.CallingPage"

    WindowTitle="Calling Page"

    WindowWidth="500" WindowHeight="500">

    <StackPanel>

    <Label Grid.Column="0" Grid.Row="0">This is Page 1</Label>

    </StackPanel>

    </Page>

    File: CallingPage.xaml.cs

    using System.Windows.Controls; // Page

    using System;

    namespace StructuredNavigationSample

    {

    public partial class CallingPage : Page

    {

    public CallingPage()

    {

    InitializeComponent();

    }

    }

    }


    File: Document.cs

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace StructuredNavigationSample

    {

    public class MyDocument

    {

    private int counter;

    public MyDocument()

    {

    DataManager.myDoc = this;

    }

    protected internal int IntVal { get { return counter; } set { counter = value; } }

    public void Clear()

    {

    counter = 0;

    }

    }

    public static class DataManager

    {

    public static MyDocument myDoc

    {

    get;

    internal set;

    }

    public static void SetValue(int i)

    {

    myDoc.IntVal = i;

    }

    public static int GetValue()

    {

    return myDoc.IntVal;

    }

    }

    }



    File: HomePage.xaml
    <Page

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    x:Class="StructuredNavigationSample.HomePage"

    WindowTitle="Home Page"

    WindowWidth="500" WindowHeight="500">

    <StackPanel>

    <Label Grid.Column="0" Grid.Row="0">Home Page</Label>

    <Frame Name="MainFrame" JournalOwnership="OwnsJournal" NavigationUIVisibility="Visible" Width="300" Height="300" BorderBrush="Black" BorderThickness="2" Source="CallingPage.xaml">

    </Frame>

    <Button HorizontalAlignment="Center" Width="Auto" Height="Auto" Grid.Column="0" Margin="0,10,15,10" Click="ObButtonClick_CallPage">

    <StackPanel Orientation="Horizontal">

    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Call Page 2" FontSize="20" Foreground="#003399" FontFamily="Verdana"/>

    </StackPanel>

    </Button>

    </StackPanel>

    </Page>


    File: HomePage.xaml.cs

    using System.Windows; // RoutedEventArgs, RoutedEventHandler, Visibility

    using System.Windows.Controls; // Page

    using System.Windows.Navigation; // ReturnEventArgs

    using System;

    namespace StructuredNavigationSample

    {

    public partial class HomePage : Page

    {

    public HomePage()

    {

    InitializeComponent();

    DataManager.myDoc.Clear();

    }

    private void ObButtonClick_CallPage(object sender, RoutedEventArgs e)

    {

    int i = DataManager.myDoc.IntVal;

    DataManager.myDoc.IntVal = ++i;

    this.MainFrame.Navigate(new Uri("CalledPageFunction.xaml", UriKind.Relative));

    }

    }

    }


    Friday, October 10, 2008 8:08 PM
  • If you want to refresh the current page, just simply call Frame.Refresh() method as follows:

    private Uri source = new Uri("CalledPageFunction.xaml", UriKind.Relative);
    private void ObButtonClick_CallPage(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        int i = DataManager.myDoc.IntVal;
        DataManager.myDoc.IntVal = ++i;
        button.Content = DataManager.myDoc.IntVal;
        if (source == this.MainFrame.CurrentSource)
        {
            this.MainFrame.Refresh();
        }
        else
        {
            this.MainFrame.Navigate(source);
        }
    }

    Hope this helps
    Monday, October 13, 2008 4:04 AM
  • That works great!!! Thank you so much for your help.
    • Edited by zahorius Monday, October 13, 2008 12:58 PM
    Monday, October 13, 2008 12:48 PM
  • 10 years old and still very helpful.
    Sunday, November 25, 2018 6:50 PM