如何在 WPF 中使用 GetResourceStream 加载图标?
我已将一个图标添加到我的 WPF 项目(BuildAction 设置为 Resource),现在我尝试将该图标加载到流中:
using(Stream iconStream = Application.GetResourceStream(new Uri("red.ico")).Stream)
{
// use the stream
}
这给了我以下错误:
无效的 URI:无法确定 URI 的格式。
我尝试更改 Uri 构造以包含 UriKind.Relative。这给出:
无法找到资源“red.ico”。
我看过有关此的各种文章例如(来自SO),我可以看不出我做错了什么。
非常感谢任何帮助。
I've added an icon to my WPF project (BuildAction set to Resource) and I'm now trying to load that icon into a stream:
using(Stream iconStream = Application.GetResourceStream(new Uri("red.ico")).Stream)
{
// use the stream
}
This gives me the following error:
Invalid URI: The format of the URI could not be determined.
I've tried altering the Uri construction to include UriKind.Relative. This gives:
Cannot locate resource 'red.ico'.
I've looked at various articles on this For Example (from SO) and I can't see what I'm doing wrong.
Any help greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试类似
new Uri("pack://application:,,,/red.ico")
,请参阅 此页面了解有关 WPF 中 URI 的详细信息。Try something like
new Uri("pack://application:,,,/red.ico")
, see this page for more info on URIs in WPF.最终,我的问题归结为这样一个事实:我为 WPF 应用程序创建了一个自定义入口点,并且在调用应用程序的静态构造之前尝试创建一个 URI。
代码从这样的: 更改
为这样的:
Ultimately my issue boiled down to the fact that I had created a custom entry point for my WPF application and I was attempting to create a URI before the Application's static constructions had been called.
The code changed from something like this:
to something like this: