Pythonnet错误:XamlParseException:'无法从文本创建'点击'

时间:2018-09-24 14:20:33

标签: python .net wpf ironpython python.net

以前,我正在研究IronPython(使用WPF开发一些GUI),最近我开始尝试pythonnet。

但是我发现在IronPython上运行的xaml文件在CPython + pythonnet上不起作用。在IronPython中,我可以定义一个Button.Click在xaml文件中,但是在CPython中似乎是不可能的。我试图查找答案,但未找到任何相关信息。所以希望你能在这里救我...

这是我的主要脚本:

import clr
clr.AddReference(r"wpf\PresentationFramework")
from System.IO import StreamReader
from System.Windows.Markup import XamlReader
from System.Windows import Application, Window
from System.Threading import Thread, ThreadStart, ApartmentState

class MyWindow(Window):
    def __init__(self):
        stream = StreamReader('test.xaml')
        window = XamlReader.Load(stream.BaseStream)
        Application().Run(window)

    def Button_Click(self, sender, e):
        print('Button has clicked')

if __name__ == '__main__':
    thread = Thread(ThreadStart(MyWindow))
    thread.SetApartmentState(ApartmentState.STA)
    thread.Start()
    thread.Join()

这是test.xmal:

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="WpfApplication1" Height="300" Width="300"> 
    <Grid>
        <Button x:Name="BUTTON" Content="Button" HorizontalAlignment="Left" Margin="101,82,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" Background="#FFFF1616"/>
    </Grid>
</Window> 

我收到的错误消息是:

未处理的异常:Python.Runtime.PythonException:XamlParseException:'无法从文本'Button_Click'创建'Click'。行号“ 6”和行位置“ 132”。

如果我在IronPython中加载相同的xaml并保持相同的Class结构,那么该脚本可以正常工作:

import wpf
from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        self.ui = wpf.LoadComponent(self, 'test.xaml')

    def Button_Click(self, sender, e):
        print('Button has clicked')

if __name__ == '__main__':
    Application().Run(MyWindow())

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您无法使用pythonnet软件包在python中导入wpf。但是您可以使用Presentationframework的PresentationFramework加载XAML文件,但有一些限制。 XamlReader无法执行事件处理,因此我们可以在python文件而不是XAML文件中手动为该按钮添加事件。

听到gui.xaml代码

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="WpfApplication1" Height="300" Width="300">
    <Grid>
        <Label Content="Hello world" HorizontalAlignment="Left" Height="32" Margin="65,77,0,0" VerticalAlignment="Top" Width="119"/>
        <Button Name="button1" Content="Button" HorizontalAlignment="Left" Margin="65,145,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window> 

然后听到python代码

import clr
clr.AddReference("wpf\PresentationFramework") 
from System.IO import *
from System.Windows.Markup import XamlReader
from System.Windows import *
from System.Threading import Thread, ThreadStart, ApartmentState
from System.Windows.Controls import *

class MyWindow(Window):
    def __init__(self):
        try:
            stream = StreamReader('gui.xaml')
            self.window = XamlReader.Load(stream.BaseStream)
            ButtoninXAML = LogicalTreeHelper.FindLogicalNode(self.window, "button1") 
            ButtoninXAML.Click +=  RoutedEventHandler(self.Button_Click)
            Application().Run(self.window)      
        except Exception as ex:
            print(ex)
    def Button_Click(self, sender, e):
        print('clicked')


if __name__ == '__main__':
    thread = Thread(ThreadStart(MyWindow))
    thread.SetApartmentState(ApartmentState.STA)
    thread.Start()
    thread.Join()