WPF图像在运行时显示但在设计器中不显示

huangapple go评论48阅读模式
英文:

WPF image showing up in runtime but not in designer

问题

更新: 问题已解决,请参考Volatile Pulse的回答。

我在我的WPF应用程序(.NET 5.0)中有一个图像控件,在运行时显示图像没有问题,但在设计时不显示。

以下是XAML:

<Window>
    <Grid x:Name="MainGrid" 
        VerticalAlignment="Stretch" 
        HorizontalAlignment="Stretch" 
        SnapsToDevicePixels="True">

        <Grid.RowDefinitions>
            <RowDefinition x:Name="BannerArea" Height="192"/>
            <RowDefinition x:Name="MainArea" Height="2.5*"/>
        </Grid.RowDefinitions>

        <Border x:Name="BannerImageBorder" Grid.Row="0">
            <Image x:Name="BannerBottonLayerImage"
                Source="{DynamicResource BannerImageBackground}"
                HorizontalAlignment="Left"
                Width="1550"
                SnapsToDevicePixels="True"
                RenderOptions.BitmapScalingMode="HighQuality">
            </Image>
        </Border>
    </Grid>
</Window>

<Window.Resources>
    <BitmapImage x:Key="BannerImageBackground" UriSource="Resources\BannerImageBackground.png"/>
</Window.Resources>

我已将"BannerImageBackground.png"的生成操作设置为"Resource"。

图像在运行时显示没有问题,但在设计时消失。有趣的是,如果我更改XAML以强制出现语法错误,然后将其改回来,图像会在设计时重新出现,但如果在VS中运行应用程序,它会再次消失。

到目前为止,我尝试过:

  • 重命名图像文件名和资源键名,没有效果。
  • 将图像移动到另一个文件夹,没有效果。
  • 将图像声明为"BannerImageBorder"的资源,并将源设置为静态资源,没有效果。
  • 将图像的生成操作设置为"Content",并将"复制到输出目录"设置为"始终复制",没有效果。

我的项目目录路径不包含任何特殊字符。

我在网上搜索,这是我能找到的其他人发布的最接近的问题: .Net Core 3: Image disappear in Designer after run,在该帖子中,Visual Studio团队表示这个问题已经在VS 2019 16.5 Preview 2中修复,但我正在使用16.11.26,这个问题仍然存在...

我不知道我是否做错了什么,还是只是一个bug?这肯定不会阻碍任何事情,只是非常烦人。

提前感谢。

英文:

Update: Issue resolved, please refer to Volatile Pulse's answer below.

I have an image control in my WPF application(.net 5.0), which shows the image at runtime with no issue, but not showing up in designer.

Here's the XAML:

&lt;Window&gt;
    &lt;Grid x:Name=&quot;MainGrid&quot; 
        VerticalAlignment=&quot;Stretch&quot; 
        HorizontalAlignment=&quot;Stretch&quot; 
        SnapsToDevicePixels=&quot;True&quot;&gt;

        &lt;Grid.RowDefinitions&gt;
            &lt;RowDefinition x:Name=&quot;BannerArea&quot; Height=&quot;192&quot;/&gt;
            &lt;RowDefinition x:Name=&quot;MainArea&quot; Height=&quot;2.5*&quot;/&gt;
        &lt;/Grid.RowDefinitions&gt;

        &lt;Border x:Name=&quot;BannerImageBorder&quot; Grid.Row=&quot;0&quot;&gt;
             &lt;Image x:Name=&quot;BannerBottonLayerImage&quot;
                Source=&quot;{DynamicResource BannerImageBackground}&quot;
                HorizontalAlignment=&quot;Left&quot;
                Width=&quot;1550&quot;
                SnapsToDevicePixels=&quot;True&quot;
                RenderOptions.BitmapScalingMode=&quot;HighQuality&quot;&gt;
             &lt;/Image&gt;
        &lt;/Border&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;

&lt;Window.Resources&gt;
    &lt;BitmapImage x:Key=&quot;BannerImageBackground&quot; UriSource=&quot;Resources\BannerImageBackground.png&quot;/&gt;
&lt;/Window.Resources&gt;

I've set the build action for "BannerImageBackground.png" to "Resource".

The image shows up in runtime without a problem while in designer it disappears. Interestingly enough, if I change the XAML to force a syntax error then change it back, the image re-appears in designer, only to disappear again if I run the application in VS.

What I have tried so far:

  • Rename both image's file name and resource key name, no avail.
  • Move the image to another folder, no avail.
  • Declare the image as a resource of the border "BannerImageBorder" and set the source as static resource, no avail.
  • Set the image's build action to "Content" and "Copy to Output Directory" to "Copy Always", no avail.

My project's directory path doesn't contain any special characters.

I searched online and that's the closest issue posted by others that I can find: .Net Core 3: Image disappear in Designer after run, in that thread the Visual Studio team says this bug was already fixed in VS 2019 16.5 Preview 2, but I'm using 16.11.26 yet this problem still persists...

I don't know if I did something wrong or it's just a bug? It's certainly not blocking anything but just super annoying.

Thanks in advance.

答案1

得分: 1

更新:

我成功找到了一个可行的解决方案来解决我的问题,但我不确定它对你的问题是否有帮助。将所有图像存储在专用的 ResourceDictionary 文件中,然后通过 App.xaml 引用该文件,允许设计师在不硬编码路径的情况下显示 StaticResource 图像。

我已经修改下面的代码,使其成为一个最小可复现的示例,并希望你能验证是否在使用新解决方案时仍然存在你的问题。如果这行不通,尝试指定绝对路径是否能够正确工作,而不是使用 StaticResource 或相对路径。

原始:

我声望不足,无法评论,但我认为我与你遇到了相同的问题,配置却大不相同。然而,我在设计时根本看不到图像。我使用 Visual Studio 2022,项目是 .Net Framework 4.8

经过大量研究,我只能在图像源是 URI 或资源在要使用的文件中定义时,设计时才能显示图像。以下三种解决方案在运行时都有效,但我想使用的那个在设计时无法工作。

不幸的是,无论我尝试什么,都无法使其暂时显示。

更新后的代码:

<!-- Images.xaml -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <BitmapImage x:Key="ResourceImage" UriSource="/Image.png" />
</ResourceDictionary>
<!-- App.xaml -->
<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="/Main.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Images.xaml" />
            </ResourceDictionary.MergedDictionaries>
            
            <BitmapImage x:Key="AppImage" UriSource="/Image.png" />
        </ResourceDictionary>
    </Application.Resources>
</Application>
<!-- Main.xaml -->
<Window x:Class="WpfApp1.Main"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Images.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <BitmapImage x:Key="MainImage" UriSource="/Image.png" />
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <StackPanel>
            <!-- 未显示 - "App.xaml" -->
            <Image Source="{StaticResource AppImage}" Width="100" Height="100" />
            
            <!-- 显示 - "Images.xaml" 通过 "App.xaml" -->
            <Image Source="{StaticResource ResourceImage}" Width="100" Height="100" />

            <!-- 显示 - "ResourceDictionary" -->
            <Image Source="{StaticResource MainImage}" Width="100" Height="100" />

            <!-- 显示 - "/Image.png" -->
            <Image Source="/Image.png" Width="100" Height="100" />
        </StackPanel>
    </Grid>
</Window>

设计师输出:

WPF图像在运行时显示但在设计器中不显示

英文:

Update:

I managed to find an acceptable workaround to my issue, but I'm not sure it will be helpful for your issue. All images being stored in a dedicated ResourceDictionary file and then referencing that file via App.xaml allows the designer to show the StaticResource images without hard coding their paths.

I've modified the below code to be a minimally reproducible example and was hoping you could verify if your issue persists using a new solution. If that doesn't work, does specifying the absolute path work correctly for you instead of using the StaticResource or relative paths?

Original:

I don't have enough reputation yet to comment, but I believe I'm experiencing the same issue as you with a significantly different configuration. I'm unable to see the images in the designer at all though. I'm using Visual Studio 2022 with a .Net Framework 4.8 project.

After tons of research I've only managed to show the image in the designer when the image source is a URI or the resource is defined in the same file where the resource is to be used. All three of the following solutions work for runtime, but the one that doesn't work for design time is the one I want to use.

And unfortunately no matter what I try, I can't get it to temporarily show either.

Updated Code:

&lt;!-- Images.xaml --&gt;
&lt;ResourceDictionary xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
                    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;&gt;
    &lt;BitmapImage x:Key=&quot;ResourceImage&quot; UriSource=&quot;/Image.png&quot; /&gt;
&lt;/ResourceDictionary&gt;
&lt;!-- App.xaml --&gt;
&lt;Application x:Class=&quot;WpfApp1.App&quot;
             xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
             StartupUri=&quot;/Main.xaml&quot;&gt;
    &lt;Application.Resources&gt;
        &lt;ResourceDictionary&gt;
            &lt;ResourceDictionary.MergedDictionaries&gt;
                &lt;ResourceDictionary Source=&quot;/Images.xaml&quot; /&gt;
            &lt;/ResourceDictionary.MergedDictionaries&gt;
            
            &lt;BitmapImage x:Key=&quot;AppImage&quot; UriSource=&quot;/Image.png&quot; /&gt;
        &lt;/ResourceDictionary&gt;
    &lt;/Application.Resources&gt;
&lt;/Application&gt;
&lt;!-- Main.xaml --&gt;
&lt;Window x:Class=&quot;WpfApp1.Main&quot;
        xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;&gt;
    &lt;Window.Resources&gt;
        &lt;ResourceDictionary&gt;
            &lt;ResourceDictionary.MergedDictionaries&gt;
                &lt;ResourceDictionary Source=&quot;Images.xaml&quot; /&gt;

            &lt;/ResourceDictionary.MergedDictionaries&gt;
            &lt;BitmapImage x:Key=&quot;MainImage&quot; UriSource=&quot;/Image.png&quot; /&gt;
        &lt;/ResourceDictionary&gt;
    &lt;/Window.Resources&gt;

    &lt;Grid&gt;
        &lt;StackPanel&gt;
            &lt;!-- Not Displayed - &quot;App.xaml&quot; --&gt;
            &lt;Image Source=&quot;{StaticResource AppImage}&quot; Width=&quot;100&quot; Height=&quot;100&quot; /&gt;
            
            &lt;!-- Displayed - &quot;Images.xaml&quot; via &quot;App.xaml&quot; --&gt;
            &lt;Image Source=&quot;{StaticResource ResourceImage}&quot; Width=&quot;100&quot; Height=&quot;100&quot; /&gt;

            &lt;!-- Displayed - &quot;ResourceDictionary&quot; --&gt;
            &lt;Image Source=&quot;{StaticResource MainImage}&quot; Width=&quot;100&quot; Height=&quot;100&quot; /&gt;

            &lt;!-- Displayed - &quot;/Image.png&quot; --&gt;
            &lt;Image Source=&quot;/Image.png&quot; Width=&quot;100&quot; Height=&quot;100&quot; /&gt;
        &lt;/StackPanel&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;

Designer Output:

WPF图像在运行时显示但在设计器中不显示

huangapple
  • 本文由 发表于 2023年5月17日 12:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定