[Windows 8 App]全域方式傳值

[Windows 8 App]全域方式傳值

在使用全域傳值方式中,需要再項目的App.xaml.cs中的App類別中宣告靜態成員的變數

由於在App類別中宣告的變數是以應用程式的全域變數存在,因此在各頁面都可以使用

也就是說,可以將需要傳遞給導航目標頁面的數據保存在這樣的靜態全域變數中

然後再由導航目標頁面來讀取,從而獲得傳遞的數據

下面透過範例來介紹全域方式傳值

 

首先,新增一個專案,名稱命名為【TransferData】

開啟【App.xaml.cs】,然後在App類別中聲明二個字串類型的全域變數

 

sealed partial class App : Application
{
    public static string UserName;
    public static string UserAge;
    
    //....
    //....
    //....
    //....
}

 

主要是這二行

public static string UserName;
public static string UserAge;

 

宣告完畢後,開啟【MainPage.xaml】

這是【MainPage.xaml】完整程式碼,只是一些簡單的屬性,所以我們就不做詳解了!!

 

<Page
    x:Class="TransferData.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TransferData"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="註冊訊息頁面" FontSize="70" VerticalAlignment="Top" Margin="457,260,0,0" Height="116" Width="456"/>
        <TextBlock HorizontalAlignment="Left" Margin="457,381,0,0" TextWrapping="Wrap" FontSize="40" Text="姓名:" VerticalAlignment="Top" Height="55" Width="136"/>
        <TextBlock HorizontalAlignment="Left" Margin="457,486,0,0" TextWrapping="Wrap" FontSize="40" Text="年齡:" VerticalAlignment="Top"/>
        <TextBox x:Name="InputName" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" FontSize="40" VerticalAlignment="Top" Margin="598,381,0,0" Height="55" Width="315"/>
        <TextBox x:Name="InputAge" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" FontSize="40" VerticalAlignment="Top" Margin="598,479,0,0" Height="55" Width="315"/>
        <Button x:Name="SubmitDatabutton" Content="送出" FontSize="50" HorizontalAlignment="Left" Margin="752,604,0,0" VerticalAlignment="Top" Height="74" Width="161" Click="SubmitDatabutton_Click"/>

    </Grid>
</Page>

 

【MainPage.xaml】的設計畫面:

291

 

完成【MainPage.xaml】的設計後,新增一個頁面【ShowPage.xaml】

然後,開啟【ShowPage.xaml】,輸入以下完整程式碼,只是一些簡單的控制項屬性,所以不多做詳解!!

<Page
    x:Class="TransferData.ShowPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TransferData"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock HorizontalAlignment="Left" FontSize="70" TextWrapping="Wrap" Text="顯示訊息頁面" VerticalAlignment="Top" Margin="447,241,0,0" Height="104" Width="448"/>
        <TextBlock x:Name="ShowName" FontSize="70"  HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="447,373,0,0" Height="134" Width="684"/>
        <TextBlock x:Name="ShowAge" FontSize="70" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="447,528,0,0" Height="134" Width="684"/>

    </Grid>
</Page>

 

顯示頁面完畢後,開啟【MainPage.xaml.cs】,在SubmitDatabutton的Click事件中輸入以下程式碼:

private void SubmitDatabutton_Click(object sender, RoutedEventArgs e)
{
   App.UserName = InputName.Text;
   App.UserAge = InputAge.Text;
   Frame.Navigate(typeof(ShowPage));
}

在上面的程式碼中,將所輸入的訊息保存到宣告的UserName和UserAge

並使用Frame類別的Navigate方法導航到ShowPage顯示訊息

 

再來開啟【ShowPage.xaml.cs】,在onNavigatedTo方法中分別讀取UserName和UserAge值

然後並將所讀取到的值分別顯示到所對應的TextBlock文字方塊中

下面是程式碼的部分:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
   ShowName.Text = "姓名: " + App.UserName;
   ShowAge.Text = "年齡: " + App.UserAge;
}

 

這是執行結果畫面:

輸入畫面

293

 

顯示畫面

294