-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Description
I noticed in your example winui-component-connector.py
that the style looks similar to what Visual Studio generates.
How can I directly use x:Name
in XAML to name a control and then access it in code with self.MyName
?
Also, how can I handle XAML-declared events, for example Click="ButtonClick"
, by simply defining a method like def ButtonClick(self, sender, e)
in code?
I’d like to use this pattern in both Window
and Page
.
Here is a simplified sample:
App.py
from win32more.appsdk.xaml import XamlApplication,XamlType
from win32more.Windows.UI.Xaml.Interop import TypeKind
from typing import override
from MainWindow import MainWindow
class App(XamlApplication):
def OnLaunched(self, args):
self._window = MainWindow()
self._window.Activate()
if __name__ == "__main__":
XamlApplication.Start(App)
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MainWindow">
<TextBlock x:Name="tb" Loaded="tb_loaded"/>
</Window>
MainWindow.py
from pathlib import Path
from win32more.Microsoft.UI.Xaml import Window, Application
from win32more.Microsoft.UI.Xaml.Markup import IComponentConnector
from win32more.Windows.Foundation import Uri
from win32more.winrt import ComClass
class MainWindow(ComClass, Window, IComponentConnector):
def __init__(self):
super().__init__(own=True)
self.InitializeComponent()
def InitializeComponent(self):
xaml_path = Path(__file__).with_name("MainWindow.xaml").as_posix()
xaml_locator = Uri(f"ms-appx:///{xaml_path}")
Application.LoadComponent(self, xaml_locator)
def tb_loaded(self, sender, e):
self.tb.Text = "123"
Metadata
Metadata
Assignees
Labels
No labels