运行时不显示图像

Posted

技术标签:

【中文标题】运行时不显示图像【英文标题】:Image not displaying at Run time 【发布时间】:2018-01-18 09:29:03 【问题描述】:

图像在运行时未显示。它正在设计器中显示。

XAML:

<ScrollViewer Grid.Row="1" Grid.Column="1">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="20"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Image Grid.Row="0" Source="/Assets/logo_108x108.png" Stretch="Fill" Width="400"  VerticalAlignment="Top" HorizontalAlignment="Center" Height="133" Grid.RowSpan="3" />
            <phone:WebBrowser x:Name="DescriptionBrowser"  Grid.Row="1" Grid.RowSpan="2"  Navigating="DescriptionBrowser_Navigating" Margin="0,149,0,0"/>            
            <TextBlock x:Name="ErrorBlock" Visibility="Collapsed" Foreground="Black" FontSize="20" Grid.Row="1" Grid.Column="1" Text="Binding LocalizedResourcesFromCommonDll.ErrorGeneral, Source=StaticResource LocalizedCommonStrings" HorizontalAlignment="Center" VerticalAlignment="Center"  FontFamily="Segoe UI"/>
        </Grid>
    </ScrollViewer>
</Grid>

我试过更换图片标签,但还是不行。

【问题讨论】:

【参考方案1】:

我相信你的意思是这样的。尽量保持简单,不要设置你可能不需要的属性。

    <Grid Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Image Grid.Row="0" Source="/Resources/AnyLogo.png" />
        <ScrollViewer Grid.Row="1">
            <WebBrowser />
        </ScrollViewer>
        <TextBlock Grid.Row="2" Text="Binding TestText">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="Binding TestText" Value="x:Null">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Grid>

【讨论】:

我尝试按照上述方式实现,但图像仍然没有显示.. 在您的项目中,您有一个文件夹 Resources,其中的图片,您是否将“Build Action”的属性设置为“Resource”?这也应该有效: "pack://application:,,,/;component/Resources/" 其中 是 Assembly.GetEntryAssembly().GetName().Name;【参考方案2】:

我尝试了建议的答案,它对我有用,并带有以下备注:

Resources 目录与 View 处于同一级别,因此 Source 如下所示:Source="Resources/img.png"

另外,在图像的属性中,Build Action 设置为 Resource(默认情况下,但您仍然可以检查它)

【讨论】:

以上是关于运行时不显示图像的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI onDrag 在设备上运行时不显示预览图像

图像视图在运行时不呈现

只有ipad air,在Xcode 6编译app时不显示图像,其他iPad都可以

wpf image控件 设计时显示图片,运行时不显示

设置图像时不显示默认图像

使用屏幕管理器时不显示相机图像