2014-09-04 94 views
-2

任何人都可以幫我解決一些疑惑嗎? 如何從按鈕調用此公共共享功能?以及如何在datagridview中顯示「結果」(文件列表)? 謝謝。VB網絡:如何調用這個公共共享函數?

Imports System.Threading 
Imports System.Threading.Tasks 
Imports Google.Apis.Auth.OAuth2 
Imports Google.Apis.Drive.v2 
Imports Google.Apis.Drive.v2.Data 
Imports Google.Apis.Services 
Imports System.Net 



Public Class Form1 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     '? 
    End Sub 
    Public Shared Function retrieveAllFiles(service As DriveService) As List(Of File) 
    Dim Secrets = New ClientSecrets() 
    Secrets.ClientId = "CLIENT ID" 
    Secrets.ClientSecret = "CLIENT SECRET" 

    Dim scope = New List(Of String) 
    scope.Add(DriveService.Scope.Drive) 
    Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, scope, "USER", CancellationToken.None).Result() 
    Dim initializer = New BaseClientService.Initializer 
    initializer.HttpClientInitializer = credential 
    initializer.ApplicationName = "APPLICATION NAME" 
    Dim service = New DriveService(initializer) 
    Dim request As FilesResource.ListRequest = service.Files.List() 
    Dim result As New List(Of File)() 
    Dim request As FilesResource.ListRequest = service.Files.List() 
    Do 
     Try 
      Dim files As FileList = request.Execute() 
      result.AddRange(files.Items) 
      request.PageToken = files.NextPageToken 
     Catch e As Exception 
      Console.WriteLine("An error occurred: " + e.Message) 
      request.PageToken = Nothing 
     End Try 
    Loop While Not [String].IsNullOrEmpty(request.PageToken) 
    Return result 
End Function 
End Class 
+4

告訴你如何使用它,那** **是相關代碼 – Plutonix 2014-09-04 13:46:59

+3

yourclassname.retrieveAllFiles(...) – Arvo 2014-09-04 13:56:52

回答

0

如果是在同一個班級,你可以叫它的名字,同樣作爲非共享功能:

lst = retrieveAllFiles(service) 

如果它是另一個類,需要用類前綴它名:

lst = className.retrieveAllFiles(service) 

官方參考:

+0

感謝帕特和Neolisk但是當我這樣稱呼它在你的例子,它說:它的服務未宣佈。我如何聲明它。對不起,如果它太簡單了,我新與apis。 – user3822492 2014-09-04 18:48:11

+0

@ user3822492:您可能應該更清楚DriveService對象是什麼,但應該像'Dim service As New DriveService(parameter1,parameter2,etc.)'那樣。 – Neolisk 2014-09-04 18:50:10

+0

Neolisk我完成了代碼,你覺得怎麼樣? – user3822492 2014-09-04 18:57:48