c# - 如何在 cmake 中指定 C#/WPF 资源文件
问题描述
我需要通过 cmake 为 c-sharp / WPF指定资源文件,这些是需要与应用程序 GUI 一起提供的图像文件。
在 Visual Studio 中,您只需:选择图像 -> 高级 -> 构建操作 -> 选择“资源”,这样就可以直接在 xaml 中访问图像,例如:
<Image Source="png/login.png"/>
作为在 VS2017 中手动完成时的参考,这会将以下几行添加到 project.csproj:
<ItemGroup>
<Resource Include="png/login.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
那么如何在cmake中指定资源文件呢?
我在这里尝试了这个建议,但无济于事: https ://gitlab.kitware.com/cmake/cmake/issues/17368
我当前的 CMakeLists.txt 如下所示:
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)
project(sampleApp VERSION 2.2.0 LANGUAGES CSharp)
include(CSharpUtilities)
add_executable(sampleApp app.config
App.xaml
App.xaml.cs
MainWindow.xaml
MainWindow.xaml.cs
Styles.xaml
Properties/AssemblyInfo.cs
Properties/Resources.Designer.cs
Properties/Resources.resx
Properties/Settings.Designer.cs
Properties/Settings.settings
)
target_link_libraries(sampleApp PRIVATE otherLibrary)
#
# Tried this setting (does not work)
set(RESOURCES "png/login.png")
set_property(SOURCE ${RESOURCES} PROPERTY VS_TOOL_OVERRIDE "Resource")
csharp_set_designer_cs_properties(
Properties/AssemblyInfo.cs
Properties/Resources.Designer.cs
Properties/Resources.resx
Properties/Settings.Designer.cs
Properties/Settings.settings)
csharp_set_xaml_cs_properties(
App.xaml
App.xaml.cs
MainWindow.xaml
MainWindow.xaml.cs
Styles.xaml)
target_compile_options(sampleApp PRIVATE "/langversion:6")
target_compile_options(sampleApp PRIVATE "/unsafe")
set_property(SOURCE App.xaml PROPERTY VS_XAML_TYPE "ApplicationDefinition")
set_property(TARGET sampleApp PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
set_target_properties(sampleApp PROPERTIES WIN32_EXECUTABLE TRUE)
set_property(TARGET sampleApp PROPERTY VS_DOTNET_REFERENCES
"System"
"System.Configuration"
"System.Configuration.Install"
"System.Data"
"System.Drawing"
"System.Management"
"System.Security"
"System.Transactions"
"System.Web"
"System.Xml"
"System.Core"
"System.Xaml"
"System.Xml.Linq"
"System.Data.DataSetExtensions"
"PresentationCore"
"PresentationFramework"
"Microsoft.CSharp"
"Microsoft.Office.Interop.Excel"
"WindowsBase"
)
#
# Install
#
install(TARGETS sampleApp EXPORT sampleAppTargets
RUNTIME DESTINATION sampleApp/
ARCHIVE DESTINATION sampleApp/
LIBRARY DESTINATION sampleApp/
)
install(
FILES
${RESOURCES}
DESTINATION
sampleApp/
COMPONENT
Devel
)
解决方案
我的项目也有类似的问题。要解决此问题,我必须在add_executable
使用 CMake 时显式添加图像资源文件。所以尝试这样的事情:
set(IMAGE_RESOURCE "png/login.png")
# Include the resource here with the other source files.
add_executable(sampleApp app.config
App.xaml
App.xaml.cs
MainWindow.xaml
MainWindow.xaml.cs
Styles.xaml
Properties/AssemblyInfo.cs
Properties/Resources.Designer.cs
Properties/Resources.resx
Properties/Settings.Designer.cs
Properties/Settings.settings
${IMAGE_RESOURCE}
)
# Then set the file property.
set_property(SOURCE ${IMAGE_RESOURCE} PROPERTY VS_TOOL_OVERRIDE "Resource")
这让 CMake 将图像文件拉入我的 Visual Studio 项目。然后,在 Visual Studio 中检查 PNG 文件属性时,“构建操作”设置为“资源”。
推荐阅读
- java - ArithmeticException 未被捕获
- python - 有没有办法在没有熊猫的情况下制作列?
- typescript - 使用 Typescript 访问对象的数组中的属性
- c - GCC 启动代码 _start 不以 main() 结尾
- amazon-web-services - aws iam - MFA 的角色策略
- javascript - 使用 jQuery 加载带有下拉列表的 HTML 文件
- angular - 如何使用 NGXS 在操作内设置布尔属性值
- javascript - JS 获取取消未将 CancellationToken 传递给 asp.net WebApi
- powershell - 如何在脚本块中传递变量以与 Invoke-Command 一起使用,该命令将调用将变量作为开关参数的可执行文件
- node.js - 有人可以帮我理解并解决这个“无法安装@parcel/transformer-image”吗?(JS)