【问题标题】:Change method signature of App.xaml.cs to return async Task<bool>更改 App.xaml.cs 的方法签名以返回异步 Task<bool>
【发布时间】: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


【解决方案1】:

我同意KooKiz的建议。

但您仍然可以保持函数签名不变并使用 Task.Run().Wait() 机制。

您可以将异步函数放在 Run 块中并强制 UI 等待它结束。

就像我说的那样,这不是一个好方法,建议显示一些加载屏幕并将您的资源加载机制放在后面。

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 2022-06-21
    相关资源
    最近更新 更多