gpt4 book ai didi

c# - Silverlight 和异步调用的问题

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:00 24 4
gpt4 key购买 nike

我有一些代码如下:

App.xaml 调用 SetUp() 方法,该方法使用异步调用填充本地集合并将该集合公开为公共(public)属性。

这一切都很好。

现在我像这样在我的 Silverlight 应用程序中创建第一页的实例

    private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Summary();
}

在 Summary() 的构造函数中,我希望那些异步调用已完成并且我的集合已填充,但异步调用尚未完成。即使我在 Summary() 上创建实例之前执行 Thread.Sleep(100000....) 也是这种情况

问题是,在 Summary() 的构造函数退出并且 UI 显示给用户之前,我的异步调用不会启动!

什么!!!

我对此有什么办法吗?或者这只是异步调用的工作方式,即它们会等到当前工作完成后再触发?

最佳答案

这就是我解决这种情况的方法(我将使用简单的字符串下载作为示例):-

private void Application_Startup(object sender, StartupEventArgs e)
{
WebClient web = new WebClient();
web.DownloadStringCompleted += (s, args) =>
{
// Do stuff with args.Result);
this.RootVisual = new Summary();
};
web.DownloadStringAsync(new Uri("SomeUrl.txt", UriKind.Relative));
}

请注意,Summary 的构造和对 RootVisual 的赋值会延迟到异步下载完成。

关于c# - Silverlight 和异步调用的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2383922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com