gpt4 book ai didi

c# - Windows Phone 7 列表选择器 InvalidCastException

转载 作者:行者123 更新时间:2023-11-30 21:05:45 24 4
gpt4 key购买 nike

我在 WP7 中构建简单的 crud 表单时遇到问题。我花了很多时间将 Enum 显示到列表选择器中,现在我在尝试绑定(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 的项目源绑定(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)到一个集合

我的 View 模型

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 列表选择器 InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11514045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com