python.net使用自定义控件构造WPF窗口

问题描述

我正在尝试使用python.net和WPF在我的应用程序上具有现代外观。
我还想使用FluentWPF库来为其赋予MS Fluent风格。

我构建了FluentWPF项目,并在python.net中加载了dll,并构建了一个简单的XAML窗口来加载:

import os
import clr

# add WPF ref,load FluentWPF dll
clr.AddReference("wpf\PresentationFramework")
dll = os.path.join(os.getcwd(),r"FluentWPF-master\FluentWPF\bin\Debug\net45\FluentWPF.dll")
clr.AddReference(dll)

from SourceChord.FluentWPF import *

from System import Exception
from System.IO import *
from System.Windows.Markup import XamlReader,ParserContext
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("window.xaml")
            ctx = ParserContext()
            ctx.XmlnsDictionary.Add("fw","clr-namespace:SourceChord.FluentWPF;assembly=FluentWPF")
            self.window = XamlReader.Load(stream.BaseStream,ctx)
            Application().Run(self.window)
        except Exception as e:
            print(e.Message)

# start app
thread = Thread(ThreadStart(MyWindow))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()

Window XAML:

<fw:Window x:Class="Window"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:fw="clr-namespace:SourceChord.FluentWPF;assembly=FluentWPF"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            Title="AcrylicWindow"
            Width="300"
            Height="300"
            mc:Ignorable="d">
    <Grid Background="#70FFFFFF">
        <TextBlock Margin="10"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Top"
                   Text="This is AcrylicWindow"
                   TextWrapping="Wrap" />
    </Grid>
</fw:Window>

到目前为止,这是我所拥有的,但是当我运行此代码时,出现错误:

'Cannot create unknown type '{clr-namespace:SourceChord.FluentWPF;assembly=FluentWPF}Window'.' Line number '1' and line position '2'.

我试图通过将这些木质素添加到Window ctor来修复它,以为它将替换库样本中使用的缺少的App.xaml:

ctx = ParserContext()
ctx.XmlnsDictionary.Add("fw","clr-namespace:SourceChord.FluentWPF;assembly=FluentWPF")

在App.xml中也有这行:

 <ResourceDictionary Source="pack://application:,/FluentWPF;component/Styles/Controls.xaml" />

我还可以通过ParserContext注入吗? 还是我想念某事?我对WPF还不是很熟悉。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)