当我尝试从 SkyDrive 下载文件时,应用程序崩溃。如果我再次打开应用程序,它可以工作。
有什么问题?
这是 xaml.cs:
namespace SDKMiniBrowserCS
{
public partial class Address : PhoneApplicationPage
{
private LiveConnectClient client = null;
private IsolatedStorageFileStream stream;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
IsolatedStorageSettings inviato = IsolatedStorageSettings.ApplicationSettings;
public Address()
{
InitializeComponent();
Loaded += Customers_Loaded;
}
void Customers_Loaded(object sender, RoutedEventArgs e)
{
using (AddressBookDataContext dc = new AddressBookDataContext())
{
var itemSource = (from c in dc.Contacts select c).AsEnumerable();
ContactListBox.ItemsSource = from c in itemSource orderby c.Name ascending select c;
}
}
private void Edit_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
NavigationService.Navigate(
new Uri(string.Format("/AddressDetail.xaml?Id={0}", (ContactListBox.SelectedItem as AddressUser).Id), UriKind.Relative));
}
private void Go_Fav(object sender, System.Windows.Input.GestureEventArgs e)
{
inviato["inviato"] = (ContactListBox.SelectedItem as AddressUser).Url;
inviato["pannello"] = "1";
inviato.Save();
NavigationService.GoBack();
}
private void AddCustomerButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/AddressDetail.xaml", UriKind.Relative));
}
private void SignInButton_OnSessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e)
{
if (e.Status == LiveConnectSessionStatus.Connected)
{
client = new LiveConnectClient(e.Session);
restore.IsEnabled = true;
btnBackup.IsEnabled = true;
}
}
private void OnBackupClicked(object sender, RoutedEventArgs e)
{
client.UploadCompleted += client_UploadCompleted;
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
stream = storage.OpenFile("3VSwipeBrowser.sdf", FileMode.Open);
client.UploadAsync("me/skydrive/my_documents", "3VSwipeBrowser.sdf", stream, OverwriteOption.Overwrite, null);
}
}
void client_UploadCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show("Upload successfull!");
stream.Close();
}
}
private void OnRestoreClicked(object sender, RoutedEventArgs e)
{
string id = string.Empty;
client.GetCompleted += (obj, args) =>
{
List<object> items = args.Result["data"] as List<object>;
foreach (object item in items)
{
Dictionary<string, object> file = item as Dictionary<string, object>;
if (file["name"].ToString() == "3VSwipeBrowser.sdf")
{
id = file["id"].ToString();
}
}
client.DownloadCompleted += (o, a) =>
{
Stream stream = a.Result;
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (
IsolatedStorageFileStream fileToSave = storage.OpenFile("3VSwipeBrowser.sdf", FileMode.Create,
FileAccess.ReadWrite))
{
stream.CopyTo(fileToSave);
stream.Flush();
stream.Close();
}
}
};
client.DownloadAsync(string.Format("{0}/content", id));
};
client.GetAsync("me/skydrive/my_documents/files");
MessageBox.Show("Download successfull!");
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
}}
错误在行中:
IsolatedStorageFileStream fileToSave = storage.OpenFile("3VSwipeBrowser.sdf", FileMode.Create, FileAccess.ReadWrite))
谢谢