设置 WPF 用户控件图标时无法识别 URI 前缀错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14356161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:03:42  来源:igfitidea点击:

The URI prefix is not recognized Error While Setting WPF UserControl Icon

c#wpfuser-controlsuri

提问by Simsons

I am creating a WPF window and loading a user control inside like below:

我正在创建一个 WPF 窗口并在其中加载一个用户控件,如下所示:

        Uri uri = new Uri("Views/ApplicationInfo.xaml", UriKind.RelativeOrAbsolute);

        UserControl versionInfoUserControl = (UserControl)Application.LoadComponent(uri);

        #region Initizalizing the Window, Winodw Proporties and Icon
        Window versionWindow = new Window();


        versionWindow.Height = 250;
        versionWindow.Width = 400;
        versionWindow.ResizeMode = ResizeMode.NoResize;

Now When I am trying to add an application icon like below:

现在,当我尝试添加如下所示的应用程序图标时:

 versionWindow.Icon = new BitmapImage(new Uri(@"pack://application:,,component/Images/Ico.png"));

I am getting URI prefix is not recognized error.

我收到 URI 前缀无法识别错误。

*Do I need to change applicationwith Application name like:

*我是否需要使用应用程序名称更改应用程序,例如:

 versionWindow.Icon = new BitmapImage(new Uri(@"pack://MyApp.MVVM.WPF:,,component/Images/Ico.png"));

Even then I am getting the same error

即使那样我也遇到同样的错误

回答by David

Assume I have a project with a folder named Assets and inside a have a png image with build action Resource or Embedded resource.

假设我有一个文件夹名为 Assets 的项目,里面有一个带有构建操作资源或嵌入式资源的 png 图像。

Then this works:

然后这有效:

var versionWindow = new Window
                                {
                                    Height = 250,
                                    Width = 400,
                                    ResizeMode = ResizeMode.NoResize,
                                    Icon = new BitmapImage(new Uri(@"pack://application:,,,/Assets/icon.png"))
                                };

        versionWindow.Show();

回答by Shaik Inthiyaz

versionWindow.Icon =  BitmapFrame.Create(new Uri("pack://application:,,,/YourProjectName;component/Images/computer.ico", UriKind.RelativeOrAbsolute))