访问 IronPython 中的嵌入式资源

发布于 2024-07-14 14:39:23 字数 701 浏览 5 评论 0原文

我正在 IronPython Studio 中开发 Windows 窗体应用程序。 我想为我的项目选择一个图标,但这两个都失败了: 1- 表单属性窗口 -> 图标(选择 *.ico 文件) 发生编译时错误并且与 IronPython.targets 文件相关

“IronPythonCompilerTask”任务 出乎意料地失败了。 System.ArgumentNullException:值 不能为空。

2-我将 *.ico 文件添加到项目(项目 -> 添加 -> 现有项目),并在其属性中,将“构建操作”更改为“嵌入资源” 现在我无法使用 System.Reflection.Assembly 来访问此资源 我的代码:

self.Icon = Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream('IronPythonWinApp.myIcon.ico'))

在运行时会引发异常:

被调用的成员在动态程序集中受机器人支持

有谁知道向 IronPython WinForms 添加图标的更好(最好?)的方法吗?

谢谢

I'm developing a Windows Forms Application in IronPython Studio.
I want to choose an Icon for my project but both of these fail:
1- Form Properties window -> Icon (choose a *.ico file)
a compile-time error occurs and is related to IronPython.targets file

The "IronPythonCompilerTask" task
failed unexpectedly.
System.ArgumentNullException: Value
cannot be null.

2- I add a *.ico file to Project (Project -> Add -> Existing Item) and in its properties, change the 'Build Action' to 'Embedded Resource'
now I cannot use System.Reflection.Assembly to gain access to this resource
my code:

self.Icon = Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream('IronPythonWinApp.myIcon.ico'))

in runtime it throws an exception:

The invoked member is bot supported in a dynamic assembly

Does anyone know a better (best?) way to add an icon to an IronPython WinForms ?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2024-07-21 14:39:23

IronPython 是一种动态脚本语言; 它在运行时从脚本文件本身进行解释,而不是编译成程序集。 由于没有编译的程序集,因此您无法拥有嵌入式资源。 以下是在 IronPython 中向表单添加图标的两种方法:

首先,您可以将图标作为松散文件与 python 脚本一起包含在内。 然后,您可以通过将图标文件名传递给 System.Drawing.Icon 构造函数来创建图标对象。 下面是此场景的一个示例,其中主 python 脚本和图标部署在同一目录中。 该脚本使用此处找到的解决方案找到目录。

import clr
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')

import os
import __main__
from System.Drawing import Icon
from System.Windows.Forms import Form

scriptDirectory = os.path.dirname(__main__.__file__)
iconFilename = os.path.join(scriptDirectory, 'test.ico')
icon = Icon(iconFilename)

form = Form()
form.Icon = icon
form.ShowDialog()

或者,您可以加载一个图标,该图标作为嵌入资源包含在用 C# 编写的 .NET 程序集中。

import clr
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')

from System.Drawing import Icon
from System.Reflection import Assembly
from System.Windows.Forms import Form

assembly = Assembly.LoadFile('C:\\code\\IconAssembly.dll')
stream = assembly.GetManifestResourceStream('IconAssembly.Resources.test.ico')
icon = Icon(stream)

form = Form()
form.Icon = icon
form.ShowDialog()

IronPython is a dynamic scripting language; it is interpreted at runtime from the script files themselves, rather than being compiled into an assembly. Since there is no compiled assembly, you cannot have an embedded resource. Here are two ways of adding an icon to a form in IronPython:

First, you could include the icon as a loose file alongside the python scripts. You can then create the icon object by passing the icon filename to the System.Drawing.Icon constructor. Here is an example of this scenario, where the main python script and the icon are deployed in the same directory. The script uses the solution found here to find the directory.

import clr
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')

import os
import __main__
from System.Drawing import Icon
from System.Windows.Forms import Form

scriptDirectory = os.path.dirname(__main__.__file__)
iconFilename = os.path.join(scriptDirectory, 'test.ico')
icon = Icon(iconFilename)

form = Form()
form.Icon = icon
form.ShowDialog()

Alternatively, you can load an icon that is included as an embedded resource in a .NET assembly that is written in C#, for instance.

import clr
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')

from System.Drawing import Icon
from System.Reflection import Assembly
from System.Windows.Forms import Form

assembly = Assembly.LoadFile('C:\\code\\IconAssembly.dll')
stream = assembly.GetManifestResourceStream('IconAssembly.Resources.test.ico')
icon = Icon(stream)

form = Form()
form.Icon = icon
form.ShowDialog()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文