如何在 CMake 中指定 C# / WPF 资源文件

3
我需要通过cmake为指定资源文件,这些是需要随应用程序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
)
3个回答

2

我在我的项目中遇到了类似的问题。为了解决这个问题,我不得不在使用CMake的add_executable时明确添加图像资源文件。因此,请尝试像这样做:

最初的回答:

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文件属性时,“Build Action”设置为“Resource”。

最初的回答

1
我也无法让资源嵌入程序集中。我找到了缺失的部分。
你的 CMakeLists.txt 文件需要:
set_property(TARGET 你的目标名称 PROPERTY VS_GLOBAL_RootNamespace 你的根命名空间)
显然,将 yourTargetNameyourRootNamespace 替换为自己的值。

1
使用SET_PROPERTY覆盖构建操作对我来说并没有完全起作用。构建操作仍然是Image。然而,您链接的ticket使用了SET_SOURCE_FILES_PROPERTIES,这为我解决了问题:
SET(IMAGE_RESOURCES
    "Icons/Foo.png"
    "Icons/Bar.png"
)

ADD_EXECUTABLE(MyApp
    App.config
    Application.cs

    ${IMAGE_RESOURCES}
)

SET_SOURCE_FILES_PROPERTIES(${IMAGE_RESOURCES} PROPERTIES VS_TOOL_OVERRIDE "Resource")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接