[英]How to create a relative path using System.Uri
我試圖創建一個這樣的URI
Uri uri = new Uri("file:///Pages/Menu/Page.xaml");
So I have a WPF project and its path is like this: C:/Programing/MyProjectName/
Now the path to my file are C:/Programing/MyProjectName/Pages/Menu/Page.xaml
as you saw above the path I give to Uri 是file:///Pages/Menu/Page.xaml
當我運行它時,我得到錯誤找不到路徑C:/Pages/Menu/Page.xaml
的部分,因為正如上面所說的那樣我的文件路徑是C:/Programing/MyProjectName/Pages/Menu/Page.xaml
所以我的問題是,有沒有辦法在我的 Uri 中使用相對路徑,我想使用相對路徑,因為我的應用程序不會總是在同一個文件夾中。
如果您想知道為什么我使用 Uri 是因為我在 WPF 框架上設置 Source 屬性,並且它使用 Uri 而不是字符串。
為了加載頁面,請使用資源文件包 URI :
var uri = new Uri("pack://application:,,,/Pages/Menu/Page.xaml");
也可以這樣寫:
var uri = new Uri("/Pages/Menu/Page.xaml", UriKind.Relative);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.