更新第一个方法中的实例,然后继续在同一命令中运行第二个方法

发布于 2024-12-08 02:52:23 字数 2332 浏览 0 评论 0原文

我有一个像这样的方法

public void LoadProgrammeListFromChannel(TVDailyScheduleParam scheduleParam, Action callback)
        {
            string url = Helper.GetProgrammeUrl(scheduleParam.Day, scheduleParam.Channel.Id); //1//
            WebClient client = new WebClient(); //2//

            client.OpenReadCompleted += new OpenReadCompletedEventHandler((sender, e) => //3//
            {//5//
                if (e.Error != null)
                    return;

                try
                {
                    _programmeList.Clear();
                    _programmeList = DataService.GetProgrammeList(e.Result);
                    // call method in MainVM to update View
                    callback();
                }
                finally
                {
                    // close file stream
                    e.Result.Close();
                }
            });

            client.OpenReadAsync(new Uri(url, UriKind.Absolute)); //4//
        }

,并且我有一个像这样的命令,

LoadWhatsonProgrammeCommand = new RelayCommand(()=>
            {
                foreach (TVDailyScheduleParam param in _tvDailyScheduleVM.ChannelList.Select(c => new TVDailyScheduleParam(DateTime.Today, c, false)))
                {
                    TVDailyScheduleParam param2 = param;

                    _tvDailyScheduleVM.LoadProgrammeListFromChannel(param2, ()=>
                    {
                        RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);
                    });

                    _tvDailyScheduleVM.GetWhatsonProgramme(param2, ()=>
                    {
                        RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);
                    });
                }
            });

现在当我调用该命令时。首先,它运行 _tvDailyScheduleVM.LoadProgrammeListFromChannel 并调用 LoadProgrammeListFromChannel 方法。

LoadProgrammeListFromChannel 方法中,它从 1 -> 运行。 2-> 3.在3处,还没有完成,所以运行到4处,然后回到命令,继续运行_tvDailyScheduleVM.GetWhatsonProgramme

但 LoadProgrammeListFromChannel 中的 _programmeList 未更新,因此 GetWhatsonProgramme 无法准确运行。

如何在运行 _tvDailyScheduleVM.GetWhatsonProgramme 之前返回到 LoadProgrammeListFromChannel 运行 3 来更新 _programmeList

I have a method like this

public void LoadProgrammeListFromChannel(TVDailyScheduleParam scheduleParam, Action callback)
        {
            string url = Helper.GetProgrammeUrl(scheduleParam.Day, scheduleParam.Channel.Id); //1//
            WebClient client = new WebClient(); //2//

            client.OpenReadCompleted += new OpenReadCompletedEventHandler((sender, e) => //3//
            {//5//
                if (e.Error != null)
                    return;

                try
                {
                    _programmeList.Clear();
                    _programmeList = DataService.GetProgrammeList(e.Result);
                    // call method in MainVM to update View
                    callback();
                }
                finally
                {
                    // close file stream
                    e.Result.Close();
                }
            });

            client.OpenReadAsync(new Uri(url, UriKind.Absolute)); //4//
        }

and I have a cammand like this

LoadWhatsonProgrammeCommand = new RelayCommand(()=>
            {
                foreach (TVDailyScheduleParam param in _tvDailyScheduleVM.ChannelList.Select(c => new TVDailyScheduleParam(DateTime.Today, c, false)))
                {
                    TVDailyScheduleParam param2 = param;

                    _tvDailyScheduleVM.LoadProgrammeListFromChannel(param2, ()=>
                    {
                        RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);
                    });

                    _tvDailyScheduleVM.GetWhatsonProgramme(param2, ()=>
                    {
                        RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);
                    });
                }
            });

Now when I invoke the command. At first it runs _tvDailyScheduleVM.LoadProgrammeListFromChannel and invoke the LoadProgrammeListFromChannel method.

In the LoadProgrammeListFromChannel method it runs from 1 -> 2 -> 3. At 3, it's not completed sothat it runs to 4 and then back to the command and continue to run _tvDailyScheduleVM.GetWhatsonProgramme.

But the _programmeList in LoadProgrammeListFromChannel is not updated so that GetWhatsonProgramme does not run exactly.

How can I go back to LoadProgrammeListFromChannel in order to run 3 to update _programmeList before running _tvDailyScheduleVM.GetWhatsonProgramme ?

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

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

发布评论

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

评论(1

我做我的改变 2024-12-15 02:52:23

您描述的行为是“设计使然”。您的方法 LoadProgrammeListFromChannel 进行异步调用。这意味着在您的方法中序列是 1 -> 2-> 3-> 4->返回->在调用函数中执行代码 ->然后稍后 5 然后回调。

因此,LoadProgrammeListFromChannelGetWhatsonProgramme 是并行执行的。因此,如果 GetWhatsonProgramme 需要始终LoadProgrammeListFromChannel 之后运行,您将需要调用将 GetWhatsonProgramme 移动到您的回调方法中,即

LoadWhatsonProgrammeCommand = new RelayCommand(()=> {
    var channelList = _tvDailyScheduleVM
                          .ChannelList
                          .Select(c => new TVDailyScheduleParam(
                              DateTime.Today, 
                              c, 
                              false
                          ));

    foreach (TVDailyScheduleParam param in channelList) {
        TVDailyScheduleParam param2 = param;

        _tvDailyScheduleVM.LoadProgrammeListFromChannel(param2, ()=> {
            RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);

            _tvDailyScheduleVM.GetWhatsonProgramme(param2, ()=> {
                RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);
            });
        });
    }
});

,您也可以订阅 ViewModel 的 PropertyChangedEvent,如果 TV_DAILY_SCHEDULE_VM 属性已更改,请调用从那里 GetWhatsonProgramme ,尽管这可能并不理想。

The behaviour you describe is "by design". Your method LoadProgrammeListFromChannel makes an asynchroneous call. This means within your method the sequence is 1 -> 2 -> 3 -> 4 -> return -> execute code in calling function -> then sometime later 5 and then the callback.

As a result LoadProgrammeListFromChannel and GetWhatsonProgramme are executed in parallel. So if GetWhatsonProgramme needs always to run after LoadProgrammeListFromChannel you will have the call to move GetWhatsonProgramme into your callback method, i.e.

LoadWhatsonProgrammeCommand = new RelayCommand(()=> {
    var channelList = _tvDailyScheduleVM
                          .ChannelList
                          .Select(c => new TVDailyScheduleParam(
                              DateTime.Today, 
                              c, 
                              false
                          ));

    foreach (TVDailyScheduleParam param in channelList) {
        TVDailyScheduleParam param2 = param;

        _tvDailyScheduleVM.LoadProgrammeListFromChannel(param2, ()=> {
            RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);

            _tvDailyScheduleVM.GetWhatsonProgramme(param2, ()=> {
                RaisePropertyChanged(TV_DAILY_SCHEDULE_VM);
            });
        });
    }
});

Alternatively, you could subscribe to the PropertyChangedEvent of your ViewModel and if the TV_DAILY_SCHEDULE_VM property has been changed, call the GetWhatsonProgramme from there, although this might not be desireable.

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