在Silverlight中可以透過QueryString方式來傳遞字串,如下面範例是導覽到Page1.xaml然後後面帶QueryString
然後在導覽到的Page1頁面中先判斷有沒有這個QueryString,有的話再去讀取出來:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override void OnNavigatedTo(NavigationEventArgs e) | |
{ | |
if (NavigationContext.QueryString.ContainsKey("id")) | |
{ | |
testText.Text = NavigationContext.QueryString["id"]; | |
} | |
} |
而在WPF中並沒有類似QueryString的方式,不過WPF的Navigate()方法擁有更多種的多載,所以可以利用第一種object參數的導覽方式,做法如下:
在Frame要導覽到Page1.xaml就直接new一個Page1,然後裡面是要傳遞的值
而在Page1的建構函式原本是沒有接收參數,所以也必須要修改一下
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class Page1 : Page | |
{ | |
public Page1(string param) | |
{ | |
InitializeComponent(); | |
testText.Text = param; | |
} | |
} |
另一種方法
另外在WPF中也可以利用類似全域變數的方式來儲存欲傳遞的值,一樣也可以達成頁面之間傳值,存取的語法如下:
當然Silverlight也可以利用定義在App.xaml.cs中的全域變數來達成,首先在App.xaml.cs中宣告變數
然後就可以在頁面中進行存取了,語法如下:
0 意見:
張貼留言