开发者

Bizarre problem with WPF XAML file

I've just started a very simple WPF application which consists of a main large image and four smaller images.

In order to assist with the layout, I created some JPEGs in MsPaint containing the images -2, -1, 0, +1 and +2 and just copied them into the top level of the project directory.

The XAML segment contains, for the five images:

<Image Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="4" Grid.RowSpan="1"
    Margin="0,0,0,0" Name="imgPicture" Stretch="Fill" VerticalAlignment="Top"
    Source="file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/zero.jpg" />
<Image Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="1" Grid.RowSpan="1"
    Margin="0,0,0,0" Name="imgPicMinus2" Stretch="Fill" VerticalAlignment="Top"
    Source="file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/minus2.jpg" />
<Image Grid.Column="2" Grid.Row="4" Grid.ColumnSpan="1" Grid.RowSpan="1"
    Margin="0,0,0,0" Name="imgPicMinus1" Stretch="Fill" VerticalAlignment="Top"
    Source="file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/minus1.jpg" />
<Image Grid.Column="3" Grid.Row="4" Grid.ColumnSpan="1" Grid.RowSpan="1"
    Margin="0,0,0,0" Name="imgPicPlus1" Stretch="Fill" VerticalAlignment="Top"
    Source="开发者_如何学编程file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/plus1.jpg" />
<Image Grid.Column="4" Grid.Row="4" Grid.ColumnSpan="1" Grid.RowSpan="1"
    Margin="0,0,0,0" Name="imgPicPlus2" Stretch="Fill" VerticalAlignment="Top"
    Source="file:///C:/DAndS/Pax/MyDocs/VS2008/Projects/MyProj/plus2.jpg" />

When I try to set the source property for the plus2 image, it complains with a dialog box stating:

Property value is not valid.
Details
   |
   V
   The file plus2.jpg is not part of the project or
   its 'Build Action' property is not set to 'Resource'.

Yet if I rename the file to plus3.jpg or plus2x.jpg, I don't have that problem.

Why is it complaining about plus2.jpg specifically?


  • Is it possible that you previously added then removed plus2.jpg?

  • Is it possible that you have not built the solution between the steps?

    • Clear src, build solution, set src to known working image, build solution, set src to non-working image.

    • Clear src of all images, build solution, set "Build Action" of all images to "None", build solution, set build-action to "Resource", build solution, set src of all images.

  • (Most Likely) Possible corrupted image, image with invalid color palette, image saved in invalid format.

    • See if you can load the image during run-time
    • Load image with Resouce Editor and "Save As".
    • The file extension does not make a difference, because the internal graphics format determines the image type. Meaning, if you created a jpg but saved it as bmp, the designer will still load the image correctly.
  • Try <Image.Source> <BitmapImage UriSource="sampleImages/bananas.jpg" /> </Image.Source>

I tried to duplicate the problem with no luck. I was able to use images that I "added existing" and images where I specified src="file:///c:..." Images inside the project, with and without the build-action set. Even images outside the project.

My other thought is there is something wrong with the image, but I did not add the comment. Yet, now that I think about it, the designer is going to try and load the image once you set src. If the image cannot be loaded then an exception will be thrown. Since you are in design-time, you get the invalid property prompt with an "obsure" details message.

What if you load the image during run-time to see what kind of exception is thrown, if any.


Don't really care about it. VS2008 when creating WPF applications has dozens of such warnings and most of them disappears after build. If app starts and is working - leave it alone. Personally I've installed around 10-15 hotfixes for various WPF related bugs in VS2008. VS2010 is much better.

Besides it's probably better to include image files in resources - you add them to the project (Add existing item) and then check whether the build action is set to Resource. (right click on that file in Solution Explorer and choose Properties)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜