更新第一个方法中的实例,然后继续在同一命令中运行第二个方法
我有一个像这样的方法
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您描述的行为是“设计使然”。您的方法
LoadProgrammeListFromChannel
进行异步调用。这意味着在您的方法中序列是 1 -> 2-> 3-> 4->返回->在调用函数中执行代码 ->然后稍后 5 然后回调。因此,
LoadProgrammeListFromChannel
和GetWhatsonProgramme
是并行执行的。因此,如果GetWhatsonProgramme
需要始终在LoadProgrammeListFromChannel
之后运行,您将需要调用将GetWhatsonProgramme
移动到您的回调方法中,即,您也可以订阅 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
andGetWhatsonProgramme
are executed in parallel. So ifGetWhatsonProgramme
needs always to run afterLoadProgrammeListFromChannel
you will have the call to moveGetWhatsonProgramme
into your callback method, i.e.Alternatively, you could subscribe to the PropertyChangedEvent of your ViewModel and if the
TV_DAILY_SCHEDULE_VM
property has been changed, call theGetWhatsonProgramme
from there, although this might not be desireable.