简体   繁体   English

无效的URI:无法确定URI的格式 - C#

[英]Invalid URI: The format of the URI could not be determined - C#

Im trying to set the source of a resource dictionary in c# to a location of a folder within the project, but get the above error. 我试图将c#中的资源字典的源设置为项目中文件夹的位置,但是得到上述错误。

Could someone advise what the issue is please? 有人可以建议问题是什么吗?

Heres the code: 下面是代码:

myResourceDictionary.Source = new Uri("../Resources/Styles/Shared.xaml");

Please let me know if you need anymore information. 如果您需要更多信息,请告诉我。

你必须使用UriKind.Relative

myResourceDictionary.Source = new Uri("../Resources/Styles/Shared.xaml",  UriKind.Relative);

I kept running into the same issue even after passing the UriKind.Relative argument. 即使在传递了UriKind.Relative参数之后,我仍然遇到了同样的问题。 The weird bit was that some of the Uris to XAML pages were working using the method @Magnus suggested - but most of them would throw the: 奇怪的是,一些Uris到XAML页面正在使用@Magnus建议的方法 - 但是大多数都会抛出:

The format of the URI could not be determined

exception. 例外。

Finally, I read up on Pack Uris for WPF applications which solved my problem a 100%. 最后,我阅读了Pack Uris for WPF应用程序,它解决了我的问题100%。

I started using urls like this: 我开始使用这样的网址:

// ResourceFile is in the Root of the Application
myResourceDictionary.Source = 
    new Uri("pack://application:,,,/ResourceFile.xaml", UriKind.RelativeOrAbsolute);

or 要么

// ResourceFile is in the Subfolder of the Application
myResourceDictionary.Source = 
    new Uri("pack://application:,,,/SubFolder/ResourceFile.xaml", UriKind.RelativeOrAbsolute);

Hope this helps someone struggling with the same issues I was facing. 希望这有助于某些人为我所面临的同样问题而苦苦挣扎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM