2013-07-14 18 views
0

我目前正在使用c#中的Windows應用商店應用程序。本地保存並加載列表框項目並將它們傳遞給其他頁面

現在, 我有一個列表框'Listbox1',它從一個文本框'任務'上的按鈕單擊事件獲取其項目,並選擇項目刪除其他按鈕單擊事件的屬性。

private void add_Click(object sender, RoutedEventArgs e) 
    { 
     string t; 
     t = tasks.Text; 
     if (t != "") 
     { 
      Listbox1.Items.Add(t); 
     } 
     else 
     { 
      var a = new MessageDialog("Please Enter the Task First"); 
      a.Commands.Add(new UICommand("Ok")); 
      a.ShowAsync(); 
     } 
     tasks.Text = ""; 
    } 

    private void del_Click(object sender, RoutedEventArgs e) 
    { 
     for (int p = 0; p < Listbox1.SelectedItems.Count; p++) 
     { 
      Listbox1.Items.Remove(Listbox1.SelectedItems[p].ToString()); 
      p--; 
     } 
    } 

現在我想將這個列表保存到本地應用程序存儲中,在用戶完成更改後(在按鈕單擊事件上)。 並且還將所有列表框項目發送到另一個頁面。

我不是一個編碼器,我設計的東西。

請以樣品或參考指導我。

預先感謝您:)

回答

0

如果您已經存儲的數據到本地存儲,你可以只讀取它在OnNavigatedTo覆蓋其他頁面。否則,請使用導航參數:http://social.msdn.microsoft.com/Forums/windowsapps/en-US/8cb42356-82bc-4d77-9bbc-ae186990cfd5/passing-parameters-during-navigation-in-windows-8

編輯:我不確定您是否還需要一些關於本地存儲的信息。這很容易:Windows.Storage.ApplicationData.Current.LocalSettings有一個名爲值的屬性,這是一個Dictionary你可以寫你的設置。看看http://msdn.microsoft.com/en-us/library/windows/apps/hh700361.aspx

編輯:嘗試像這樣的代碼來存儲您的列表。

// Try to get the old stuff from local storage. 
object oldData = null; 
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; 
bool isFound = settings.Values.TryGetValue("List", out oldData); 

// Save a list to local storage. (You cannot store the list directly, because it is not 
// serialisable, so we use the detours via an array.) 
List<string> newData = new List<string>(new string[] { "test", "blah", "blubb" }); 
settings.Values["List"] = newData.ToArray(); 

// Test whether the saved list contains the expected data. 
Debug.Assert(!isFound || Enumerable.SequenceEqual((string[]) oldData, newData)); 

注意,這僅適用於測試演示代碼 - 它沒有真正意義上的...

編輯:一個忠告:不要在你的點擊處理堅持名單,因爲這會變得非常慢隨着名單的增長。我會加載和保存在導航處理程序列表,即添加類似

protected override void OnNavigatedTo(NavigationEventArgs e) { 
    base.OnNavigatedTo(e); 
    if (this.ListBox1.ItemsSource == null) { 
     object list; 
     if (ApplicationData.Current.LocalSettings.Values.TryGetValue("List", out list)) { 
      this.ListBox1.ItemsSource = new List<string>((string[]) list); 
     } else { 
      this.ListBox1.ItemsSource = new List<string>(); 
     } 
    } 
} 

protected override void OnNavigatedFrom(NavigationEventArgs e) { 
    if (this.ListBox1.ItemsSource != null) { 
     ApplicationData.Current.LocalSettings.Values["List"] = this.ListBox1.ItemsSource.ToArray();   
    } 
    base.OnNavigatedFrom(e); 
} 
+0

nope, 我還沒有存儲數據。 這就是每當應用程序重新啓動時最讓我感到困擾的是,所有更改都會丟失。 –

+0

你把這些東西寫入'ApplicationDataContainer'中了嗎?我添加了一個適合我的樣本。在第一次運行中'isFound'是'false',之後它應該是'true','oldData'應該包含字符串。 – Christoph

0

這裏是在WinRT的應用開發SQLite數據庫使用非常漂亮的簡單的例子。看看它,你會知道如何將你的數據存儲在本地機器上。我從這個例子中學習了基本代碼。現在

http://blogs.msdn.com/b/robertgreen/archive/2012/11/13/using-sqlite-in-windows-store-apps.aspx

,爲便於導航讓我建議你爲你的應用程序的該部分的流程。

採取文本框的 一個ObservableCollection<>string和存儲的值到該ObservationCollection與onClick()然後 指的是ObservableCollection<String>到 列表框的ItemsList

現在您需要將數據發送到下一個頁面的時間,使下一個頁面的一個參數化的構造函數,並傳遞ObservableCollection<String>,因爲它的參數。

現在,您可以在構造函數中訪問這些數據,並且可以按照您的需要使用它們。

希望這會有所幫助..

相關問題