Pythonnet错误:XamlParseException:';未能创建';单击';从文本中
我以前在IronPython上工作(使用WPF开发一些GUI),最近我开始尝试pythonnet 但是我发现在IronPython上工作的xaml文件在CPython+pythonnet上不工作。在IronPython中,我可以定义一个按钮。在xaml文件中单击,但在CPython中似乎不可能。我试图寻找答案,但没有找到相关的答案。所以希望你能在这里救我 以下是我的主要剧本:Pythonnet错误:XamlParseException:';未能创建';单击';从文本中,python,.net,wpf,ironpython,python.net,Python,.net,Wpf,Ironpython,Python.net,我以前在IronPython上工作(使用WPF开发一些GUI),最近我开始尝试pythonnet 但是我发现在IronPython上工作的xaml文件在CPython+pythonnet上不工作。在IronPython中,我可以定义一个按钮。在xaml文件中单击,但在CPython中似乎不可能。我试图寻找答案,但没有找到相关的答案。所以希望你能在这里救我 以下是我的主要剧本: import clr clr.AddReference(r"wpf\PresentationFramework") fr
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>
非常感谢你的帮助 不能使用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>
是的,它与IronPython一起工作。我刚刚又进行了一次测试,看起来IronPython能够读取您的事件处理程序定义,而python.net却不能。如果在
\uuuu init\uuuu
工作之前单击定义按钮,您可以尝试一下吗?如果不是这样的话,那么很可能您必须在代码中附加事件处理程序,而不是在xaml中附加事件处理程序。我收到了同样的错误消息。谢谢你的回答。我尝试了一些与你的答案相似的方法,效果很好。谢谢。我还需要从系统导入LogicalTreeHelper和RoutedEventHandler。Windows导入应用程序,窗口,LogicalTreeHelper,RoutedEventHandler
<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>
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()