locked
how to navigate using a button to another page?

    Question

  • Hi

    i have a main page.inside of it i have a frame.

    now i want by pressing a button, navigate to another page. the second page should be appear in the same frame.

    it matters to me the back and forward buttons of brower should be enable

    thanks

    Saturday, September 3, 2011 4:19 AM

Answers

  • I have developed for similar case...

    In App i created a static method called LoadPage. On click of button in Page1 i called the method like,

    App.LoadPage(this, "/Page2");

    public static T GetLogicalParentOfType<T>(FrameworkElement element)
            {
                if (element != null)
                {
                    FrameworkElement parent = (FrameworkElement)element.Parent;
                    while (parent != null)
                    {
                        if (parent is T)
                            return (T)(object)parent;
    
                        parent = (FrameworkElement)parent.Parent;
                    }
                }
                return default(T);
            }
    
    public static MainPage GetMainPage(Page page)
            {
                MainPage obj = GetLogicalParentOfType<MainPage>(page);
                return obj;
            }
    
     public static void LoadPage(Page page, string newPage)
            {
                try
                {
                    MainPage obj = GetMainPage(page);
    
                    if (obj != null)
                    {
                        Uri uri = new Uri(newPage, UriKind.Relative);
                        obj.ContentFrame.Navigate(uri);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    
    
    
    Monday, September 5, 2011 12:34 AM
  • this code has error

    App.LoadPage(this, "/Page2");

    Error    2    Argument 1: cannot convert from 'Tahririeh.Login' to 'System.Windows.Controls.Page'


    I changed the Page type to FrameworkElement in those functions and it worked.

    thank you so much

    Monday, September 5, 2011 2:04 AM

All replies

  • In Main Page on click of the button. Here ContentFrame is the name of your Frame.

    Uri uri = new Uri("/PageName",UriKind.Relative);
    
    this.ContentFrame.Navigate(uri);
    Saturday, September 3, 2011 5:59 AM
  • i have a main page.inside of it i have a frame.

    now i want by pressing a button, navigate to another page. the second page should be appear in the same frame.

    Lets say ur pages are Page1.xaml and Page2.xaml and in page1.xaml, U have button. when user cliks button then try implementing the below line of code

    this.Content= new Page2();

    Hope this will help u

    Saturday, September 3, 2011 10:19 AM
  • hi

    your code will work if only the button and the frame were in the same page. but they aren't!!!

    i have a main page which has a contentFrame. and i have two views (page1 , page2 ).

    in the page1 view i have a button which when you click on it, i want to the page2 view load into ContentFrame of mainPage.

    thank you

    Saturday, September 3, 2011 11:54 PM
  • Lets say ur pages are Page1.xaml and Page2.xaml and in page1.xaml, U have button. when user cliks button then try implementing the below line of code

    this.Content= new Page2();

    Hope this will help u

    this code will work but browser buttons will not be enabled

    Saturday, September 3, 2011 11:55 PM
  • I have developed for similar case...

    In App i created a static method called LoadPage. On click of button in Page1 i called the method like,

    App.LoadPage(this, "/Page2");

    public static T GetLogicalParentOfType<T>(FrameworkElement element)
            {
                if (element != null)
                {
                    FrameworkElement parent = (FrameworkElement)element.Parent;
                    while (parent != null)
                    {
                        if (parent is T)
                            return (T)(object)parent;
    
                        parent = (FrameworkElement)parent.Parent;
                    }
                }
                return default(T);
            }
    
    public static MainPage GetMainPage(Page page)
            {
                MainPage obj = GetLogicalParentOfType<MainPage>(page);
                return obj;
            }
    
     public static void LoadPage(Page page, string newPage)
            {
                try
                {
                    MainPage obj = GetMainPage(page);
    
                    if (obj != null)
                    {
                        Uri uri = new Uri(newPage, UriKind.Relative);
                        obj.ContentFrame.Navigate(uri);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    
    
    
    Monday, September 5, 2011 12:34 AM
  • thanks arya009. it seems will work. i'll test it

    Monday, September 5, 2011 1:47 AM
  • this code has error

    App.LoadPage(this, "/Page2");

    Error    2    Argument 1: cannot convert from 'Tahririeh.Login' to 'System.Windows.Controls.Page'


    Monday, September 5, 2011 1:56 AM
  • this code has error

    App.LoadPage(this, "/Page2");

    Error    2    Argument 1: cannot convert from 'Tahririeh.Login' to 'System.Windows.Controls.Page'


    I changed the Page type to FrameworkElement in those functions and it worked.

    thank you so much

    Monday, September 5, 2011 2:04 AM
  • I guess Tahririeh.Login is a user control. In my case i used pages so it worked without any problem..

    Glad you are able to solve your issue :)

    Monday, September 5, 2011 2:16 AM