简体   繁体   中英

My WPF application doesn't open after I put an image through XAML (Visual Studio)

I have some practice with Windows Forms, but now I decided to experiment with WPF in Visual Studio; Through XAML I have put an image in the Grid, called Street2.jpg, and the image can be seen in the studio. But if I try to start the application it just doesn't open (altough it generates a proccess in my task manager), and if I debug it, a message is shown:

在此处输入图片说明

Here's my XAML code (I haven't inserted anything in the C# code yet):

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WhenItAllEndedWPF.MainWindow"
    Title="MainWindow" Height="600" Width="800" Background="{x:Null}">
<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="Images\Street2.jpg"/>
    </Grid.Background>

</Grid>

What could I do to fix this problem? Thanks for the help!

[EDIT]

This was showed in the output tab:

The thread 0x1f0c has exited with code 259 (0x103). 'WhenItAllEndedWPF.vshost.exe' (CLR v4.0.30319: WhenItAllEndedWPF.vshost.exe): Loaded 'c:\\users\\vini\\documents\\visual studio 2013\\Projects\\When It All Ended WPF\\WhenItAllEndedWPF\\bin\\Debug\\WhenItAllEndedWPF.exe'. Symbols loaded. 'WhenItAllEndedWPF.vshost.exe' (CLR v4.0.30319: WhenItAllEndedWPF.vshost.exe): Loaded 'C:\\Windows\\Microsoft.Net\\assembly\\GAC_MSIL\\System.Configuration\\v4.0_4.0.0.0__b03f5f7f11d50a3a\\System.Configuration.dll'. Cannot find or open the PDB file.

This is likely to be because the file that you have specified has not been found ("Images/Street2.jpg")

Please see the inner exception.. I expect something such as this...

"Cannot locate resource 'images/Street2.jpg"

Please ensure that the file exists in the location you are expecting it: The build action should be Resource (the default).

Also.. Visual Studio will give intellisense for the image path so just check that you have not done something silly such as use the wrong extension (jpg / jpeg) which is something I do from time to time.

Have you tried making sure that the image is set to content in your solution explorer? This can be done by right clicking selecting properties and then changing "Build Action" to "Content".

Try to specify Image source like this:

<ImageBrush ImageSource="/WhenItAllEndedWPF;component/Images/Street2.jpg" />

It should be valid if your projet name is WhenItAllEndedWPF and if You have Street2.jpg inside Images folder in projects root.

Also check, is your jpg file is included in project (visible in solution explorer when non-included files are hidden).

Also ensure, that your output folder or files in it are not locked by some other application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM