在MVVM中加载LongListSelector

时间:2011-07-14 14:54:38

标签: mvvm mvvm-light windows-phone-7

我第一次使用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。

2 个答案:

答案 0 :(得分:2)

确实你的演员表会失败并且使用as运算符不会在你的情况下抛出InvalidCastException,因此它被屏蔽了。

创建PublicGrouping的构造函数,其中IEnumerable包含worksByDate包含的内容。

这种类型的投射(称为显式投射):

object o = 2;
string h = (string)o;

如果InvalidCastException的内部类型无法转换为字符串,则抛出oas运算符不会抛出此异常,而是返回null。

答案 1 :(得分:1)

这应该有效:

public const string WorkoutsPropertyName = "Workouts";

private List<PublicGrouping<DateTime, Workout>> _workouts;
public List<PublicGrouping<DateTime, Workout>> Workouts
{
    get { return _workouts; }
    set
    {
        if (_workouts == value)
        {
            return;
        }



         _workouts = value;

         RaisePropertyChanged(WorkoutsPropertyName);
        }
    }

    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 new PublicGrouping<DateTime, Workout>(c);
                        Workouts = workoutsByDate.ToList();
                    }
                }
            });
    }