2015-05-30 34 views
1

我正在將Windows Forms VB項目轉換爲WPF(日記程序)。 在Windows窗體版本中,我在圖片框中顯示圖像,並且可以傳遞完整路徑或相對於My.Computer.Filesystem.CurrentDirectory的路徑 - 圖片框可以解決這個問題。WPF - 相對於當前目錄的URI?

在新的WPF版本中,我想這樣做。然而,這僅適用於絕對,而不是相對路徑:

Dim TheBitmap As New BitmapImage 
TheBitmap.BeginInit() 
TheBitmap.UriSource = New Uri(TheURL, UriKind.RelativeOrAbsolute) 
TheBitmap.EndInit() 
TheImage.Source = TheBitmap 

只是爲了確認,圖像不資源,他們不是在設計時可用,所以我不能將它們複製到解決方案,我不想在可執行文件的目錄中找到它們(如果沒有它,我想UAC不會讓它像管理員那樣運行)

我在這裏和其他地方讀到的關於WPF URI的一切似乎都表明它們只能相對於可執行目錄或工作目錄,它似乎只能在設計時在我的項目 - >調試頁面中設置 - 而不是我想要的。

我可以讓WPF考慮一個相對於當前目錄的URI,還是我必須爲它編寫我自己的測試?

非常感謝

尼克

編輯

順便說一句,我在此期間使用的測試:

If IO.File.Exists(DatabaseLocation & "\" & TheURL) Then 
'must be a relative url 
TheBitmap.UriSource = New Uri(DatabaseLocation & "\" & TheURL,UriKind.Absolute) 
Else 
'must be an absolute url 
TheBitmap.UriSource = New Uri(TheURL, UriKind.Absolute) 
End If 

回答

0

你應該能夠轉換路徑是通過執行以下操作相對於當前目錄爲絕對路徑:

Path.Combine(Directory.GetCurrentDirectory(), relativePath) 

然後使用生成的路徑創建可以使用的絕對Uri

編輯:鑑於要求傳入的路徑可以是絕對路徑相對於當前目錄的路徑,我建議寫一個實用的輔助方法,如下列:

/// <summary> 
/// Given a file system path, will return an absolute Uri representing that path. 
/// </summary> 
/// <param name="path">The file system path to turn into a Uri</param> 
/// <returns> 
/// If the provided path is absolute, returns an absolute Uri representing that path. 
/// If the provided path is relative, returns an absolute Uri representing that path relative to the current directory. 
/// </returns> 
/// <exception cref="ArgumentNullException">The provided path value is null</exception> 
/// <exception cref="ArgumentException">The provided path value is an empty string or invalid file system path</exception> 
public static Uri GetAbsoluteUri(string path) 
{ 
    if (path == null) throw new ArgumentNullException("path"); 
    if (path == string.Empty) throw new ArgumentException("Path cannot be an empty string.", "path"); 

    Uri uri; 
    if (Uri.TryCreate(path, UriKind.Absolute, out uri)) 
    { 
     return uri; 
    } 
    if (Uri.TryCreate(path, UriKind.Relative, out uri)) 
    { 
     Uri baseUri = new Uri(Directory.GetCurrentDirectory(), UriKind.Absolute); 
     Uri newUri; 
     if (Uri.TryCreate(baseUri, uri, out newUri)) 
     { 
      return newUri; 
     } 
    } 

    throw new ArgumentException("The provided path could not be converted to a Uri.", "path"); 
} 

你可以給你的任何路徑這種方法,並找回你應該能夠使用的絕對Uri。正如所寫的,如果你傳遞了一個無效路徑,它會拋出一個異常,但你可以自定義它,但是你想要的。

編輯:我忘了這個問題是VB而不是C#。我試圖將該方法轉換爲VB。如果事情不完全正確,請原諒我,因爲我沒有使用VB的經驗。

Public Shared Function GetAbsoluteUri(path As String) As Uri 
    If (path Is Nothing) Then 
     Throw New ArgumentNullException("path") 
    End If 
    If (path = String.Empty) Then 
     Throw New ArgumentException("Path cannot be an empty string.", "path") 
    End If 

    Dim inUri As Uri = Nothing 
    If (Uri.TryCreate(path, UriKind.Absolute, inUri)) Then 
     Return inUri 
    End If 
    If (Uri.TryCreate(path, UriKind.Relative, inUri)) Then 
     Dim baseUri As Uri = New Uri(Directory.GetCurrentDirectory(), UriKind.Absolute) 
     Dim newUri As Uri = Nothing 
     If (Uri.TryCreate(baseUri, inUri, newUri)) Then 
      Return newUri 
     End If 
    End If 

    Throw New ArgumentException("The provided path could not be converted to a Uri.", "path") 
End Function 
+0

我可以很容易地轉換一個相對uri - 但如果我喂絕對的話就不會工作。 Picturebox.LoadAsync()可以使用。我希望有一種方法可以在WPF中自動處理。同時我寫了自己的測試。 – nicke

+0

用可能有幫助的東西更新了我的答案。 – Xavier

+0

謝謝Xavier。我實際上使用VB,但我可以弄清楚你的代碼是幹什麼的。我認爲這基本上是我現在使用的更強大的版本。如果WPF可以以某種方式在本地執行此操作,那將會很不錯,但我知道WPF與更好的組織項目相比具有更大的靈活性和潛力,與Win Forms相比,WPF也缺少很多東西! – nicke