我在 WP7 中构建简单的 CRUD 表单时遇到问题。我花了很多时间在列表选择器中显示枚举,现在在尝试绑定(bind)到 (IsolatedStorage) 对象时看到 InvalidCastException。
public class Bath {
public string Colour { get; set; }
public WaterType WaterType { get; set; }
}
public enum WaterType {
Hot,
Cold
}
枚举绑定(bind)到ListPicker,但由于WP7中没有enum.GetValues(),这不是一个简单的任务。
我有一个简单的类型类...
public class TypeList
{
public string Name { get; set; }
}
在我的 View 模型中,我有 ObservableCollection 并模拟枚举中的值...
private ObservableCollection<TypeList> _WaterTypeList;
public ObservableCollection<TypeList> WaterTypeList
{
get { return _WaterTypeList; }
set
{
_WaterTypeList= value;
NotifyPropertyChanged("WaterTypeList");
}
}
public void LoadCollectionsFromDatabase()
{
ObservableCollection<TypeList> wTypeList = new ObservableCollection<WaterTypeList>();
wTypeList.Add(new TypeList{ Name = WaterType.Hot.ToString() });
wTypeList.Add(new TypeList{ Name = WaterType.Income.ToString() });
WaterTypeList = new ObservableCollection<TypeList>(wTypeList);
}
最后,我的 xaml 包含列表框...
<toolkit:ListPicker
x:Name="BathTypeListPicker"
ItemsSource="{Binding WaterTypeList}"
DisplayMemberPath="Name">
</toolkit:ListPicker>
我不确定以上是否是最佳实践,以及以上是否确实是问题的一部分,但以上确实给了我一个填充的 ListPicker。
最后,当提交表单时,强制转换会导致 InvalidCastException。
private void SaveAppBarButton_Click(object sender, EventArgs e)
{
var xyz = WaterTypeList.SelectedItem; // type AppName.Model.typeList
Bath b = new Bath
{
Colour = ColourTextBox.Text ?? "Black",
WaterType = (WaterType)WaterTypeListPicker.SelectedItem
};
App.ViewModel.EditBath(b);
NavigationService.Navigate(new Uri("/Somewhere.xaml", UriKind.Relative));
}
}
有没有人遇到过类似的问题并可以提供建议。我发现我的选择是集中精力从 ListPicker 中转换一些有意义的内容,还是应该重新考虑 ListPicker 的填充方式?
最佳答案
据我所知,WaterTypeList 是一个 ObservableCollection,它是一种类型,而可观察集合没有 SelectedItem 属性。
您的 Bath 类有一个接受 WaterType 属性的 WaterType,并且您正在尝试将 WaterTypeListPicker.SelectedItem 转换为它。所以我假设您的 WatertypeListPicker 是您的 ListBox?
如果是,那么您就做错了,因为您的 ListBox 的 itemssource 绑定(bind)到一个类,并且您正在尝试将 a 添加到您的 WaterType 属性。
我会说,
Bath b = new Bath
{
Colour = ColourTextBox.Text ?? "Black",
WaterType = WaterTypeListPicker.SelectedItem
};
要么将 Bath 的 WaterType 属性更改为 TypeList,这样上面的代码就可以工作。但我不建议做另一个类来包装枚举只是为了将其显示到列表框。
我要做的是创建一个 EnumHelper
public static class EnumExtensions
{
public static T[] GetEnumValues<T>()
{
var type = typeof(T);
if (!type.IsEnum)
throw new ArgumentException("Type '" + type.Name + "' is not an enum");
return (
from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
where field.IsLiteral
select (T)field.GetValue(null)
).ToArray();
}
public static string[] GetEnumStrings<T>()
{
var type = typeof(T);
if (!type.IsEnum)
throw new ArgumentException("Type '" + type.Name + "' is not an enum");
return (
from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
where field.IsLiteral
select field.Name
).ToArray();
}
}
并将其绑定(bind)到集合
我的ViewModel
public IEnumerable<string> Priority
{
get { return EnumExtensions.GetEnumValues<Priority>().Select(priority => priority.ToString()); }
public string SelectedPriority
{
get { return Model.Priority; }
set { Model.Priority = value; }
}
就这样。
我的XAML。
<telerikInput:RadListPicker SelectedItem="{Binding SelectedPriority, Mode=TwoWay}" ItemsSource="{Binding Priority}" Grid.Column="1" Grid.Row="4"/>
关于c# - Windows Phone 7 ListPicker InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11514045/