【发布时间】:2015-10-26 17:53:54
【问题描述】:
能否更改 App.xaml.cs 中初始化方法的方法签名?
我正在尝试让应用程序等到我下载基本数据后才能将其加载到下一个屏幕,但是当我使用 Windows Phone Silverlight 8.0 尝试它时出现错误。
这就是方法。它在 App.xaml.cs 中,它是应用程序启动时执行的方法。
带有异步的普通签名。我添加了 async 这个词。
private async void Application_Launching(object sender, LaunchingEventArgs e) { Debug.WriteLine("hhhhhhhhhhhhhhhhhhhhhhhhhhhh"); Debug.WriteLine("App-> Launching -> salvar_JSON"); App.estaAtivo = true; try { Debug.WriteLine("App-> Launching -> sevidorWeb-> salvarJSON // Tentativa"); await web.salvarJSON(ServidorWEB.URL_JSON_SLZ, ServidorWEB.ARQUIVO_JSON_SLZ); await web.salvarJSON(ServidorWEB.URL_JSON_ITZ, ServidorWEB.ARQUIVO_JSON_ITZ); await web.salvarJSON(ServidorWEB.URL_JSON_STI, ServidorWEB.ARQUIVO_JSON_STI); await sleepObj.set_to_NENHUM(); App.HOJE = await tempo.getDiaSemana(); await tempo.iniciarRelogioInterno(web); }
签名更改为我想要的:
// Code to execute when the application is launching (eg, from Start) // This code will not execute when the application is reactivated private async Task<bool> Application_Launching(object sender, LaunchingEventArgs e) { Debug.WriteLine("hhhhhhhhhhhhhhhhhhhhhhhhhhhh"); Debug.WriteLine("App-> Launching -> salvar_JSON"); App.estaAtivo = true; try { Debug.WriteLine("App-> Launching -> sevidorWeb-> salvarJSON // Tentativa"); await web.salvarJSON(ServidorWEB.URL_JSON_SLZ, ServidorWEB.ARQUIVO_JSON_SLZ); await web.salvarJSON(ServidorWEB.URL_JSON_ITZ, ServidorWEB.ARQUIVO_JSON_ITZ); await web.salvarJSON(ServidorWEB.URL_JSON_STI, ServidorWEB.ARQUIVO_JSON_STI); await sleepObj.set_to_NENHUM(); App.HOJE = await tempo.getDiaSemana(); await tempo.iniciarRelogioInterno(web); return true; }
我该怎么做?
错误如下:
【问题讨论】:
-
只有在同步调用方法的情况下才能在Application_Lauching中进行下载,这会带来糟糕的用户体验(因为手机网络不稳定,所以下载可能需要很长时间)。只需将用户重定向到带有“正在加载”消息(基本上是一种启动画面)的自定义页面,然后在闲暇时进行下载。或者更好的是,直接从需要它的页面下载(使用进度指示器)
标签: c# silverlight windows-phone-8 async-await task