The URI prefix is not recognized Error While Setti

2019-02-27 18:18发布

问题:

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

        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.

*Do I need to change application with 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

回答1:

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

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();


回答2:

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