图像未在XAML代码中显示

时间:2015-11-11 20:57:59

标签: c# xaml windows-store-apps

在每个GridViewItem中都不显示图像。在XAML的“设计”视图中,在Visual Studio中显示图像,但是当我运行代码时,不显示任何图像。以下是GridViewItem的代码片段:

<GridViewItem x:Name="Performance" Margin="0,0,0,0" Width="207" Height="275" HorizontalAlignment="Left" VerticalAlignment="Top">
     <Grid HorizontalAlignment="Left" Height="268" Width="207" VerticalAlignment="Top">
          <Image Margin="0" Source="Images\performanceLaptop.JPG" Stretch="UniformToFill" Width="207" Height="268" />
          <Grid HorizontalAlignment="Left" Height="51" Margin="0" VerticalAlignment="Bottom" Width="207" Background="#E5231F20">
               <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" TextWrapping="Wrap" Text="Performance" VerticalAlignment="Center" FontSize="26.667"/>
          </Grid>
      </Grid>
</GridViewItem>

整个代码:

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="ms-appx:/Images/SampleAppBg.jpg"/>
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="Sample Text Header " Margin="80,36,870,44" FontSize="50" VerticalAlignment="Center" FontStyle="Italic"/>
            <TextBlock Grid.Column="0" HorizontalAlignment="Left" Margin="80,0,0,10" TextWrapping="Wrap" Text="Sample Category :" VerticalAlignment="Bottom" FontFamily="Segoe UI" FontSize="30" FontStyle="Italic"/>
        </Grid>
        <Grid Grid.Row="1" Background="Transparent" HorizontalAlignment="Left" Height="526" Margin="120,0,0,0" VerticalAlignment="Top" Width="1170">
            <StackPanel HorizontalAlignment="Left" Height="500" Margin="0" VerticalAlignment="Center" Width="1125" Orientation="Horizontal">
                <GridViewItem x:Name="Performance" Margin="0,0,0,0" Width="207" Height="275" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <Grid HorizontalAlignment="Left" Height="268" Width="207" VerticalAlignment="Top">
                        <Image Margin="0" Source="Images\1.JPG" Stretch="UniformToFill" Width="207" Height="268" />
                        <Grid HorizontalAlignment="Left" Height="51" Margin="0" VerticalAlignment="Bottom" Width="207" Background="#E5231F20">
                            <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" TextWrapping="Wrap" Text="Performance" VerticalAlignment="Center" FontSize="26.667"/>
                        </Grid>
                    </Grid>
                </GridViewItem>
                <GridViewItem x:Name="INSTRUCTIONS" Margin="15,0,0,0" Width="207" Height="275" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <Grid HorizontalAlignment="Left" Height="268" Width="207" VerticalAlignment="Top">
                        <Image Margin="0" Source="2.JPG" Stretch="Fill" Width="207" Height="268"/>
                        <Grid HorizontalAlignment="Left" Height="51" Margin="0" VerticalAlignment="Bottom" Width="207" Background="#E5231F20">
                            <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" TextWrapping="Wrap" Text="Entertainment" VerticalAlignment="Center" FontSize="26.667"/>
                        </Grid>
                    </Grid>
                </GridViewItem>
                <GridViewItem x:Name="PLAY" Margin="15,0,0,0" Width="207" Height="275" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <Grid HorizontalAlignment="Left" Height="268" Width="207" VerticalAlignment="Top">
                        <Image Margin="0" Source="3.JPG" Stretch="Fill" Width="207" Height="268"/>
                        <Grid HorizontalAlignment="Left" Height="51" Margin="0" VerticalAlignment="Bottom" Width="207" Background="#E5231F20">
                            <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" TextWrapping="Wrap" Text="Student" VerticalAlignment="Center" FontSize="26.667"/>
                        </Grid>
                    </Grid>
                </GridViewItem>
                <GridViewItem x:Name="SETTINGS" Margin="15,0,0,0" Width="207" Height="275" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <Grid HorizontalAlignment="Left" Height="268" Width="207" VerticalAlignment="Top">
                        <Image Margin="0" Source="4.JPG" Stretch="Fill" Width="207" Height="268"/>
                        <Grid HorizontalAlignment="Left" Height="51" Margin="0" VerticalAlignment="Bottom" Width="207" Background="#E5231F20">
                            <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" TextWrapping="Wrap" Text="Professional" VerticalAlignment="Center" FontSize="26.667"/>
                        </Grid>
                    </Grid>
                </GridViewItem>
                <GridViewItem x:Name="SCORES" Margin="15,0,0,0" Width="207" Height="275" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <Grid HorizontalAlignment="Left" Height="268" Width="207" VerticalAlignment="Top">
                        <Image Margin="0" Source="5.JPG" Stretch="Fill" Width="207" Height="268"/>
                        <Grid HorizontalAlignment="Left" Height="51" Margin="0" VerticalAlignment="Bottom" Width="207" Background="#E5231F20">
                            <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" TextWrapping="Wrap" Text="Custom" VerticalAlignment="Center" FontSize="26.667"/>
                        </Grid>
                    </Grid>
                </GridViewItem>
            </StackPanel>
        </Grid>
    </Grid>

1 个答案:

答案 0 :(得分:2)

\属性中的Source=的斜杠更改为/。重建您的解决方案。

<Image Margin="0" Source="Images/performanceLaptop.jpg"  ../>

More information

在您的项目中,转到文件夹Images,然后用鼠标右键单击SampleAppBg.jpg并选择“属性”。在“属性”工具窗口中,将文件类型设置为“资源”。

对您的文件performanceLaptop.jpg执行相同操作。虽然“资源”是添加文件的默认值。

Resource有点装入您的可执行文件或库中,并在XAML中可用。