C# 未找到WPF目录异常,未加载PresentationCore.pdb

C# 未找到WPF目录异常,未加载PresentationCore.pdb,c#,wpf,visual-studio,C#,Wpf,Visual Studio,我试图将背景图像添加到文本框中,但在textChange事件中,图像会按预期消失,但是如果我退格或删除文本框中的文本,使其为空,我会得到处理的目录NotFoundException。 和目录: 找不到路径“C:\myProjectFolder\bin\Debug..img\txtBackground.png”的一部分 XAML: <TextBox Name="myTextBox" Width="200" TextChanged="myTextBox_TextChanged"> &l

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

找不到路径“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>
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;
}
}
删除引用,重新添加它们,生成/清除解决方案并重新生成,但什么都没有。 这些错误仅在我尝试将背景添加到文本框时发生。

您的Uri应为

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

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

假设具有映像的img文件夹位于项目下(不在调试文件夹下,最好不在调试文件夹中),并且映像的BuildAction设置为Resource,您可以尝试以下操作:

new BitmapImage(new Uri("pack://application:,,,/img/txtBackground.png", UriKind.Absolute));
textImageBrush.ImageSource =new BitmapImage("/Projectname;Component/img/txtBackground.png");
如果您在调试文件夹中有img,那么您必须达到这个目标

new Uri(@"bin/Debug/img/txtBackground.png", UriKind.Relative)
您也可以尝试以下方法:

new BitmapImage(new Uri("pack://application:,,,/img/txtBackground.png", UriKind.Absolute));
textImageBrush.ImageSource =new BitmapImage("/Projectname;Component/img/txtBackground.png");

图像的
生成操作
必须设置为
资源

我尝试过,但仍然得到错误:我在项目中有其他图像引用如下:../img/imagename,它们工作正常。那么您的路径中缺少斜线了吗?看看你提供的uri,上面写的是“.img”,而不是“.img”。/img“我已经用各种可能的方法尝试过了:../img/image或img/or/img,但我仍然得到了错误,是调试文件夹吗?是的,我已将img文件夹添加到调试文件夹中。理想情况下,您应该将img文件夹添加到项目中,并使用BuildAction将图像添加到该文件夹中,因为资源实际起到了作用:但我更改了以上内容:新建BitmapImage(Uri=新建Uri()pack://application:,,,/img/txtBackground.png“,UriKind.Absolute”);为此:新建位图图像(新Uri(“pack://application:,,,/img/txtBackground.png“,UriKind.Absolute”);非常感谢:该图像不在调试文件夹中,我已将其删除,它位于项目文件夹下。生成操作设置为:Resources。非常感谢你:这是一种痛苦。