2010-05-06 102 views
3

我的Silverlight項目的目錄結構是這樣的:的Silverlight:下載本地文件與Web客戶端

\Bin 
- MainModule.xap 
- \Images 
--- Image1.png 
--- Image2.png 
- \Modules 
--- SubModule.xap 

我希望能夠通過任何Web服務器或通過Visual Studio直接運行(調試目的我想繞過內容下載)。

在我的媒體加載代碼我這樣做如下:

if (runningLocally) 
{ 
    var bitmapImage = new BitmapImage(); 
    bitmapImage.UriSource = new Uri("Images/Image1.png", UriKind.Relative); 
    var image = new Image(); 
    image.Source = bitmapImage; 
} 
else 
{ 
    WebClient wc = new WebClient(); 
    wc.OpenReadCompleted += (s, e) => 
    { 
     var bitmapImage = new BitmapImage(); 
     bitmapImage.SetSource(e.Result); 
     var image = new Image(); 
     image.Source = bitmapImage; 
    }; 
    wc.OpenReadAsync(new Uri("Images/Image1.png", UriKind.Relative)); 
} 

這適用於圖像,但我也有子模塊,其只是組件住房UserControl秒。由於Silverlight無法讀取磁盤,因此我已經辭職了,因爲我將不得不「下載」XAP,因爲我需要的是否是本地運行的。問題是,如果我在本地運行該項目,並嘗試使用WebClient下載XAP我得到一個異常:

System.Net.WebException: An exception occurred during a WebClient request. ---> System.NotSupportedException: The URI prefix is not recognized. 

有沒有運行時,我可以讓我的子模塊XAPs任何方式(Web客戶端或其他方式)直接Silverlight項目而不是擊中Web服務器?

編輯:

忘了提我使用MEF與DeploymentCatalog也試過,雖然我沒有得到一個例外,從來都沒有撰寫所以我只能假定它有同樣的問題。

回答

0

我的設計試圖訪問文件關閉未在Silverlight允許本地磁盤。我仍然沒有一個好的答案,說明爲什麼你可以在Visual Studio中從磁盤運行Silverlight應用程序,但是它可以成功地從磁盤讀取圖像/視頻/音頻。

0

如果您不想一次又一次地從服務器上下載相同的文件(在調試過程中),請使用WebClient從服務器下載一次,然後將其存儲在IsolatedStorage上。

下面的代碼將讓你開始:

// read/write from/to IsolatedStorage 
IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForSite.OpenFile