我遇到了使用雙向綁定與listpicker的問題。我可以使用c#設置值,但不能在xaml的SelectedItem=".."
中設置。綁定正在返回正確的值(並且是listpicker中的一個值),因爲我通過將文本分配給文本塊來發短信。雙向綁定設置問題
頁面加載後,綁定的listpicker使用導致System.ArgumentOutOfRangeException
我使用的設置它的代碼是:
// Update a setting value. If the setting does not exist, add the setting.
public bool AddOrUpdateValue(string key, Object value)
{
bool valueChanged = false;
try
{
// If new value is different, set the new value
if (settingsStorage[key] != value)
{
settingsStorage[key] = value;
valueChanged = true;
}
}
catch (KeyNotFoundException)
{
settingsStorage.Add(key, value);
valueChanged = true;
}
catch (ArgumentException)
{
settingsStorage.Add(key, value);
valueChanged = true;
}
catch (Exception e)
{
Console.WriteLine("Exception occured whilst using IsolatedStorageSettings: " + e.ToString());
}
return valueChanged;
}
// Get the current value of the setting, if not found, set the setting to default value.
public valueType GetValueOrDefault<valueType>(string key, valueType defaultValue)
{
valueType value;
try
{
value = (valueType)settingsStorage[key];
}
catch (KeyNotFoundException)
{
value = defaultValue;
}
catch (ArgumentException)
{
value = defaultValue;
}
return value;
}
public string WeekBeginsSetting
{
get
{
return GetValueOrDefault<string>(WeekBeginsSettingKeyName, WeekBeginsSettingDefault);
}
set
{
AddOrUpdateValue(WeekBeginsSettingKeyName, value);
Save();
}
}
而在XAML:
<toolkit:ListPicker x:Name="WeekStartDay"
Header="Week begins on"
SelectedItem="{Binding Source={StaticResource AppSettings},
Path=WeekBeginsSetting,
Mode=TwoWay}">
<sys:String>monday</sys:String>
<sys:String>sunday</sys:String>
</toolkit:ListPicker>
StaticResource AppSettings
是來自單獨的.cs文件的資源。
<phone:PhoneApplicationPage.Resources>
<local:ApplicationSettings x:Key="AppSettings"></local:ApplicationSettings>
</phone:PhoneApplicationPage.Resources>
在此先感謝
當你寫'{StaticResource AppSettings}'時,你是什麼意思?它是''XAML'或'static class'中定義的'resource'的名字嗎? – decyclone 2010-12-17 16:23:06
對不起,我忘了提及AppSettings是一個單獨的類文件 – Jamie 2010-12-17 16:35:54
它是一個靜態類嗎?您是否將當前綁定與{x:Static AppSettings.WeekBeginsSetting}混淆?因爲Silverlight不支持'{x:Static}'。 – decyclone 2010-12-17 16:40:25