Windows Phone開發之路(16) 如何在頁面間傳遞數據


  這一篇文章要解決的問題是如何從源頁面傳遞數據到目標頁面。其實Windows Phone已經為我們提供了一套解決方案,那就是查詢字符串。

  下面這個項目要實現的效果是:當從MainPage頁面導航到SecondPage時,該項目為SecondPage提供了MainPage當前的背景色,而且SecondPage也把自己初始化成這種顏色。這里傳遞的數據是背景顏色值。

  MainPage.xaml XAML代碼:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Navigate to Second Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  MainPage.xaml C#代碼:

namespace SilverlightPassData
{
public partial class MainPage : PhoneApplicationPage
{
Random rand = new Random();

// 構造函數
public MainPage()
{
InitializeComponent();
}

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//處理Manipulation事件
{
//todo:導航到Second Page頁面,並且傳遞頁面背景顏色信息給Second Page.
string destination = "/SecondPage.xaml";//創建一個String變量保存目標頁面地址

if (this.ContentPanel.Background is SolidColorBrush)//檢查能否成功轉換成SolidColorBrush類型。Background屬性為Brush類型
{
Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;//將Background屬性值轉換成SolidColorBrush類型並獲取顏色值
destination += string.Format("?Red={0}&Green={1}&Blue={2}",clr.R,clr.G,clr.B);//將顏色值作為參數附在目標地址后,類似於HTML查詢字符串格式
}

this.NavigationService.Navigate(new Uri(destination,UriKind.Relative));//導航到目標地址

e.Complete();//表示不再處理其它Manipulation事件
e.Handled = true;//將路由事件標記為已處理
}

protected override void OnManipulationStarted(ManipulationStartedEventArgs e)//重寫基類Control的虛方法
{
//todo:實現當單擊屏幕的時候隨機改變頁面背景色
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255,(byte)rand.Next(256),(byte)rand.Next(256),(byte)rand.Next(256)));

base.OnManipulationStarted(e);//基類訪問表達式調用基類方法
}
}
}

  

  SecondPage.xaml XAML代碼:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Go back to Main Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  SecondPage.xaml C#代碼:

namespace SilverlightPassData
{
public partial class SecondPage : PhoneApplicationPage
{
public SecondPage()
{
InitializeComponent();
}

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//處理Manipulation事件
{
//todo:返回或導航到MainPage頁面
this.NavigationService.GoBack();

e.Complete();
e.Handled = true;
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page的虛方法,當頁面變為框架的活動頁面時調用
{
//todo:獲取從MainPage傳遞過來的數據,並以此來初始化頁面背景顏色
IDictionary<string, string> parameters = this.NavigationContext.QueryString;//創建泛型接口字典來接受返回結果對象

if (parameters.ContainsKey("Red"))//調用ContainsKey()方法判斷結果對象中是否包含Red的鍵
{
//todo:分別獲取每個鍵的值並轉換為byte類型
byte r = byte.Parse(parameters["Red"]);
byte g = byte.Parse(parameters["Green"]);
byte b = byte.Parse(parameters["Blue"]);

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

base.OnNavigatedTo(e);//調用基類虛方法
}
}
}

  效果如圖:

           

  下一篇將要總結的是關於如何在頁面間數據共享。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM