在 MVVM 中加载 LongListSelector
我是第一次使用 MVVM Light,所以请耐心等待...
我的主页上有一个 LongListSelector,当加载页面时,我使用 RelayCommand 来加载来自 SQLCE 数据库的数据。
在我的 MainViewModel 中,我有一个属性 Workouts:
public const string WorkoutsPropertyName = "Workouts";
private PublicGrouping<DateTime, Workout> _workouts;
public PublicGrouping,DateDime, Workout> Workouts
{
get { return workouts; }
set
{
if (_workouts == value) { return; }
_workouts = value;
RaisePropertyChanged(WorkoutsPropertyName);
}
}
然后,在构造函数中,我创建 LoadedCommand:
public MainViewModel()
{
LoadedCommand = new RelayCommand(() =>
{
context = new XLogDataContext();
using (context)
{
if (context.Workouts.Count() > 0)
{
var workoutsByDate = from workout in context.Workouts
group workout by workout.WorkoutDate
into c
orderby c.Key
select c;
Workouts = workoutsByDate as PublicGrouping<DateTime, Workout>;
}
}
});
}
当 RelayCommand 运行时,Workouts 始终返回 null。我知道有数据被返回,所以我认为问题在于将 LINQ 查询转换为 PublicGrouping。我也尝试过创建属性,
ObservableCollection<PublicGrouping<DateTime, Workout>>
但这也不起作用。
更新:
我更改了 RelayCommand 以使用此行:
Workouts = new PublicGrouping<DateTime, Workout>(workoutsByDate);
并为 PublicGrouping 添加了此构造函数:
private readonly IGrouping<TKey, TElement> _internalGrouping;
public PublicGrouping(IOrderedQueryable<IGrouping<DateTime, Workout>> workoutsByDate)
{
_internalGrouping = (IGrouping<TKey, TElement>) workoutsByDate;
}
我仍然在构造函数中的 _internalGrouping 分配上收到 InvalidCastException 。
I am using MVVM Light for the first time, so bear with me...
I have a LongListSelector on my Main Page, and I am using a RelayCommand to load it with data from a SQLCE database when the page is loaded.
In my MainViewModel, I have a property Workouts:
public const string WorkoutsPropertyName = "Workouts";
private PublicGrouping<DateTime, Workout> _workouts;
public PublicGrouping,DateDime, Workout> Workouts
{
get { return workouts; }
set
{
if (_workouts == value) { return; }
_workouts = value;
RaisePropertyChanged(WorkoutsPropertyName);
}
}
Then, in the constructor, I create the LoadedCommand:
public MainViewModel()
{
LoadedCommand = new RelayCommand(() =>
{
context = new XLogDataContext();
using (context)
{
if (context.Workouts.Count() > 0)
{
var workoutsByDate = from workout in context.Workouts
group workout by workout.WorkoutDate
into c
orderby c.Key
select c;
Workouts = workoutsByDate as PublicGrouping<DateTime, Workout>;
}
}
});
}
When the RelayCommand runs, Workouts always returns null. I know there is data being returned, so I think the problem lies in casting the LINQ query to the PublicGrouping. I've also tried creating the properties as
ObservableCollection<PublicGrouping<DateTime, Workout>>
but that doesn't work either.
UPDATE:
I changed the RelayCommand to use this line:
Workouts = new PublicGrouping<DateTime, Workout>(workoutsByDate);
and added this constructor for the PublicGrouping:
private readonly IGrouping<TKey, TElement> _internalGrouping;
public PublicGrouping(IOrderedQueryable<IGrouping<DateTime, Workout>> workoutsByDate)
{
_internalGrouping = (IGrouping<TKey, TElement>) workoutsByDate;
}
I still get the InvalidCastException on the _internalGrouping assigment in the constructor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您的转换将会失败,并且在您的情况下使用
as
运算符不会抛出InvalidCastException
,因此它被屏蔽。创建
PublicGrouping
的构造函数,该构造函数采用worksByDate
包含的任何内容的IEnumerable
。这种类型的转换(称为显式转换):
如果
o
的内部类型无法转换为字符串,则抛出InvalidCastException
。as
运算符不会抛出此异常,而是返回 null。Indeed your cast will fail and using the
as
operator will not throw anInvalidCastException
in your case so it is being masked.Create a constructor of your
PublicGrouping
that takes anIEnumerable
of whateverworksByDate
contains.This type of casting (called explicit casting):
Throws an
InvalidCastException
if the internal type ofo
cannot be cast to a string. Theas
operator does not throw this exception and instead returns null.这应该有效:
This should work: