Windows Phone 8如何导航到一个页面实例,加载了新的UserControls?

时间:2014-05-01 05:44:02

标签: windows-phone-8 user-controls custom-controls

我是窗口手机应用程序的新手,并试图找出如何实现以下场景:

我有一个包含两个边框的BasePage,我将把不同的视图加载到。

我希望通过重用BasePage在整个应用程序中维护相同的UI。我知道可以创建一个BasePage实例并填充视图。我只是不知道如何导航到这个新实例。

BasePage Page1 = new BasePage();
Page1.TopSection.Child = new View1();
Page1.BottomSection.Child = new View2();

//How do I Navigate to  Page1?
--------------------------
|      Top Section       |
|      (load view 1)     |
|                        |
|                        |
--------------------------
|     Bottom Section     |
|      (load view 2)     |
|                        |
--------------------------
|          [ Next Page ] |
-------------------------- 
          BasePage Page2 = new BasePage();
          Page2.TopSection.Child = new View3();
          Page2.BottomSection.Child = new View4();

          //How do I Navigate to  Page2?
          --------------------------
          |      Top Section       |
          |      (load view 3)     |
          |                        |
          |                        |
          --------------------------
          |     Bottom Section     |
          |      (load view 4)     |
          |                        |
          --------------------------
          |          [ Next Page ] |
          --------------------------

1 个答案:

答案 0 :(得分:0)

您可以导航到Uri而不是Page实例。您可以尝试以另一种方式完成此要求。导航到BasePage Uri并传递查询字符串参数,以指示应如何自定义BasePage。然后在BasePage根据收到的查询字符串参数进行自定义:

//for example, to navigate to Page2 :
NavigationService.Navigate(new Uri("/BasePage.xaml?page=page2", UriKind.Relative));

//on Loaded event :
public BasePage_Loaded()
{
    SetupView();
}

public void SetupView()
{
    string page;
    if(NavigationContext.QueryString.TryGetValue("page", out page))
    {  
        switch (page)
        {
            case "page1" :
                ....... 
                break;
            case "page2" :
                this.TopSection.Child = new View3();
                this.BottomSection.Child = new View4();
                break;
            ....... 
        }
    }
}