【问题标题】:WPF UserControl Runtime error: Unable to find path of the icon in UserControlWPF UserControl 运行时错误:无法在 UserControl 中找到图标的路径
【发布时间】:2020-05-23 05:39:32
【问题描述】:

在我的WPF User Control 中,我添加了一个图标(如下面的 XAML 所示)。但是在运行时,用户控件的以下代码中的InitializeComponent()调用给出了如下所示的错误:

UserControl1.xaml.cs

public partial class UserControl1 : System.Windows.Window
    {
        public UserControl1()
        {
            InitializeComponent();
        }
…………….
}

用户控件的 XAML(带图标):

<Window x:Class="MyProject.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyProject"
             mc:Ignorable="d"
            "d:DesignHeight="450" d:DesignWidth="800"
             Icon="pack://siteoforigin:,,,/Resources/test_32.png">
    <Grid>
………………
    </Grid>

错误:

DirectoryNotFoundException:找不到路径“C:\MyFolder\VSTO\MyProject\bin\Debug\Resources\MyIcon.png”的一部分。

备注:

我注意到Resources 文件夹没有在Debug 文件夹下创建,尽管它是项目的一部分。我已将带有Build 操作的 MyIcon.png 文件设置为 Resource 和 Copy to output directory 设置为 Copy if newer

【问题讨论】:

    标签: c# wpf wpf-controls


    【解决方案1】:

    我不知道为什么,但是您应该将 application 与资源 Uri 一起使用,并将 siteoforigin 与其余的一起使用。

    您可以将 Uri 更改为:

    Icon="pack://application:,,,/Resources/test_32.png"
    

    或者,如果您想继续使用siteoforigin,而不是将构建选项更改为“内容”而不是“资源”。 相关主题:

    What is application's site of origin and when to use it

    pack-uris-in-wpf

    【讨论】:

    • 您的建议有效(谢谢)。我将构建操作更改为content。此外,您的参考资料也很有帮助(这是一种很好的做法,因此其他人也可以获得帮助)
    猜你喜欢
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    相关资源
    最近更新 更多