提问人:WSC 提问时间:6/17/2022 更新时间:6/22/2022 访问量:150
无法在设计视图中创建 UserControl 的实例,在发布模式和运行时工作
Cannot create an instance of UserControl in design view, works in Release mode and at runtime
问:
我有一个用户控件,当加载发生时,该控件显示在各种其他控件中。LoadingIndicatorControl
此用户控件非常简单,如下所示:
<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>
我把它嵌套在其他视图中,如下所示:
<uc:LoadingIndicatorControl Loading="{Binding Loading}"
DockPanel.Dock="Bottom"/>
当我运行此应用程序时,它编译时没有问题,并且加载指示器按我的预期显示。
但是,仅在设计时,我收到有关此用户控件的错误,与图像资源有关。此 gif 设置为 Build Action 。Resource
在我的主机视图中,我得到这个:
如果我看一下原因的细节,它会说:Cannot locate resource 'images/loading_spinner.gif'
如果我在设计视图中查看,它实际上确实找到了资源并且没有错误。LoadingIndicatorControl
loading_spinner.gif
我试过什么:
- 多次清洁和重建
- 关闭 VS2019 并重新打开
- 清理、关闭 VS、重新打开并重建
- 将“生成操作”设置为“(在运行时也有效)
Loading_Spinner.gif
Embedded Resource
Resource
- 尝试的平台目标 , ,
Any CPU
x64
x86
有趣的是,将构建配置设置为确实可以防止错误出现!它仅在使用时出现。Release
Debug
我正在使用 Visual Studio Professional v16.11.16。
我有什么想法可以解决这个问题吗?
答:
0赞
Orace
6/22/2022
#1
只需使用图像的直接路径:
<Image gif:ImageBehavior.AnimatedSource="Images/Loading_Spinner.gif"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="30"
Height="30" />
如果你真的需要 URI,格式解释在这里,它看起来像这样:pack
<Image gif:ImageBehavior.AnimatedSource="pack://application:,,,/SO_72648958;component/Images/Loading_Spinner.gif"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="30"
Height="30" />
评论
0赞
WSC
6/27/2022
直接路径对我不起作用。添加到包 URI 中会导致异常: 。component
NotSupportedException: The URI prefix is not recognized.
0赞
Orace
6/28/2022
这不是正常的行为,你破坏了一些东西。也许是 AssemblyInfo.cs 文件?
0赞
WSC
6/28/2022
Assembly.cs
看起来不错...它比你的示例多了几个位,但这一切都只是设置的东西。Properties -> Application -> Assembly Information
评论