簡體   English   中英

使用Xamarin和C#將文件從Android應用程序上傳到Microsoft OneDrive

[英]Upload file from Android application to Microsoft OneDrive with Xamarin and C#

如何使用Xamarin for Android將文件上傳到OneDrive(SkyDrive)?

我了解有關在OneDrive(Android)上下載和上傳文件的信息

我可以在Xamarin Studio中使用Microsoft.Live嗎? 我在Visual Studio for Windows Phone應用程序中使用它:

C#:

    private void skydrive_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
    {
        if (e != null && e.Status == LiveConnectSessionStatus.Connected)
        {
            this.client = new LiveConnectClient(e.Session);
            this.GetAccountInformations();
        }
        else
        {
            this.client = null;
            InfoText.Text = e.Error != null ? e.Error.ToString() : string.Empty;
        }
    }

    private async void GetAccountInformations()
    {
        try
        {
            LiveOperationResult operationResult = await this.client.GetAsync("me");
            var jsonResult = operationResult.Result as dynamic;
            string firstName = jsonResult.first_name ?? string.Empty;
            string lastName = jsonResult.last_name ?? string.Empty;  
            InfoText.Text = "Welcome " + firstName + " " + lastName;
        }
        catch (Exception e)
        {
            InfoText.Text = e.ToString();
        }
    }

    private async void btnUpload_Click(object sender, RoutedEventArgs e)
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (var fileStream = store.OpenFile(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                try
                {
                    LiveOperationResult res = await client.BackgroundUploadAsync("me/skydrive",
                                                                                new Uri("/shared/transfers/" + fileName, UriKind.Relative),
                                                                                OverwriteOption.Overwrite
                                                                                );
                    InfoText.Text = "File " + fileName + " uploaded";
                }
                catch (Exception ex)
                {

                }
            }
        }
    }

XAML:

 <Controls:SignInButton Canvas.Left="10" Canvas.Top="351" Content="Button" 
                                           Name="skydrive" Scopes="wl.basic wl.signin wl.offline_access wl.skydrive_update" 
                                           SessionChanged="skydrive_SessionChanged" 
                                           ClientId="00000000########"/>
 <TextBlock Name="InfoText" Width="167" Height="42" Canvas.Left="192" Canvas.Top="367"></TextBlock>
 <Button Name="btnUpload" Canvas.Left="10" Canvas.Top="430" Width="166"  Click="btnUpload_Click">Upload</Button>

還有其他方法可以將文件從Android應用上傳到其他服務器嗎? PS我不能使用Visual Studio創建Android應用程序,只能使用Xamarin Studio。

您可以通過創建和引用Xamarin綁定庫來使用OneDrive for Android。 它允許您創建一個綁定項目,該項目基於聲明性方法使用C#包裝器自動包裝.jar庫。

這是官方手冊

這是帶有現有綁定庫項目和示例的Xamarin論壇鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM