首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在cmake中指定C# / WPF资源文件

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

Stack Overflow用户
提问于 2019-02-13 22:26:51
回答 3查看 1.2K关注 0票数 2

我需要通过cmake为c-sharp / WPF指定资源文件,这些是需要与应用程序图形用户界面一起提供的图像文件。

在Visual Studio中,您只需:选择图像xaml高级->构建操作->选择“资源”,这使得在->中直接访问图像成为可能,例如:

<Image Source="png/login.png"/>

在VS2017中手动执行操作时,这会将以下行添加到project.csproj中以供参考:

<ItemGroup>
    <Resource Include="png/login.png">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Resource>
  </ItemGroup>

如何在中指定资源文件?

我在这里尝试了这个建议,但没有效果: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
)
EN

回答 3

Stack Overflow用户

发布于 2019-06-06 07:44:22

我的项目也遇到了类似的问题。为了解决这个问题,我必须使用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”。

票数 2
EN

Stack Overflow用户

发布于 2020-01-08 22:13:02

使用SET_PROPERTY覆盖构建操作对我来说并不是很有效。build操作仍然是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")
票数 1
EN

Stack Overflow用户

发布于 2021-09-01 14:03:28

我也无法获得资源来将自己嵌入到程序集中。我刚刚找到了遗失的东西。

您的CMakeLists.txt文件需要

set_property(TARGET yourTargetName PROPERTY VS_GLOBAL_RootNamespace yourRootNamespace)

显然,用您自己的值替换yourTargetNameyourRootNamespace

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54672541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档