问题描述
我需要通过cmake为 c-sharp/WPF指定资源文件,这些是需要随应用程序GUI一起提供的图像文件.
I need to specify resource files for c-sharp / WPF via cmake, these are image files that need to ship with the application GUI.
在Visual Studio中,您只需:选择图像->高级->构建操作->选择资源",这使得可以直接在xaml中访问图像,例如:
In Visual Studio you just:select the image -> Advanced -> Build Action -> select "Resource", this makes it possible to access the images directly in xaml, ex:
<Image Source="png/login.png"/>
供参考,当在VS2017中手动完成 时,会将以下行添加到project.csproj:
For reference when done manually in VS2017 this adds the following lines to the project.csproj:
<ItemGroup>
<Resource Include="png/login.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
那我如何在cmake中指定资源文件?
我在这里尝试了这个建议,但无济于事: https://gitlab.kitware.com/cmake/cmake/issues/17368
I tried the suggestion here, to no avail:https://gitlab.kitware.com/cmake/cmake/issues/17368
我当前的CMakeLists.txt如下:
My current CMakeLists.txt looks as follows:
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
)
推荐答案
我的项目遇到类似的问题.要解决此问题,我必须使用CMake在 add_executable
中显式添加图像资源文件.所以尝试这样的事情:
I had a similar issue with my project. To fix the issue, I had to explicitly add the image resource file in add_executable
using CMake. So try something like this:
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文件属性后,将生成操作"设置为资源".
This let CMake pull in the image file into my Visual Studio project. Then, upon examining the PNG file properties in Visual Studio, the "Build Action" was set to "Resource".
这篇关于如何在cmake中指定C#/WPF资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!