找不到 WPF 目录异常和演示文稿核心.pdb未加载

本文关键字:核心 文稿 pdb 加载 WPF 异常 找不到 | 更新日期: 2023-09-27 18:33:47

我正在尝试将背景图像添加到文本框中,但在 textChange 事件中,图像按预期消失,但是如果我退格或删除文本框中的文本使其为空,我得到DirectoryNotFoundException被处理了。 和目录:

找不到路径"C:''myProjectFolder''bin''Debug"的一部分。img''txtBackground.png'.

XAML:

<TextBox Name="myTextBox" Width="200" TextChanged="myTextBox_TextChanged">
<TextBox.Background>
<ImageBrush ImageSource="img/txtBackground.png" />
</TextBox.Background>

C# 代码:

private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (myTextBox.Text == "")
{
ImageBrush textImageBrush = new ImageBrush();
textImageBrush.ImageSource =
new BitmapImage(
    new Uri(@"..img/txtBackground.png", UriKind.Relative)
);
myTextBox.Background = textImageBrush;    
}
else
{
myTextBox.Background = null;
}
}

删除了引用,重新添加了它们,构建/清理解决方案并重建,但没有任何东西。这些错误仅在我尝试将背景添加到文本框时发生。

找不到 WPF 目录异常和演示文稿核心.pdb未加载

假设包含图像的 img 文件夹位于项目下(不在调试文件夹下,理想情况下它不应位于调试文件夹中),并且图像的 BuildAction 设置为资源,您可以尝试以下操作:

new BitmapImage(new Uri("pack://application:,,,/img/txtBackground.png", UriKind.Absolute));

如果您在调试文件夹中有img,那么您必须达到该文件夹

new Uri(@"bin/Debug/img/txtBackground.png", UriKind.Relative)

您的 Uri 应为

new Uri(@"/img/txtBackground.png", UriKind.Relative)

至少错误消息是这么说的。

你也可以试试这个:

textImageBrush.ImageSource =new BitmapImage("/Projectname;Component/img/txtBackground.png");

图像的Build Action必须设置为 Resource