ViewModel 之间与 Messenger 系统的交火会导致问题

发布于 2024-09-29 00:58:49 字数 514 浏览 6 评论 0原文

我的 ButtonBar 上有一个 DateNavigatorViewModel + DateNavigatorView 。

以下是交换的 2 个视图: 每日视图和每周视图。每个视图都有一个 DailyViewModel 和 WeeklyViewModel。

在我的 DateNavigatorViewModel 中,我有 messenger.Send(SelectedDate);

在我的 DailyViewModel 和 WeeklyViewModel 中,每个寄存器都在构造函数中注册:

messenger.Register<DateTime>(this, LoadDailyData);
messenger.Register<DateTime>(this, LoadWeeklyData);

猜猜当我选择日期时会发生什么...

我正在使用 MVVM Light 工具包。

如何解决从数据库获取两次数据的问题?

I have a DateNavigatorViewModel + DateNavigatorView on my ButtonBar.

Below are 2 Views which get exchanged:
DailyView and WeeklyView. Each View has a DailyViewModel and WeeklyViewModel.

In my DateNavigatorViewModel I have messenger.Send(SelectedDate);

In my DailyViewModel and WeeklyViewModel each register in the constructor:

messenger.Register<DateTime>(this, LoadDailyData);
messenger.Register<DateTime>(this, LoadWeeklyData);

guess what happens when I select a date...

I am using MVVM Light toolkit.

How can I solve that problem of getting 2 times data from database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

云裳 2024-10-06 00:58:49

在数据访问层中,您可以使用存储在某些静态字典中的缓存,从数据库加载所需的所有数据,以便在数据层上为各个视图模型进行视图和过滤。

或者另一种方法是,让 Data 对象接收 DateChanged 消息,加载数据,然后让两个视图引发并接收第二条消息。

In your data access layer you could use caching stored in some static dictionary, load all the data you need from the database for both view and filter at the data layer for the individual viewsmodels.

Or an alternative, have the DateChanged messege be received by the Data objects, load the data and then have a second message raised and received by your two views.

无戏配角 2024-10-06 00:58:49

然后 MainViewModel 被实例化:

点击 Daily-Button 实例化:

public DailyViewModel(IMessenger messenger)
        {
            _messenger = messenger;
            _messenger.Register<DateNavigatorInfoObject>(this, LoadDailyData);

        }

        private void LoadDailyData(DateNavigatorInfoObject infoObject)
        {
            if (infoObject.DateNavigatorMode != DateTest.DateMode.Day)
                return;

            // get daily database stuff
        }

After DateNavigatorViewModel 被实例化,参见下面

点击 Weekly- Button 实例化:

public WeeklyViewModel(IMessenger messenger)
{
    _messenger = messenger;
    _messenger.Register<DateNavigatorInfoObject>(this, LoadWeeklyData);                
}

private void LoadWeeklyData(DateNavigatorInfoObject infoObject)
{
    if (infoObject.DateNavigatorMode != DateTest.DateMode.Week)
       return;

    // get weekly database stuff
}

DateNavigatorViewModel 实例化后,请参阅下面

 public DateNavigatorViewModel(IMainRepository mainRepo, IMessenger messenger)
        {
            _mainRepo = mainRepo;
            _messenger = messenger; 

            SelectedDate = DateTime.Now;
             // Wether daily/weekly data is fetched the start date for the data is  NOW // when the  ViewModels are instantiated the first time using a ViewModelLocator...
        }

现在,在 Ctor 中设置 DateTime.Now 时触发的属性

private DateTime _selectedDate;
        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set
            {
                if (_selectedDate.Date == value.Date)
                    return;

                _selectedDate = value;
                this.RaisePropertyChanged("SelectedDate");


                var infoObject = new DateNavigatorInfoObject();
                switch (DateNavigatorMode)
                {    
                    case DateTest.DateMode.Day:
                        infoObject.DateNavigatorMode = DateNavigatorMode;
                        infoObject.SelectedStartDate = value;
                        break;

                    case DateTest.DateMode.Week:
                        infoObject.DateNavigatorMode = DateNavigatorMode;
                        infoObject.SelectedStartDate = value;
                        infoObject.SelectedEndDate = value.AddDays(6);
                        break;
                }

                _messenger.Send(infoObject);                
            }

public class DateTest
    {
        public enum DateMode
        {
            Day,
            Week,
            Month,
        }
    }

infoObject 将同时发送到Daily 和 WeeklyViewModel,但根据 DateNavigatorMode,ViewModel 会拒绝数据库提取。

对我来说,这是一个解决方案,因为它首先有效,其次不涉及 DAL,仅涉及 ViewModel。

如果您喜欢,有人可能会将其标记为解决方案。也欢迎批评,也许我还可以改进一些东西?

MainViewModel is instantiated then:

clicking on the Daily-Button instantiates the:

public DailyViewModel(IMessenger messenger)
        {
            _messenger = messenger;
            _messenger.Register<DateNavigatorInfoObject>(this, LoadDailyData);

        }

        private void LoadDailyData(DateNavigatorInfoObject infoObject)
        {
            if (infoObject.DateNavigatorMode != DateTest.DateMode.Day)
                return;

            // get daily database stuff
        }

After the DateNavigatorViewModel got instantiated see BELOW

clicking on the Weekly-Button instantiates the:

public WeeklyViewModel(IMessenger messenger)
{
    _messenger = messenger;
    _messenger.Register<DateNavigatorInfoObject>(this, LoadWeeklyData);                
}

private void LoadWeeklyData(DateNavigatorInfoObject infoObject)
{
    if (infoObject.DateNavigatorMode != DateTest.DateMode.Week)
       return;

    // get weekly database stuff
}

After the DateNavigatorViewModel got instantiated see BELOW

 public DateNavigatorViewModel(IMainRepository mainRepo, IMessenger messenger)
        {
            _mainRepo = mainRepo;
            _messenger = messenger; 

            SelectedDate = DateTime.Now;
             // Wether daily/weekly data is fetched the start date for the data is  NOW // when the  ViewModels are instantiated the first time using a ViewModelLocator...
        }

Now the property that got fired setting the DateTime.Now in the Ctor

private DateTime _selectedDate;
        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set
            {
                if (_selectedDate.Date == value.Date)
                    return;

                _selectedDate = value;
                this.RaisePropertyChanged("SelectedDate");


                var infoObject = new DateNavigatorInfoObject();
                switch (DateNavigatorMode)
                {    
                    case DateTest.DateMode.Day:
                        infoObject.DateNavigatorMode = DateNavigatorMode;
                        infoObject.SelectedStartDate = value;
                        break;

                    case DateTest.DateMode.Week:
                        infoObject.DateNavigatorMode = DateNavigatorMode;
                        infoObject.SelectedStartDate = value;
                        infoObject.SelectedEndDate = value.AddDays(6);
                        break;
                }

                _messenger.Send(infoObject);                
            }

public class DateTest
    {
        public enum DateMode
        {
            Day,
            Week,
            Month,
        }
    }

The infoObject is send both to the Daily and WeeklyViewModel but depending on the DateNavigatorMode the database fetching is rejected from the ViewModel.

For me thats a solution because it firstly works and second no DAL is involved just the ViewModels.

Someone might mark it as solution if you like it. Critics are welcome too maybe I can still improve something?

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