2016-04-24 36 views
0

我知道有很多類似的問題,但我對這種情況在很多方面都有限制。我正在使用SharpBox將文件上傳到Dropbox,並且爲了創建用戶可見的進度條,我使用了SharpBox返回percenatge的靜態方法。這一切都很好,但我需要以某種方式將此信息返回到aspx頁面或JavaScript。在類c中的靜態方法中獲取標籤#

我無法向該方法添加參數。我可以從方法中移除靜態,但它仍然在標籤上給出了一個非常奇怪的異常(可能是因爲該方法會動態地從SharpBox激發)。

所以方法UploadDownloadProgress是我有問題的部分。

public class docUpload 
{ 

    static public void Doc_Upload() 
    { 
     dropBoxStorage.UploadFile(stream, filename, entry, UploadDownloadProgress); 
    } 

    static void UploadDownloadProgress(Object sender, FileDataTransferEventArgs e) 
    { 

     // I need the e.PercentageProgress on aspx page 
     System.Diagnostics.Debug.WriteLine(e.PercentageProgress); 

     // This wont work since it is a static method 
     myLabel.Text = e.PercentageProgress.ToString(); 

     e.Cancel = false; 
    } 
} 

我需要標籤中的e.PercentageProgress。我也試圖調用JavaScript沒有成功。你能提出其他的選擇嗎?

+0

WinForms/WPF/UWP/etc? –

+0

不明白你的意思? –

+0

你在製作什麼樣的應用程序?我猜myLabel是一個UI控件。 myLabel在哪裏定義,因爲我沒有在您的docUpload類中看到它... –

回答

0

嘗試這樣:

public class docUpload 
{ 
    static public void Doc_Upload() 
    { 
     dropBoxStorage.UploadFile(stream, filename, entry, ProgressInformer.UploadDownloadProgress); 
    } 
} 

public class ProgressInformer { 

    public static string Progress = "0"; 

    static void UploadDownloadProgress(Object sender, FileDataTransferEventArgs e) 
    { 

     // print a dot   
     System.Diagnostics.Debug.WriteLine(e.PercentageProgress); 

     // Need to show this on a label or return to front end somehow 
     ProgressInformer.Progress = e.PercentageProgress.ToString(); 

     e.Cancel = false; 
    } 
} 

現在,因爲你是用值設置靜態變量,你可以從別的地方訪問它。然後,您可以使用該值使用某種方法或服務在前端進行回顯。也許是這樣的:

public string EchoToFrontEnd() 
{ 
    return ProgressInformer.Progress; 
} 

限制:如果這對你的作品還是這個解決方案不是線程安全的。這意味着,您無法迴應多次下載的進度。您一次只能使用一次下載。

希望這有助於...!

+0

無法調用docUpload內部的方法,像這樣ProgressInformer.UploadDownloadProgress –

+0

嘗試使用lambda表達式來執行此操作(e,s)=> {ProgressInformer.UploadDownloadProgress(e, s)}。我對這個想象力的解決方案感到抱歉,因爲我不能在我的本地VS中複製確切的代碼以給你明確的方式。 –

+0

即使我在那裏工作,UploadDownloadProgress會自動從SharpBox中觸發幾次,並且Progress不會更新aspx頁面上的標籤。 –

相關問題