在 MVVM 中加载 LongListSelector

发布于 2024-11-19 23:08:22 字数 1941 浏览 3 评论 0原文

我是第一次使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榕城若虚 2024-11-26 23:08:22

实际上,您的转换将会失败,并且在您的情况下使用 as 运算符不会抛出 InvalidCastException ,因此它被屏蔽。

创建 PublicGrouping 的构造函数,该构造函数采用 worksByDate 包含的任何内容的 IEnumerable

这种类型的转换(称为显式转换):

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

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

Indeed your cast will fail and using the as operator will not throw an InvalidCastException in your case so it is being masked.

Create a constructor of your PublicGrouping that takes an IEnumerable of whatever worksByDate contains.

This type of casting (called explicit casting):

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

Throws an InvalidCastException if the internal type of o cannot be cast to a string. The as operator does not throw this exception and instead returns null.

寒尘 2024-11-26 23:08:22

这应该有效:

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();
                    }
                }
            });
    }

This should work:

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();
                    }
                }
            });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文