使用.NET平台工具操作Live Framework

目的

这个试验中展示了如何创建一个Windows窗体应用程序来查找和操作保存在Live Framework中的图片。由于这个示例的目的是展示Live Framework的.NET API,而并不是为了讲解构建一个高质量的应用程序,所以这里很少包含输入验证、异常处理、错误处理。这个示例中使用一套“Mesh Object”对象的实例。

准备

如果要设计如下示例必须下载并安装Live Framework SDK,而且需要使用Visual Studio 2008 SP1。

开始

在开始试验之前,请确认你的Windows Live ID和密码。

Live Framework 服务的URI:https://user-ctp.windows.net

注意!!!在使用https://user-ctp.windows.net 这个地址之前,必须登陆到“Azure Services Developer Portal”,在新项目页面中单击“Activate Live Framework CTP”来激活帐号。

练习一:连接到Live Framework

任务一:创建一个新的Visual Studio项目,命名为MyPhotos

1. 打开Visual Studio。单击开始菜单,在所有程序列表中定位Visual Studio 2008,然后单击。

2. 点击文件菜单,选择新建-〉项目-〉Windows窗体应用程序。并选择C# 语言。

3. 输入MyPhotos作为项目名。

默认情况下,Visual Studio在Windows Vista系统下创建的项目在C:\Users\USERNAME\Documents\Visual Studio 2008\Projects目录下。这里的UserName是你登录到本机的帐户名。

clip_image002

4. 点击OK。

此时,你会进入窗体的设计界面(标签为Form1.cs[Design])。

5. 点击Form1,右键单击,选择属性,然后将窗体的名字修改为MyPhotos,大小设置为730,660,窗体的文本属性是My Photos。

6. 将鼠标移到解决方案资源管理器上,右键单击引用,选择添加引用。

7. 单击浏览按钮,定位到Live Framework SDK安装路径下的.NET Library目录(C:\Program Files\Microsoft SDKs\Live Framework\v0.91\API Toolkits\.Net Library)。

8. 选择其中全部DLL(Microsoft.LiveFX.Client.dll、Microsoft.LiveFX.ResourceModel.dll、Microsoft.Web.dll),然后将他们添加到引用。

9. 右键单击引用,选择添加引用,并选择.NET选项卡,然后添加对System.Runtime.Serialization的引用。

clip_image002[8]

任务二:使用全局变量和语句来添加连接字符串

1. 展开解决方案资源管理器中的属性节点,双击Resources.resx。然后添加一个URI到里面。这个URI指向Mesh服务器: https://user-ctp.windows.net

2. 保存并关闭现在的选项卡。此时你会返回窗体的设计视图。

3. 按F7转向窗体的代码视图。

4. 添加如下Using语句到代码文件的顶端。

   1:  using System.Collections;
   2:  using System.IO;
   3:  using System.Net;
   4:  using System.Runtime.Serialization;
   5:  using Microsoft.LiveFX.Client;
   6:  using Microsoft.LiveFX.ResourceModel;  // For RoleType

    • System.Collections 用来获得缓存Framework信息的列表。
    • System.IO 用作文件操作
    • System.Net用来创建连接到Framework的凭证。
    • System.Runtime.Serialization 用来添加用户数据信息到Framework。
    • Microsoft.LiveFX.Client用来提供Framework的运行时支持。
    • Microsoft.LiveFX.ResourceModel用来支持RoleType枚举。

5. 在MyPhotos类中添加如下声明。这是对Live Framework的引用。

任务三:添加控件来连接到Framework以及退出应用程序

  1. 按Shift+F7返回设计界面clip_image002[13]
  2. 拖拽一个Label控件到窗体,并将它放置在左上角。设置其字体为粗体。设置文本属性为Windows Live ID。
  3. 拖拽一个文本控件到窗体,并放置到Label控件的右侧。
  4. 设置其名称属性为LiveIDTextBox,并设置其大小为175,20。
  5. 拖拽一个Label控件到窗体上并放置在TextBox的右侧。
  6. 设置字体为粗体,文本属性为Passwork。
  7. 拖拽一个TextBox控件到窗体,并放置在Label控件的右侧。
  8. 设置名称属性为PasswordTextBox,大小为100,20。然后将UseSystemPasswordChar属性设置为Ture。
  9. 拖拽一个Button控件,放置在TextBox控件的右侧。
  10. 设置名称为ConnectButton,并且,文本属性为Connect。
  11. 拖拽一个Button控件到窗体的右下角。
  12. 设置其名称为ExitButton,文本属性为Exit。

任务四:添加连接Framework的代码,以及退出应用程序代码

  1. 双击Connect按钮。
  2. 添加如下代码到ConnectButton_Click方法以连接到Framework。这个方法获得了Uir资源实体的值,并且从界面上的两个文本框中获得用户的Windows Live ID以及密码。它创建一个FrameworkItemAccessOptions对象,当Framework加载时获取Framework的资源。它用Windows Live ID、密码、URI,创建一个用户代理。并使用这个用户代理、URI和FrameworkItemAccessOptions对象进行连接。
       1:  if (mesh != null)
       2:     {
       3:        return;
       4:     }
       5:   
       6:     LiveItemAccessOptions accessOptions = new LiveItemAccessOptions(true);
       7:   
       8:     try
       9:     {
      10:        string address = LiveIdTextBox.Text;
      11:   
      12:        if (string.IsNullOrEmpty(address))
      13:        {
      14:           MessageBox.Show("You must specify a Windows Live ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      15:           return;
      16:        }
      17:   
      18:        string password = PasswordTextBox.Text;
      19:   
      20:        if (string.IsNullOrEmpty(password))
      21:        {
      22:           MessageBox.Show("You must enter your password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      23:           return;
      24:        }
      25:   
      26:        string uri = Properties.Resources.Uri;
      27:   
      28:        if (string.IsNullOrEmpty(uri))
      29:        {
      30:           MessageBox.Show("No URI in the resources file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      31:           return;
      32:        }
      33:   
      34:        // Create user token
      35:        String token = new NetworkCredential(address, password, uri).GetWindowsLiveAuthenticationToken();
      36:              
      37:        // Create URI for cloud Mesh
      38:        Uri theUri = new Uri(uri);
      39:   
      40:        env.Connect(token, AuthenticationTokenType.UserToken, theUri, accessOptions);
      41:   
      42:        mesh = env.Mesh;
      43:   
      44:        // Tell user they are connected
      45:        ConnectButton.Text = "connected";
      46:     }
      47:     catch (Exception ex)
      48:     {
      49:        MessageBox.Show("Caught exception trying to connect: " + ex.Message
      50:                                     , "Error
      51:                                     , MessageBoxButtons.OK
      52:                                     , MessageBoxIcon.Error);
      53:     }
  3. 返回设计界面。
  4. 双击Exit按钮。
  5. 在ExitButton_Click方法中添加如下语句。
       1:  System.Windows.Forms.Application.Exit();

任务五:保存,生成,并运行项目

  1. 按F5保存、生成、并以调试模式运行应用程序。
  2. 输入你的Windows Live ID 和密码。你也可以尝试输入错误的密码来看看应用程序会出现什么情况。
  3. 单击连接。当连接成功,按钮的文本从Connect变为connected。
  4. 单击Exit关闭应用程序。
  5. 注意:如果文本框的大小不足以显示你的Live ID或密码,可以对其进行调整。
原文地址:https://www.cnblogs.com/zhangdong/p/1646499.html