我有这个 WP7 应用程序用于阅读网络提要。我正在使用 Async CTP 下载提要,将它们保存在本地并使用它们,这样我就可以离线使用 App。
当我连接到 wifi 或蜂窝网络时,一切都很好。但是当我离线时,应用程序在开始时崩溃并出现错误:
远程服务器返回错误:NotFound。
调用堆栈位置:
AsyncCTPLibrary_Phone.dll!System.Runtime.CompilerServices.AsyncVoidMethodBulder.SetException.AnonymousMethod_2(对象状态)
堆栈跟踪:
在 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) 在 System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 System.Net.WebClient.GetWebResponse(WebRequest 请求,IAsyncResult 结果) 在 System.Net .WebClient.DownloadBitsResponseCallback(IAsyncResult result) at System.Net.Browser.ClientHttpWebRequest.<>c_ DisplayClassa.b _8(Object state2) at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state) at 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
}