本例演示了如何从隔离存储中备份文件到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);
}

最新文章

  1. 【记录】xUnit for vs2012/vs2013
  2. JS中正则匹配的三个方法match exec test的用法
  3. Func&lt;T,T&gt;应用之Elasticsearch查询语句构造器的开发
  4. AngularJs自定义指令详解(1) - restrict
  5. java String 的方法 (01)
  6. Linux C 文件与目录4 将缓冲区数据写入磁盘
  7. HW5.31
  8. re模块正则表达式
  9. eclipse中的.yml和.properties文件没有绿色叶子图标
  10. Java 面试基础总结(一)
  11. WindowsPhone8.1 开发技巧
  12. SQL 常用判断语句
  13. innodb_file_per_table - 转换为InnoDB
  14. 摩登家庭第一季/全集Modern Family迅雷下载
  15. FZU 2092 收集水晶(记忆化搜索)
  16. Jinja2 简明使用手册
  17. php识别二维码
  18. Maven项目版本继承 – 我必须指定父版本?
  19. DOM基础操作(一)
  20. Xcode5.1.1支持低版本和image not found和Couldn&#39;t register XXXX with the bootstrap server. Error: unknown error code.

热门文章

  1. 解决magento后台无法登陆/登陆没有反应的方法
  2. python成长之路【第十四篇】:HTML初步认识
  3. C#文件创建、修改、访问时间修改
  4. golang os/exec 执行外部命令
  5. 【Java】变量类接口_学习笔记
  6. Evolutionary Computing: 5. Evolutionary Strategies(2)
  7. MySQL学习记录--生成时间日期数据
  8. Python 修饰符
  9. 20169212《Linux内核原理与分析》第八周作业
  10. 虚拟机解决Device eth0 does not seem to be present 问题。