我在WP7中構建一個簡單的粗糙表單存在問題。我花了很多時間將Enum顯示到列表選擇器中,現在我在嘗試綁定到(IsolatedStorage)對象時看到InvalidCastException。Windows Phone 7 ListPicker InvalidCastException
public class Bath {
public string Colour { get; set; }
public WaterType WaterType { get; set; }
}
public enum WaterType {
Hot,
Cold
}
枚舉綁定到ListPicker,但因爲沒有enum.GetValues()在WP7這不是一個簡單的任務。
我有一個簡單類型的類...
public class TypeList
{
public string Name { get; set; }
}
而在我的視圖模型,我的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。
最後,當提交表單時,cast會導致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));
}
}
有沒有人遇到類似的問題,並可以提供建議。我看到我的opions將集中精力從ListPicker轉換有意義的東西,還是應該重新思考ListPicker的填充方式?
那真棒,真該感謝,因爲我是很好,真正卡住! – Gavin 2012-07-17 16:04:22