System.Windows.Baml2006.TypeConverterMarkupExtension

时间:2014-09-12 19:58:24

标签: c# wpf xaml imagesource

你好我确实有很多人似乎有的问题,但我尝试了建议的解决方案,他们没有工作。我有这个代码总是抛出一个异常(消息:“在'System.Windows.Baml2006.TypeConverterMarkupExtension'上提供一个异常的值')在Window.InitializeComponent()Methode。 这是我的Xaml代码:

<Window x:Class="SimpleMoving.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Name="SquareFighter" Height="392" Width="621" KeyDown="Window_KeyDown" KeyUp="Window_KeyUp" ResizeMode="CanMinimize">
    <Canvas Name="MyCanvas">
        <Rectangle Name="Box" Height="60" Canvas.Left="17" Stroke="Black" Canvas.Top="172" Width="30">
            <Rectangle.Fill>
                <ImageBrush ImageSource="pack://application:,,,/Resources/Peanut.png"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Name="Rec1" Height="30" Canvas.Left="908" Stroke="Black" Canvas.Top="50" Width="30">
            <Rectangle.Fill>
                <ImageBrush ImageSource="pack://application:,,,/Resources/Rock.png"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Height="30" Canvas.Left="908" Stroke="Black" Canvas.Top="110" Width="30">
            <Rectangle.Fill>
                <ImageBrush ImageSource="pack://application:,,,/Resources/Rock.png"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Height="30" Canvas.Left="908" Stroke="Black" Canvas.Top="169" Width="30">
            <Rectangle.Fill>
                <ImageBrush ImageSource="pack://application:,,,/Resources/Rock.png"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Height="30" Canvas.Left="908" Stroke="Black" Canvas.Top="229" Width="30">
            <Rectangle.Fill>
                <ImageBrush ImageSource="pack://application:,,,/Resources/Rock.png"/>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Height="30" Canvas.Left="908" Stroke="Black" Canvas.Top="289" Width="30">
            <Rectangle.Fill>
                <ImageBrush ImageSource="pack://application:,,,/Resources/Rock.png"/>
            </Rectangle.Fill>
        </Rectangle>
        <Label Name="LevelLabel" Content="Level: 1" Canvas.Left="54" Canvas.Top="10" Height="38" Width="164" FontSize="20"/>
        <Label Content="Shoot the squares back using WSDA!" Canvas.Left="180" Width="290" FontSize="16"/>
    </Canvas>
</Window>

我不知道该怎么办。我希望你能提供帮助。

1 个答案:

答案 0 :(得分:3)

您很可能没有位于Resources文件夹下的图片。

确保以下内容对XAML中使用的图像(Rock.png和Peanut.png)有效:

  1. 图片在同一个项目中 added in project under Resources folder
  2. 图片的
  3. Build Action 设置为 Resource

  4. 此外,您可以尝试使用更详细的Pack URI定义:

    "pack://application:,,,/SimpleMoving;component/Resources/Peanut.png""pack://application:,,,/SimpleMoving;component/Resources/Rock.png",其中SimpleMoving是程序集的名称。