選擇組合框的項目我有一個組合框負荷漫遊設置
<ComboBox x:Name="cityPicker">
<ComboBoxItem IsSelected="True">
<x:String>
city1
</x:String>
</ComboBoxItem>
<ComboBoxItem>
<x:String>
city2
</x:String>
</ComboBoxItem>
當用戶選擇「城2」,那麼我將它保存到漫遊設置在selectedCity
關鍵。
我需要從漫遊設置中加載這個值,當用戶在退出後啓動應用程序時以及從另一個啓動應用程序返回到此頁面後。
將此代碼值保存到RoamingSetting,並且當我在更改城市後啓動應用程序時,roamingsettings具有它的價值。但Combobox不檢索它。組合框選定的項目保持空白。
如何以編程方式更改組合框中的選定項目?
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
if (roamingSettings.Values.ContainsKey("selectedCity"))
{
cityPicker.SelectedValue = roamingSettings.Values["selectedCity"].ToString();
}
}
public StartPage()
{
InitializeComponent();
cityPicker.SelectionChanged += cityPicker_SelectionChanged;
}
void cityPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var roamingSettings =
Windows.Storage.ApplicationData.Current.RoamingSettings;
var cityPick = cityPicker.SelectedItem as ComboBoxItem;
if (cityPick != null) roamingSettings.Values["selectedCity"] = cityPick.Content;
}
我只能通過改變SelectedIndex
來做到這一點。但這不是我想要的。
哪裏是保存數據的代碼? – 2013-05-03 18:13:46
我更新問題。在調試模式下值保存成功,roamingSettings.Values [「selectedCity」]包含所需的值。 – TheX 2013-05-03 18:41:54
那麼,有什麼問題?你的文章沒有特別好的解釋。 – 2013-05-03 19:52:25