【问题标题】:WPF Cannot see image in runtimeWPF 在运行时看不到图像
【发布时间】:2015-07-27 12:42:35
【问题描述】:

我为上下文菜单创建了 3 个图像。问题是我在运行时看不到它们。 (我可以在编辑器窗口中看到它们)

我已经

  • 将编译类型更改为资源
  • 图片类型为png(以防万一)

    <Color x:Key="BackgroundColor" A="255" R="19" G="19" B="19"/>
    
    <BitmapImage x:Key="BiCut" UriSource="Images/cut.tif"/>
    <BitmapImage x:Key="BiCopy" UriSource="Images/copy.tif"/>
    <BitmapImage x:Key="BiPaste" UriSource="Images/paste.tif"/>
    
    <SolidColorBrush x:Key="BorderBrush" Color="#ECECEC"/>
    
    <Style TargetType="ContextMenu">
        <Setter Property="Foreground" Value="{StaticResource BorderBrush}"/>
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Grid.IsSharedSizeScope" Value="true" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContextMenu">
                    <Border BorderThickness="1" BorderBrush="#2468d9" Padding="2" Background="#131313">
                        <StackPanel Orientation="Horizontal">
                            <StackPanel Orientation="Horizontal" Height="20">
                                <Image Source="{StaticResource BiCut}" Width="20"/>
                                <Button Content="Ausschneiden" Margin="5,0,0,0"/>
                                <TextBlock Text="Strg+X" TextAlignment="Center" VerticalAlignment="Center" Margin="5,0,0,0"/>
                            </StackPanel>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

【问题讨论】:

  • 你看这个问题了吗? stackoverflow.com/questions/8483002/…
  • 我的图片在我的项目中/Images/。所以我尝试了:&lt;Image Source="pack://application:,,,/Images/cut.png"/&gt;。再一次,我在编辑器中看到它,但在运行时看不到:(
  • 当您说“将编译类型更改为资源”时,您实际上是指您将其构建操作设置为资源?
  • 我多次发现以下链接很有帮助,它涵盖了多种场景,包括保存在子文件夹和其他程序集中的资源。 link
  • Ty,但我认为这不是问题,因为在我的编辑器中我可以看到图像?如果我错了,请纠正我

标签: c# wpf image runtime


【解决方案1】:

好的,我只需将加载到项目中的图像拖到Source="&lt;dragged image path&gt;" 即可解决此问题。它将插入图像的完整路径。

【讨论】:

    猜你喜欢
    • 2012-08-06
    • 2013-02-08
    • 2021-05-30
    • 1970-01-01
    • 2010-10-30
    • 2013-03-16
    相关资源
    最近更新 更多