本例演示了如何从隔离存储中备份文件到SkyDrive。

1. 登陆微软网站,申请“Create application”,获取一个“Client ID”

2. XAML中添加对Live相关控件库的命令控件

xmlns:my="clr-namespace:Microsoft.Live.Controls;assembly=Microsoft.Live.Controls"

3. 使用Live控件 (<my:SignInButton Name="btnSignIn"/>)

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <Image x:Name="cameraImage" Height="400" Width="480" /> <!--Button StackPanel to the right of viewfinder>-->
<Grid x:Name="ContentPanel" Grid.Row="1" Grid.Column="1" Margin="12,0,12,0">
<StackPanel>
<Button x:Name="btnShowCamera" Content="Show Camera" FontSize="26" FontWeight="ExtraBold" Height="75" Click="btnShowCamera_Click" />
<!--CHANGE ClientId to your own client ID. Leave the rest the same-->
<my:SignInButton Name="btnSignIn"/>
<Button x:Name="btnBackup" Click="btnBackup_Click" Content="Backup" IsEnabled="False" />
<TextBlock x:Name="tbDate" TextWrapping="Wrap" Margin="12, 30, 12, 12"/>
</StackPanel>
</Grid> <!--Used for debugging >-->
<TextBlock Height="40" HorizontalAlignment="Left" Margin="8,428,0,0" Name="tbDebug" VerticalAlignment="Top" Width="626" FontSize="24" FontWeight="ExtraBold" />
</Grid>

4. 申明相关成员 LiveConnectClient  LiveConnectSession

       private LiveConnectClient client = null;
private LiveConnectSession session = null;
private string strSkyDriveFolderName = "IsolatedStorageFolder"; // The folder name for backups
private string strSkyDriveFolderID = string.Empty; // The id of the folder name for backups
private string fileID = null; // The file id of your backup file
private IsolatedStorageFileStream readStream = null; // The stream for restoring data
private string fileName = "MyAppBackup.jpg"; // Backup name for the capture image.

5. 初始化live中APP相关注册信息

           btnSignIn.ClientId = "00000000480E7666";
btnSignIn.Scopes = "wl.basic wl.signin wl.offline_access wl.skydrive_update";
btnSignIn.Branding = BrandingType.Windows;
btnSignIn.TextType = ButtonTextType.SignIn;
btnSignIn.SessionChanged += btnSignIn_SessionChanged;

6. 处理SigninButton控件相关事件

private void btnSignIn_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
{
// If the user is signed in.
if (e.Status == LiveConnectSessionStatus.Connected)
{
session = e.Session;
client = new LiveConnectClient(e.Session);
// Write message to the UI thread.
UpdateUIThread(tbDebug, "Accessing SkyDrive..."); // Get the folders in their skydrive.
client.GetCompleted +=
new EventHandler<LiveOperationCompletedEventArgs>(btnSignin_GetCompleted);
client.GetAsync("me/skydrive/files?filter=folders,albums");
}
else // Otherwise the user isn't signed in.
{
// Write message to the UI thread.
UpdateUIThread(tbDebug, "Not signed in.");
client = null;
}
} /// <summary>
/// Event for if the user just logged in.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btnSignin_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
{
if (e.Error == null)
{
// Write message to the UI thread.
UpdateUIThread(tbDebug, "Loading folder..."); // Check all the folders in user's skydrive.
Dictionary<string, object> folderData = (Dictionary<string, object>)e.Result;
List<object> folders = (List<object>)folderData["data"]; // Loop all folders to check if the isolatedstoragefolder exists.
foreach (object item in folders)
{
Dictionary<string, object> folder = (Dictionary<string, object>)item;
if (folder["name"].ToString() == strSkyDriveFolderName)
strSkyDriveFolderID = folder["id"].ToString();
} // If the IsolatedStorageFolder does not exist, create it.
if (strSkyDriveFolderID == string.Empty)
{
Dictionary<string, object> skyDriveFolderData = new Dictionary<string, object>();
skyDriveFolderData.Add("name", strSkyDriveFolderName);
client.PostCompleted += new EventHandler<LiveOperationCompletedEventArgs>(CreateFolder_Completed);
// To create the IsolatedStorageFolder in Skydrive.
client.PostAsync("me/skydrive", skyDriveFolderData); // Write message to the UI thread.
UpdateUIThread(tbDebug, "Creating folder...");
}
else // Check if the backup file is in the IsolatedStorageFile
{
// Write message to the UI thread.
UpdateUIThread(tbDebug, "Ready to backup.");
UpdateUIThread(tbDate, "Checking for previous backups...");
btnBackup.IsEnabled = true; // Get the files' ID if they exists.
client = new LiveConnectClient(session);
client.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(getFiles_GetCompleted);
// Get the file in the folder.
client.GetAsync(strSkyDriveFolderID + "/files");
}
}
else
{
MessageBox.Show(e.Error.Message);
}
}

7. 开始上传操作  client.UploadAsync

                   using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
// Upload many files.
foreach (string itemName in iso.GetFileNames())
{
fileName = itemName;
readStream = iso.OpenFile(fileName, FileMode.Open, FileAccess.Read);
client.UploadAsync(strSkyDriveFolderID, fileName, readStream, OverwriteOption.Overwrite, null);
}

例子:Backup Isolated Storage To SkyDrive的更多相关文章

  1. How to use the Isolated Storage Explorer tool for Windows Phone

    Isolated Storage Explorer is installed in the following location: Program Files (x86)\Microsoft SDKs ...

  2. 与众不同 windows phone (6) - Isolated Storage(独立存储)

    原文:与众不同 windows phone (6) - Isolated Storage(独立存储) [索引页][源码下载] 与众不同 windows phone (6) - Isolated Sto ...

  3. Silverlight-管理独立存储(Isolated Storage)

    Silverlight中的独立存储是其内部的可信任的可访问文件空间,在这里你可以使用Silverlight 随意的创建.读取.写入.删除目录和文件,它有一些类似于Cookie,但是它可以在客户端保存大 ...

  4. 例子:Alarm Clock with voice Commands Sample

    通过本例子学习: 如何使用自定义字体文件(.TTF) 如何播放声音 动画的使用 Speech 设置闹铃 应用 设置 数据存储到IsolatedStorage 如何使用自定义字体文件(.TTF) < ...

  5. WindowsPhone8拍照功能实现简介

    WindowsPhone作为一款智能手机操作系统,支持APP中拍照是必不可少的,目前在WP8上的拍照主要有以下三种途径: 1.使用CameraCaptureTask: 2.使用PhotoCamera类 ...

  6. WPF SDK研究 之 AppModel

    Jianqiang's Mobile Dev Blog iOS.Android.WP CnBlogs Home New Post Contact Admin Rss Posts - 528 Artic ...

  7. Windows Phone 开发——相机功能开发

    相机功能是手机区别于PC的一大功能,在做手机应用时,如果合理的利用了拍照功能,可能会给自己的应用增色很多.使用Windows Phone的相机功能,有两种方法,一种是使用PhotoCamera类来构建 ...

  8. 转载WPF SDK研究 之 AppModel

    Jianqiang's Mobile Dev Blog iOS.Android.WP CnBlogs Home New Post Contact Admin Rss Posts - 528 Artic ...

  9. Catel(翻译)-为什么选择Catel

      1. 介绍        这篇文章主要是为了说明,我们为什么要使用Catel框架作为开发WPF,Silverlight,和Windows phone7应用程序的开发框架.   2. 通用功能 2. ...

随机推荐

  1. Markdown学习和插件介绍

    markdown能干啥 亲们github上的项目首页的 内容+样式,都是项目中README.md文件控制的.将md风格的代码,转化成html. 而且markdown语法非常简单,5-10分钟即可学会! ...

  2. M2事后分析报告

    设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 这次M2预想的就是解决3个主要问题,1:增加查询自己购买或者发布记录的功能,2:优化 所有的网络连接 ...

  3. EasyUI-datagrid 对于展示数据进行处理(formatter)

    一:声明datagrid列,在列中添加formatter属性,并指定js方法 columns = [[ { title: '编号', field: 'Id', width: 100, sortable ...

  4. CentOS 安装 lamp(转)

    一般情况下,安装的都是最新的正式版,除非你有特殊需求,要安装指定的版本,本文暂不讨论.从最基础的开始,一点点完成一个可用的 Linux 主机.这里就开始介绍如何在 CentOS 6.0 上安装 LAM ...

  5. spring-boot启动信息中non-fatal error

    java.lang.ClassNotFoundException: org.springframework.data.web.config.EnableSpringDataWebSupport缺少依赖 ...

  6. 无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用。

    在删除northwindcs表时,发生报错,消息 3726,级别 16,状态 1,第 2 行,无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用.此时判断是因为有其他表的外键 ...

  7. Load Runner录制C/S客户端

    1.    打开应用程序 2.    点击如下菜单 弹出窗口如下 3.    点击New,弹出窗口如下,选择Web(HTTP/HTML) 4.    点击Create,弹出窗口 5.    点击OK, ...

  8. 用ORBSLAM2运行TUM Dataset数据集

    参照https://github.com/raulmur/ORB_SLAM2/blob/master/README.md 运行 4. Monocular Examples TUM Dataset 数据 ...

  9. python pandas根据首字母选行

    ret2.loc[ret2['INNERCODE'].map(lambda x:x[0]=='6' or x[0]=='3' or x[0]=='0' ),:]和matlab不一样的风格 - -直接用 ...

  10. Python模块学习笔记

    1.作用域 私有private:用'_x'或'__xx'表示,如,_a,__ab; 公有public: 如 a,b; 特殊变量,可被直接引用,如:__author__,__name__,命名变量时一般 ...