[WPF 筆記] Page 間傳遞資料(以相對位置)

  • 619
  • 0
  • 2014-03-17

摘要:[WPF 筆記] Page 間傳遞資料(以相對位置)

//Page1.xaml.cs
private void Page1_btn_Click(object sender, RoutedEventArgs e)
{
        string passData = "Page1Data";
        NavigationService.GetNavigationService(this).Navigate(new Uri("Page1.xaml?" + 
        passData, UriKind.Relative));
 }

//Page2.xaml.cs
//因為發生在傳遞中文字時有問題,所以用 Uri.UnescapeDataString 轉成未逸出前字串
//參考來源:http://msdn.microsoft.com/zh-tw/library/system.uri.escapedatastring(v=vs.110).aspx
private void Page2_btn_Click(object sender, RoutedEventArgs e)
{
            string[] tmpStr;
            string ShowStr = "";
            tmpStr = NavigationService.CurrentSource.OriginalString.Split('?');
            ShowStrAfter = Uri.UnescapeDataString(tmpStr[1]); //將 Data 轉成未逸出前表示
            MessageBox.Show("轉換前:"+tmpStr[1]+"\r\n轉換後:"+ShowStrAfter,"Page2");
}

成功圖片: