简体   繁体   English

无法在设计视图中创建 UserControl 的实例,在发布模式和运行时工作

[英]Cannot create an instance of UserControl in design view, works in Release mode and at runtime

I have a user control LoadingIndicatorControl which is displayed in various other controls when loading is happened.我有一个用户控件LoadingIndicatorControl ,当加载发生时,它会显示在各种其他控件中。

This user control is very simple and looks like this:这个用户控件非常简单,看起来像这样:

<StackPanel HorizontalAlignment="Stretch"
            Visibility="{Binding Loading, Converter={StaticResource Bool2Vis}}">
    <Image x:Name="imgLoadingSpinner"
           gif:ImageBehavior.AnimatedSource="pack://application:,,,/Images/Loading_Spinner.gif"
           HorizontalAlignment="Right"
           Width="30"
           Height="30" />
</StackPanel>

I nest it in other views like this:我将它嵌套在其他视图中,如下所示:

<uc:LoadingIndicatorControl Loading="{Binding Loading}"
                            DockPanel.Dock="Bottom"/>

When I run this application it compiles without issues and the loading indicator is displayed as I expect.当我运行这个应用程序时,它编译没有问题,并且加载指示器按我的预期显示。

However at design time only I'm getting errors about this user control, related to the image resource.但是,只有在设计时,我才收到有关此用户控件的错误,这些错误与图像资源相关。 This gif is set to Build Action Resource .此 gif 设置为 Build Action Resource

In my host view I get this:在我的主机视图中,我得到了这个:

设计视图错误

And if I look at the detail of why, it says Cannot locate resource 'images/loading_spinner.gif' :如果我查看原因的详细信息,它会Cannot locate resource 'images/loading_spinner.gif'

找不到资源错误

If I look at LoadingIndicatorControl in design view it does in fact locate the loading_spinner.gif resource and gives no errors.如果我在设计视图中查看LoadingIndicatorControl ,它实际上确实找到了loading_spinner.gif资源并且没有给出错误。

What I have tried:我试过的:

  • Clean and rebuild numerous times多次清理和重建
  • Close VS2019 and reopen关闭 VS2019 并重新打开
  • Clean, close VS, reopen and rebuild清理,关闭 VS,重新打开并重建
  • Set Loading_Spinner.gif Build Action to Embedded Resource (was Resource which also works at runtime)Loading_Spinner.gif构建操作设置为Embedded Resource Resource也是在运行时工作的资源)
  • Tried platform targets Any CPU , x64 , x86尝试过的平台目标是Any CPU , x64 , x86

Interestingly, setting to build configuration to Release does prevent the error from appearing!有趣的是,将构建配置设置为Release确实可以防止出现错误! It only appears when Debug is used.它仅在使用Debug时出现。

I'm using Visual Studio Professional v16.11.16.我正在使用 Visual Studio Professional v16.11.16。

Any ideas how I can fix this?有什么想法可以解决这个问题吗?

Just use the direct path to the image:只需使用图像的直接路径:

<Image gif:ImageBehavior.AnimatedSource="Images/Loading_Spinner.gif"
       HorizontalAlignment="Center"
       VerticalAlignment="Center"
       Width="30"
       Height="30" />

If you really need the pack URI, the format is explained here , it look like this:如果你真的需要pack URI,格式在这里解释,它看起来像这样:

<Image gif:ImageBehavior.AnimatedSource="pack://application:,,,/SO_72648958;component/Images/Loading_Spinner.gif"
       HorizontalAlignment="Center"
       VerticalAlignment="Center"
       Width="30"
       Height="30" />

Working demo here .工作演示在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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