天天看點

Windows phone 7之頁面導航

首先說一下WP7程式的呈現主體與關系,直接呈現給使用者的界面是頁面Page,每個Page是繼承自PhoneApplicationPage的類,一個程式可以包含任意多個的Page頁面,這些Page頁面被放在一個共同的Frame下,Frame繼承自PhoneApplicationFrame 。一個應用程式隻能有一個Frame,建立程式時自動生成的App.xaml.cs檔案中,有關于程式Frame初始化的代碼,在InitializePhoneApplication()方法中,如下

// Do not add any additional code to this method
        private void InitializePhoneApplication()
        {
if (phoneApplicationInitialized)
return;

// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
            RootFrame = new PhoneApplicationFrame();
            RootFrame.Navigated += CompleteInitializePhoneApplication;

// Handle navigation failures
            RootFrame.NavigationFailed += RootFrame_NavigationFailed;

// Ensure we don't initialize again
            phoneApplicationInitialized = true;
        }      

可以看到,注釋提到,不要添加對于的代碼到該方法中,是以我們也不要這麼做。

當我們改變某個Page的屬性時不會影響其他頁面,但是一旦改變了Frame,那就會影響所有的頁面,是以,我們應該認為Frame是隻讀的(雖然不是)。

App類是程式主類,我沒刻意用它來操作全局的屬性,比如擷取Frame,就是RootFrame屬性。

頁面導航的方法

頁面導航一般分為兩種方法,代碼導航,和XAML聲明導航

1、代碼實作

NavigationService類提供了導航的功能,NavigationService.Navigate(new Uri("/NewPage.xaml", UriKind.Relative));将這段代碼放到Button點選事件中,點選Button就會跳轉到NewPage.xaml頁面,其中naviagte接受一個Uri類型的參數,這裡傳入字元串路徑和路徑類型UriKind,UriKind是個枚舉型,一邊頁面導航是相對路徑。WP7的特點的從根目錄起,"/"開頭表示根目錄,依次輸入檔案路徑,NewPage.xaml檔案放在了根目錄下,是以路徑寫為"/NewPage.xaml", 如果NewPage.xaml在根目錄的View檔案夾下,則路徑為"/View/NewPage.xaml"。

2、XAML實作

可以利用導航控件如 HyperlinkButton,寫法<HyperlinkButton NavigateUri="/NewPage.xaml" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />,NavigateUri屬性用來設定路徑。

上述兩種方式達到的目的是一樣的,點選Button或HyperlinkButton就會跳轉到NewPage.xaml

Windows phone 7之頁面導航
Windows phone 7之頁面導航

WP7頁面導航可以使用路徑别名,也是從Silverlight繼承過來的

首先在App.xaml的檔案上注冊Windows.Navigation 命名空間,代碼:mlns:navigate="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone",

然後注冊資源代碼如下:

<Application.Resources>
        <navigate:UriMapper x:Key="UriMapper">
            <navigate:UriMapping Uri="NewPage" MappedUri="/NewPage.xaml"/>
        </navigate:UriMapper>
    </Application.Resources>      

最後給Frame添加UriMapper :this.RootFrame.UriMapper = Resources["UriMapper"] as UriMapper; 将這句代碼寫到App類的構造函數裡即可。

使用方式

NavigationService.Navigate(new Uri("NewPage", UriKind.Relative));

<HyperlinkButton NavigateUri="NewPage" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />

路徑中直接填别名就可以了,運作效果還是一樣的。

頁面之間傳遞參數

頁面傳遞參數和網頁中傳遞參數的方式一樣,在路徑的後面加上"?參數名=參數值"

NavigationService.Navigate(new Uri("/NewPage.xaml?ID=10", UriKind.Relative));

<HyperlinkButton NavigateUri="/NewPage.xaml?ID=10" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />

這樣就導航到了NewPage.xaml并将ID及其值傳遞到了NewPage.xaml中。

在NewPage.xaml中接受參數的方法

NavigationContext的QueryString屬性存儲了上個頁面中傳遞的所有參數下面代碼擷取ID并用MessageBox顯示其值

if (NavigationContext.QueryString.ContainsKey("ID"))
            {
                MessageBox.Show(NavigationContext.QueryString["ID"]);
            }      

導航中使用了别名的話也可以将參數傳遞變得簡單,修改資源為

<Application.Resources>
        <navigate:UriMapper x:Key="UriMapper">
            <navigate:UriMapping Uri="NewPage" MappedUri="/NewPage.xaml"/>
            <navigate:UriMapping Uri="NewPage/{param}" MappedUri="/NewPage.xaml?ID={param}"/>
        </navigate:UriMapper>
    </Application.Resources>      

這樣通過

NavigationService.Navigate(new Uri("NewPage/10", UriKind.Relative));

<HyperlinkButton NavigateUri="NewPage/10" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />

就可以将ID=10傳遞到NewPage.xaml,

由于定義了兩個導航到UriMapping,表現方式不一樣,參數傳遞方世也不一樣,加上預設不使用别名的方式,給Newpage.xaml傳遞參數,路徑可以有三種寫法

"/NewPage.xaml?ID=10“,"NewPage?ID=10“,"NewPage/ID=10“,效果一樣。

頁面傳遞參數是頁面之間共享資料的一種方式,還有兩種比較建立的資料共享方法,一種是全局變量,比如使用靜态類,和APP類的靜态成員,這種方法比較簡單,會C#的童鞋都會使用,還有一種方式是使用IsolatedStorage類,這也是內建自Silverlight,叫做獨立存儲。獨立存儲在WP7程式的聲明周期和資料共享起到最重要的作用,沒有之一,是以,無論如何也要掌握,我将之留到介紹WP7程式的聲明周期時詳細講。這裡隻寫一下他最簡單用法,不了解的童鞋别急。

在MainPage.xaml.cs中寫入

private void btnNavigate_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
            iss["ID"] = 10;
            NavigationService.Navigate(new Uri("NewPage", UriKind.Relative));
        }      

在NewPage.xaml.cs中寫入

IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
if (iss.Contains("ID"))
            {
                MessageBox.Show(iss["ID"].ToString());
            }      

最終運作和NavigationService跳轉在路徑後面加參數效果是一樣的

Windows phone 7之頁面導航

我新浪微網誌的昵稱是"@馬蔬菜",多多關注,謝謝

轉載于:https://www.cnblogs.com/xiaogeer/archive/2012/04/06/2435375.html

c#