我试图在Visual 2013中创建一个带有ImageBrush背景的非常简单的窗口,作为对一个更复杂项目的测试。该图像显示在设计师,但程序崩溃时,我启动它。以下是XAML:
<Window x:Class="BackgroundTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="grid1">
<Grid.Background>
<ImageBrush ImageSource="/Images\Koala.jpg" Stretch="Fill"/>
</Grid.Background>
</Grid>
</Window>基本上是空的C# (就像我说的,只是一个测试):
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace BackgroundTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}我确信这与ImageBrush有关,如果我删除XAML的这一部分,程序将按预期运行一个空白窗口。有人能帮我跑吗?
以下是调试输出:System.Windows.Markup.XamlParseException类型的第一次异常发生在PresentationFramework.dll中
附加信息:“提供'System.Windows.Baml2006.TypeConverterMarkupExtension‘上的值,抛出一个例外”。行号'7‘和线位置'14’。
发布于 2014-01-23 01:49:34
明白了!通过右键单击解决方案资源管理器中的解决方案名称( -> Properties -> resources -> Add Resource-> Add Resource -> ),并选择Koala的图片,我将该图像添加到程序的资源中。我将XAML中的代码更改为:
ImageSource="Resources/Koala.jpg"根据Eric的建议,我在解决方案资源管理器中单击了Resources,当然,图片就在那里,我能够将构建操作更改为Resource。在那之后,它完美地运作了,谢谢埃里克!一旦我有足够的声誉,我就会检查你的答案。我对堆栈溢出相当陌生。
发布于 2014-01-23 01:33:55
可以将图像的生成操作设置为Content。将其改为资源。
发布于 2014-01-23 00:11:12
你的图像源里有反斜杠..。
假设Images是解决方案中的一个文件夹,您的图像源应该是:
Source="../Images/Koala.jpg"甚至更好:
Source="pack://application:,,,/Images/Koala.jpg"https://stackoverflow.com/questions/21296429
复制相似问题