0

I'm trying to use python.net and WPF to have a modern look & feel on my app.
I also want to use the FluentWPF library to give it the MS Fluent style.

I built the FluentWPF project and loaded the dll in python.net and built a simple window XAML to load :

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

The 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>

This is what I have so far, but when I run this code I get the error :

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

I tried to fix it by adding these lignes to the Window ctor thinking it would replace the missing App.xaml used in the library samples :

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

Also in the App.xml there is this line :

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

Is this something I can also inject through the ParserContext ? Or I am missing somethig ? I'm not really familiar with WPF yet.

G.Vernier
  • 369
  • 2
  • 14
  • X:class is the code behind class. Which is mywindow, assuming python follows a similar pattern to c#. Personally, my experiences with python as a development language for wpf was not a positive one. – Andy Aug 29 '20 at 15:41
  • I just tried to use MyWindow instead, but still have the error, which points to line 1 char 2, so the x:class is not even reached yet :( I know python is not going to be the best experience with WPF, but I'm curious to see if I could move a big PyQT project to WPF :) – G.Vernier Aug 29 '20 at 16:50
  • I would start with a working solution and window. Use the ironpython template. Then start adding fiddly bits. https://www.google.com/amp/s/www.telerik.com/amp/python-desktop-application-development-with-telerik-ui-for-wpf/WEx1ZE1sRUVUWkE5S0dNbEhBNXJyUkU3T1Q4PQ2 – Andy Aug 29 '20 at 17:48

1 Answers1

1
clr.AddReference("TestWpfProjectForPython")
        
from TestWpfProjectForPython import MainWindow


def app_thread():
    sb = MainWindow()
    sb.ShowDialog()

from System.Threading import ApartmentState, Thread, ThreadStart

thread = Thread(ThreadStart(app_thread))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()

It is important to open the WPF window as a dialog, because otherwise the starting thread ends instantly.