【问题标题】:Accessing embedded resources in IronPython在 IronPython 中访问嵌入式资源
【发布时间】:2010-10-10 01:16:43
【问题描述】:

我正在 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 的更好(最好的?)方法?

谢谢

【问题讨论】:

    标签: ironpython icons ironpython-studio


    【解决方案1】:

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

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

    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()
    

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 1970-01-01
      • 2013-12-29
      • 2011-07-01
      • 2012-11-03
      • 2011-10-30
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      相关资源
      最近更新 更多