Windows phone 7 在Windows Phone 7应用程序上从SkyDrive下载文件时出现NullReferenceException
我有一个应用程序,可以将其数据库备份并恢复到SkyDrive。 但如果我使用“开始”按钮(快速应用程序切换)在向SkyDrive上传或从SkyDrive下载备份时关闭应用程序,然后使用“返回”按钮返回应用程序,我会捕获未处理的NullReferenceException:Windows phone 7 在Windows Phone 7应用程序上从SkyDrive下载文件时出现NullReferenceException,windows-phone-7,nullreferenceexception,onedrive,Windows Phone 7,Nullreferenceexception,Onedrive,我有一个应用程序,可以将其数据库备份并恢复到SkyDrive。 但如果我使用“开始”按钮(快速应用程序切换)在向SkyDrive上传或从SkyDrive下载备份时关闭应用程序,然后使用“返回”按钮返回应用程序,我会捕获未处理的NullReferenceException: void RestoreFromSkyDrive() { ShowProgress(); LiveConnectClient client = new LiveConnectClient(App.Session);
void RestoreFromSkyDrive() {
ShowProgress();
LiveConnectClient client = new LiveConnectClient(App.Session);
string id = string.Empty;
if (client != null) {
client.GetCompleted += (obj, args) => {
try {
List<object> items;
if (args != null && args.Result != null && args.Result["data"] != null)
items = args.Result["data"] as List<object>;
else
return;
foreach (object item in items) {
Dictionary<string, object> file = item as Dictionary<string, object>;
// fileName is a name of backup-file
if (file != null && file["name"] != null && file["name"].ToString() == fileName) {
id = file["id"].ToString();
if (client != null) client.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(client_DownloadCompleted);
if (client != null) client.DownloadAsync(String.Format("{0}/content", id));
break;
}
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message + " restore");
}
};
if (client != null) {
client.GetAsync("me/skydrive/files");
}
}
}
void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e) {
try {
if (e.Error == null) {
Stream stream = e.Result;
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) {
var fileToSave = new IsolatedStorageFileStream(Constants.dbName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, storage);
stream.CopyTo(fileToSave);
stream.Flush();
stream.Close();
fileToSave.Close();
}
}
else {
MessageBox.Show(e.Error.Message + " dcompleted mb");
}
}
catch (WebException ex) { MessageBox.Show(ex.Message + " dcompleted ex"); }
HideProgress();
}
void RestoreFromSkyDrive(){
ShowProgress();
LiveConnectClient=新的LiveConnectClient(App.Session);
string id=string.Empty;
如果(客户端!=null){
client.GetCompleted+=(对象、参数)=>{
试一试{
清单项目;
if(args!=null&&args.Result!=null&&args.Result[“数据”]!=null)
items=args.Result[“data”]作为列表;
其他的
返回;
foreach(项中的对象项){
字典文件=作为字典的项;
//fileName是备份文件的名称
if(file!=null&&file[“name”]!=null&&file[“name”]。ToString()=fileName){
id=文件[“id”].ToString();
if(client!=null)client.downloadpleted+=neweventhandler(client\u downloadpleted);
if(client!=null)client.downloadsync(String.Format(“{0}/content”,id));
打破
}
}
}
捕获(例外情况除外){
MessageBox.Show(例如Message+restore);
}
};
如果(客户端!=null){
GetAsync(“me/skydrive/files”);
}
}
}
无效客户端\u下载已完成(对象发送方,LiveDownloadCompletedEventArgs e){
试一试{
如果(e.Error==null){
Stream=e.结果;
使用(IsolatedStorageFile storage=IsolatedStorageFile.GetUserStoreForApplication()){
var fileToSave=new-IsolatedStorageFileStream(Constants.dbName、FileMode.OpenOrCreate、FileAccess.ReadWrite、FileShare.None、storage);
stream.CopyTo(fileToSave);
stream.Flush();
stream.Close();
fileToSave.Close();
}
}
否则{
MessageBox.Show(例如Error.Message+“dcompletedmb”);
}
}
catch(WebException-ex){MessageBox.Show(ex.Message+“dcompleted-ex”);}
HideProgress();
}
我为null插入了很多检查,但无论如何我捕获了NullReferenceException。我不知道我能用它做什么
PS:StackTrace已启用CLR异常:
System.Net.WebException occurred
Message=WebException
StackTrace:
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
System.NullReferenceException was unhandled
Message=NullReferenceException
StackTrace:
at Microsoft.Live.Operations.DownloadOperation.CompleteOperation(Exception error)
at Microsoft.Live.Operations.DownloadOperation.OnCancel()
at Microsoft.Live.Operations.WebOperation.OnGetWebResponse(IAsyncResult ar)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadPool.WorkItem.doWork(Object o)
at System.Threading.Timer.ring()
System.Net.WebException发生
Message=WebException
堆栈跟踪:
位于System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
在System.Net.Browser.ClientHttpWebRequest.c__DisplayClass2.b__1(对象发送状态)中
在System.Net.Browser.AsyncHelper.c__DisplayClass4.b__0(对象发送状态)中
在System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi、对象obj、BindingFlags invokeAttr、绑定器绑定器、对象参数、CultureInfo区域性、布尔值isBinderDefault、程序集调用者、布尔值验证访问、堆栈爬网标记和堆栈标记)
位于System.Reflection.RuntimeMethodInfo.InternalInvoke(对象obj、BindingFlags invokeAttr、绑定绑定器、对象[]参数、CultureInfo区域性、StackScrawMark和stackMark)
在System.Reflection.MethodBase.Invoke(对象obj,对象[]参数)处
位于System.Delegate.DynamicInvokeOne(对象[]args)
位于System.MulticastDelegate.DynamicInvokeImpl(对象[]args)
位于System.Delegate.DynamicInvoke(对象[]args)
在System.Windows.Threading.Dispatcher.c__DisplayClass4.b__3()中
在System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi、对象obj、BindingFlags invokeAttr、绑定器绑定器、对象参数、CultureInfo区域性、布尔值isBinderDefault、程序集调用者、布尔值验证访问、堆栈爬网标记和堆栈标记)
位于System.Reflection.RuntimeMethodInfo.InternalInvoke(对象obj、BindingFlags invokeAttr、绑定绑定器、对象[]参数、CultureInfo区域性、StackScrawMark和stackMark)
在System.Reflection.MethodBase.Invoke(对象obj,对象[]参数)处
位于System.Delegate.DynamicInvokeOne(对象[]args)
位于System.MulticastDelegate.DynamicInvokeImpl(对象[]args)
位于System.Delegate.DynamicInvoke(对象[]args)
在System.Windows.Threading.DispatcherOperation.Invoke()中
在System.Windows.Threading.Dispatcher.Dispatch中(DispatcherPriority优先级)
位于System.Windows.Threading.Dispatcher.OnInvoke(对象上下文)
位于System.Windows.Hosting.CallbackCookie.Invoke(对象[]args)
位于System.Windows.Hosting.DelegateWrapper.InternalInvoke(对象[]args)
位于System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle、Int32 nParamCount、ScriptParam[]pParams、ScriptParam&pResult)
具有禁用CLR异常的StackTrace:
System.Net.WebException occurred
Message=WebException
StackTrace:
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
System.NullReferenceException was unhandled
Message=NullReferenceException
StackTrace:
at Microsoft.Live.Operations.DownloadOperation.CompleteOperation(Exception error)
at Microsoft.Live.Operations.DownloadOperation.OnCancel()
at Microsoft.Live.Operations.WebOperation.OnGetWebResponse(IAsyncResult ar)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadPool.WorkItem.doWork(Object o)
at System.Threading.Timer.ring()
System.NullReferenceException未处理
Message=NullReferenceException
堆栈跟踪:
位于Microsoft.Live.Operations.DownloadOperation.CompleteOperation(异常错误)
在Microsoft.Live.Operations.DownloadOperation.OnCancel()上
位于Microsoft.Live.Operations.WebOperation.OnGetWebResponse(IAsyncResult ar)
在System.Net.Browser.ClientHttpWebRequest.c__中显示ClassA.b__8(对象状态2)
位于System.Threading.ThreadPool.WorkItem.WaitCallback\u上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadPool.WorkItem.doWork(对象o)处
在System.Threading.Timer.ring()中
使用BackgroundDownload(Upload)Async方法而不是Download(Upload)Async对我有帮助。我尝试使用BackgroundDownloadAsync而不是DownloadAsync,乍一看效果不错。