2014-04-01 58 views
1

如何保存windows phone中的頁面導航之間的值, 假設我的一個電話應用程序頁面中有兩個文本塊,並且它們每次都包含動態更改的值,現在假設我的文本塊的值爲「abc」,出於某種原因,我回到上一頁,現在當我回到我的頁面時,我希望該文本塊的值爲「abc」。怎麼做??保存Windows Phone中的頁面導航之間的值

+0

分配文本塊值。 – Jaihind

+0

@Jaihind如何做到這一點..通過代碼兄弟..向我展示..謝謝 –

+0

我給一個ans。看看,讓我知道如果工作與否。 – Jaihind

回答

2

在全局定義的靜態變量定義兩個靜態變量在App.xaml.cs

public static valueOne = string.Empty; 
public static valueTwo = string.empty; 

//Assign textbox value to variable on page leaving event 
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) 
{ 
    if(!string.IsNullOrEmpty(txtBoxOne.Text)) 
    App.valueOne = txtBoxOne.Text; 
    if(!string.IsNullOrEmpty(txtBoxTwo.Text)) 
    App.valueTwo = txtBoxTwo.text; 
} 
//Get value from page load 
protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      if(!string.IsNullOrEmpty(App.valueOne)) 
      string valueFirst = App.valueOne; 
     if(!string.IsNullOrEmpty(App.valueTwo)) 
      string valueTwo = App.valueTwo ; 
     } 
1

有多種方法可以解決這個問題。 常見的事情是使用一個靜態類,其中包含靜態屬性並將其綁定到您的視圖。

3

有幾種方法

  • IsolatedStorageSettings

保存

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; 
    // txtInput is a TextBox defined in XAML. 
    if (!settings.Contains("userData")) 
    { 
     settings.Add("userData", txtInput.Text); 
    } 
    else 
    { 
     settings["userData"] = txtInput.Text; 
    } 
    settings.Save(); 

if (IsolatedStorageSettings.ApplicationSettings.Contains("userData")) 
    { 
     txtDisplay.Text += 
     IsolatedStorageSettings.ApplicationSettings["userData"] as string; 
    } 
  • PhoneApplicationService.Current.State

PhoneApplicationService.Current.State["param"] = param

和其他頁面上,我們可以得到它這樣。

var k = PhoneApplicationService.Current.State["param"];