[英]Async CTP on Windows Phone - cannot using offline?
我有這個WP7應用程序,用於閱讀Web feed。 我正在使用異步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 request,IAsyncResult result) System.Net.Browser.ClientHttpWebRequest上的.WebClient.DownloadBitsResponseCallback(IAsyncResult結果).System System.Threading.ThreadPool.WorkItem.WaitCallback_Context(對象狀態)在System.Threading.ExecutionContext.Run處的<> c_ DisplayClassa.b _8(對象狀態2) (System.Threading.ThreadPool.WorkItem.doWork(Object o)在System.Threading.Timer.ring()處(ExecutionContext executeContext,ContextCallback回調,對象狀態)
知道為什么嗎? 我沒有自動降落任何東西...
編輯:這是代碼的開始部分,我真的盡量避免在離線時調用Web客戶端:
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
}
你應該總是有一個try
/ catch
圍繞網絡代碼。 存在很多可能的錯誤情況,其中離線狀態只是其中之一。
例如,如果您連接到沒有Internet的本地WiFi,則即使沒有Internet訪問, IsNetworkAvailable
也會返回true
。
從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());}
可以按照以下方式為您提供輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.