【问题标题】:Async CTP on Windows Phone - cannot using offline?Windows Phone 上的异步 CTP - 不能离线使用?
【发布时间】:2012-08-03 21:13:19
【问题描述】:

我有这个 WP7 应用程序用于阅读网络订阅源。我正在使用 Async CTP 下载提要,将它们保存在本地并使用它们,这样我就可以离线使用 App。

当我连接到 wifi 或蜂窝网络时,一切正常。但是当我离线时,应用程序在启动时崩溃并出现错误:

远程服务器返回错误:NotFound。

调用栈位置:

AsyncCTLPibrary_Phone.dll!System.Runtime.CompilerServices.AsyncVoidMethodBulder.SetException.AnonymousMethod_2(object 州)

堆栈跟踪:

在 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback 开始方法,对象状态)在 System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 System.Net.WebClient.GetWebResponse(WebRequest 请求,IAsyncResult 结果)在 System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult 结果) 在 System.Net.Browser.ClientHttpWebRequest.c_DisplayClassa.b_8(Object 状态2) 在 System.Threading.ThreadPool.WorkItem.WaitCallback_Context(对象 状态)在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Threading.ThreadPool.WorkItem.doWork(Object o) 在 System.Threading.Timer.ring()

知道为什么吗?我不会自动下载任何东西...

编辑:这是代码的开始部分,如果我离线,我真的尽量避免调用网络客户端:

private void MainPage_Loaded(object sender, RoutedEventArgs e)
 {
     MainPage_Init();
 }


private async void MainPage_Init()
 {
     string isConfig = await Load_Config("");

         if (isLoaded == false)
         {
        if (RSS.Parameters.AutoUpdate == "On")
                {
                    string isDataOk = await Refresh_data(); 
                    Refresh_ui();
                }
            else
                {
                    Refresh_ui();
                }
        isLoaded = true;
         }
 }

async Task<string> Load_Config(string force)
 {
     if (!DeviceNetworkInformation.IsNetworkAvailable)
     {
         MessageBox.Show(RSS.Parameters.MessageNetwork);
         //Load Config from IsoStore
         return "No Network";
     }
     else
     {
         string data = await new WebClient().DownloadStringTaskAsync(url);
         return await GetConfig_Save_Local_XML_Async(data);
         //Load Config from Web, Save to IsoStore
     }
 }

private async Task<string> Refresh_data() //load feeds form Internet and save them to IsoStore
 {
     IsolatedStorageSettings isoStorage = IsolatedStorageSettings.ApplicationSettings;

     if (!DeviceNetworkInformation.IsNetworkAvailable)
     {
         ProgressBarSwitch("off");
         MessageBox.Show(RSS.Parameters.MessageNetwork);

     }
     else
     {
         foreach (RSSFeedInfo sfi in RSS.Parameters.FeedsInfo)
         {
        await Load_Web_XML(new Uri(sfi.Web_XML), sfi.Local_XML);
         }
         isoStorage["SettingsLastUpdate"] = System.DateTime.Now;
         isoStorage.Save();
         ProgressBarSwitch("off");
     }
     return "ok";
 }

private void Refresh_ui() //load feeds from IsoStore
 {
     //use local data
 }

【问题讨论】:

  • 您需要向我们展示您使用 Async 的部分代码。
  • 我在帖子中添加了起始代码...

标签: c# windows-phone-7 async-ctp


【解决方案1】:

您应该始终在网络代码周围有一个try/catch。有很多可能的错误情况,离线只是其中一种。

例如,如果您连接到没有互联网的本地 WiFi,即使没有互联网访问,IsNetworkAvailable 也会返回 true。

【讨论】:

  • 你是绝对正确的 :) 我在这个问题上添加了 try/catch 并且问题消失了。
【解决方案2】:

查看this article from MSDN,IsNetworkAvailable 可以返回true,即使没有连接任何底层服务:

所以一些像这样的代码:

private void button1_Click(object sender, RoutedEventArgs e){
  var sb = new StringBuilder();
  sb.Append("Network available:  ");
  sb.AppendLine(DeviceNetworkInformation.IsNetworkAvailable.ToString());
  sb.Append("Cellular enabled:  ");
  sb.AppendLine(DeviceNetworkInformation.IsCellularDataEnabled.ToString());
  sb.Append("Roaming enabled:  ");
  sb.AppendLine(DeviceNetworkInformation.IsCellularDataRoamingEnabled.ToString());
  sb.Append("Wi-Fi enabled:  ");
  sb.AppendLine(DeviceNetworkInformation.IsWiFiEnabled.ToString());
  MessageBox.Show(sb.ToString());}

可以为您提供以下方面的输出:

  • 可用网络:是的
  • 已启用蜂窝网络:错误
  • 已启用漫游:False
  • Wi-Fi 已启用:错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多