天天看點

windows phone 頁面傳值(7)

在windows phone 中微軟為我們提供了頁面間傳遞參數的解決方案,下面就為大家介紹使用方法,頁面傳值的案例中我們建立兩個頁面,一個是MainPage另一個是SecondPage頁面;MianPage頁面的主要代碼為:

   <Grid x:Name="ContentPanel" Grid.Row="1" Background="Goldenrod" Margin="12,0,12,0"></Grid>

        <TextBlock x:Name="Navigation" Text="導航到第二個頁面" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" ManipulationStarted="Navigation_ManipulationStarted"></TextBlock>

    </Grid>

 MainPage 設定好顔色的效果圖:

windows phone 頁面傳值(7)

 從上面代碼可以看出我們為名為ContentPanel的Grid的屬性Background設定為一個金麒麟色,當點選名為Navigation的TextBlock元素時,把ContentPanel的Grid的屬性Background設定的顔色傳遞到第二個頁面去;Navigation的事件ManipulationStarted 的隐藏代碼為:

windows phone 頁面傳值(7)
windows phone 頁面傳值(7)

View Code

private void Navigation_ManipulationStarted(object sender, ManipulationStartedEventArgs e)

        {

            //獲得顔色ARGB值

            SolidColorBrush scb = (SolidColorBrush)ContentPanel.Background;

            Color c = scb.Color;

            //參數傳遞格式--知識點①

            Uri uri = new Uri("/SecondPage.xaml?Red="+c.R+"&Green="+c.G+"&Blue="+c.B, UriKind.Relative);

            this.NavigationService.Source = uri;

            e.Complete();

            e.Handled = true;

        }

 頁面需要傳遞的值設定好之後,會導航到第二個頁面SecondPage頁面,SecondPage.xaml檔案中的主要代碼為:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>

        <TextBlock x:Name="Navigation" Text="導航到第-個頁面" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" ManipulationStarted="Navigation_ManipulationStarted"></TextBlock>

    </Grid> 

 SecondPage頁面效果:

windows phone 頁面傳值(7)

 從上面代碼中看一看出我們并沒有名為ContentPanel的Grid的屬性Background設定顔色值,這裡我們會從隐藏檔案對其背景顔色進行設定,Navigation的事件ManipulationStarted 的隐藏代碼為:

windows phone 頁面傳值(7)
windows phone 頁面傳值(7)

View Code   //textblock的導航時間

        private void Navigation_ManipulationStarted(object sender, ManipulationStartedEventArgs e)

            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

windows phone 頁面傳值(7)

 OnNavigatedTo隐藏檔案的方法是:

windows phone 頁面傳值(7)
windows phone 頁面傳值(7)

View Code   //活動頁面構造函數之後--知識點②

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

            //以泛型集合的方式獲得傳遞過來數值--知識點③

            IDictionary<string, string> para = this.NavigationContext.QueryString;

            if (para.Count>0)

            {

                byte r = Byte.Parse(para["Red"]);

                byte b = Byte.Parse(para["Blue"]);

                byte g = Byte.Parse(para["Green"]);

                ContentPanel.Background=new SolidColorBrush(Color.FromArgb(255,r,g,b));

            }

此方法獲得從MainPage也傳遞類的參數,我們進行解析,根據解析到的結果設定 SecondPage中ContentPanel.Background屬性,這也就完成參數的傳遞了;

windows phone 頁面傳值(7)

這裡參數傳遞的格式是我們要注意的,參數是在相對URI後緊接着是問号(?),之後就是鍵值對的形式了(key=value),如果參數是多個,則需要用一個&符号隔開

windows phone 頁面傳值(7)
windows phone 頁面傳值(7)
windows phone 頁面傳值(7)

記住:參數傳遞的格式多個參數傳遞需要用&符号隔開;活動頁面接受參數時重寫的 OnNavigatedTo方法;this.NavigationContext.QueryString接受所有傳遞到此頁面的參數及其值,傳回一個字典容器

繼續閱讀