Windows Phone 社交分享SDK發布(新浪微博、騰訊微博、人人)


update2012.7.15: 現在可以使用Nuget獲取到 Alexis.WindowsPhone.Social.dll

 

Chapter 1. Why I create Alexis.WindowsPhone.Social.dll

    現在的互聯網是社交化時代,人們有什么稀奇古怪的事情都喜歡放到互聯網上與好友分享。

各個社交化平台都各自推出了自己的Windows Phone SDK,然則這些SDK都是功能比較齊全,占用的空間也是比較大。

但是有些時候,我們在應用程序中可能僅僅只需要某幾項功能,如登錄授權,分享。龐大的分享SDK占據着大量的空間,雖不致程序運行緩慢,但始終拖了一點后腿。

於是精簡版的SDK:Alexis.WindowsPhone.Social.dll 就誕生了。開發者可以使用Alexis.WindowsPhone.Social.dll方便的進行社交分享,只需要簡單配置,就能將您的App分享到各平台。

Chapter 2. What’s in Alexis.WindowsPhone.Social.dll

Alexis.WindowsPhone.Social.dll 大小71KB,遠比各平台提供的官方SDK要小,內嵌了新浪微博、騰訊微博、人人網的logo

image_thumb

賬戶管理控件,在SDK中集成了賬戶管理控件,內置了注銷賬號、綁定帳號的功能

1_thumb

分享控件,當用戶想要分享的時候,可以讓用戶選擇分享的平台

image_thumb1

授權控件,采用WebBrowser展示授權頁面

image_thumb2

 

公共接口:

LogOff:注銷賬號

UploadStatusWithPic:分享一條帶圖片的微博

image_thumb3

 

Chapter 3. How to use Alexis.WindowsPhone.Social.dll

一、前期准備:

1. 注冊分享平台注冊

在使用SDK之前,我們需要先在各個分享平台上注冊自己的應用,以獲取相應的AppKey和AppSecret

新浪微博:http://open.weibo.com -> 我的應用 -> 創建應用

騰訊微博:http://dev.open.t.qq.com/ -> 我的應用 -> 創建應用

人人網:http://dev.renren.com/ -> 我的應用 -> 創建應用

並且填寫好各個分享平台需要填寫的資料,以讓你的應用順利通過審核

2. 添加測試帳號

各個平台都支持沒有通過審核的應用進行測試運行,即在審核沒有通過之前,我們可以添加一些測試帳號來調用開發平台的接口。

二、 使用Social SDK

分享流程:用戶點擊分享-》使用ShareControl讓用戶選擇平台-》如果用戶沒有登錄,使用AuthControl進行授權-》登錄成功后,進入發送頁面(自己定義)

1. 下載Alexis.WindowsPhone.Social.dll,並添加引用到主項目中。

2. 在App.cs 中定義SocialSDK中需要用到的全局變量(當然你也可以在其他類里面定義)

/// <summary>
/// current socail type
/// </summary>
public static SocialType CurrentSocialType { get; set; }

/// <summary>
/// if login from account page, then we should goback
/// </summary>
public static bool IsLoginGoBack { get; set; }

/// <summary>
/// shared text
/// </summary>
public static string Statues { get; set; }

/// <summary>
/// shared image
/// </summary>
public static WriteableBitmap ShareImage { get; set; }
3. 新建Constants類,填寫申請到的社交平台的Key,如下:
public class Constants
{
    public const string SHARE_IMAGE = "share.jpg";

    public static ClientInfo GetClient(SocialType type)
    {
        ClientInfo client = new ClientInfo();
        switch (type)
        {
            case SocialType.Weibo:
                client.ClientId = "YOUR_WEIBO_CLIENT_ID";
                client.ClientSecret = "YOUR_WEIBO_CLIENT_SECRET";
                //client.RedirectUri = "http://weibo.com/";//if not set,left this property empty
                break;
            case SocialType.Tencent:
                client.ClientId = "";
                client.ClientSecret = "";
                break;
            case SocialType.Renren:
                client.ClientId = "";
                client.ClientSecret = "";
                break;
            default:
                break;
        }
        return client;
    }
}
4. 添加授權頁面,命名為SocialLoginPage.xaml,改頁面用於放置SDK中的授權控件,並處理相關的頁面跳轉邏輯。清空頁面中的XAML,如下:
<!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
       
    </Grid>

5. 在頁面構造函數中加載Social SDK中的登錄授權控件,代碼如下:

private void LoadLoginControl()
{
    AuthControl control = new AuthControl();
    var type = App.CurrentSocialType;
    control.SetData(type, Constants.GetClient(type));
    control.action += (p) =>
    {
        if (App.IsLoginGoBack)
        {
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                if (NavigationService.CanGoBack)
                {
                    NavigationService.GoBack();
                }
            });
        }
        else
        {
            _isClearBackStack = true;
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                NavigationService.Navigate(new Uri("/SocialSendPage.xaml", UriKind.Relative));
            });
        }
    };
    this.LayoutRoot.Children.Add(control);
}

6. 同時重寫頁面的OnNavigatedFrom事件,原因是在登錄成功后需要清理掉登錄頁面(清理臨時頁面)

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    if (_isClearBackStack)
    {
        if (NavigationService.CanGoBack)
        {
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                NavigationService.RemoveBackEntry();
            });
        }
    }
    base.OnNavigatedFrom(e);
}

7.創建發送頁面,命名為SocialSendPage.xaml,改頁面用於發送分享的內容,添加一個Appbar按鈕並注冊點擊事件,用於響應發送,具體的發送代碼如下:

private void Send()
{
    this.Focus();
    ApplicationBar.IsVisible = false;

    grid.Visibility = System.Windows.Visibility.Visible;
    tbk_busy.Text = "正在發送...";
    if (sb_busy != null)
    {
        sb_busy.Begin();
    }
    SocialAPI.Client = Constants.GetClient(App.CurrentSocialType);

    SocialAPI.UploadStatusWithPic(App.CurrentSocialType, ptb_status.Text, Constants.SHARE_IMAGE, (isSuccess, err) =>
    {
        Deployment.Current.Dispatcher.BeginInvoke(delegate
        {
            ApplicationBar.IsVisible = true;
            grid.Visibility = System.Windows.Visibility.Collapsed;
            if (isSuccess)
            {
                MessageBox.Show("發送成功");
                if (NavigationService.CanGoBack)
                {
                    NavigationService.GoBack();
                }
            }
            else
            {
                MessageBox.Show("分享失敗");
            }
        });
    });
}

8.如果想要帳號管理頁面,可以創建AccountPage.xaml頁面,並在頁面中放置SDK中的賬戶控件,注冊點擊事件即可。

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <social:AccountControl x:Name="accountControl" />
        </Grid>
public AccountPage()
{
    InitializeComponent();
    accountControl.BindAction = ((p) =>
    {
        App.IsLoginGoBack = true;
        App.CurrentSocialType = p;
        NavigationService.Navigate(new Uri("/SocialLoginPage.xaml", UriKind.Relative));
    });
}

完整的代碼可以查看SDK中Demo。


項目開源地址:http://socialsdk.codeplex.com/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM