[英]WinUI 3 1.0 : Window Ready Event
I am trying to execute code when the window is ready.我正在尝试在 window 准备就绪时执行代码。
Specifically, I am trying to make the window maximize.具体来说,我正在尝试使 window 最大化。 The issue is I cannot run this code synchronously after
InitializeComponent
because it is too soon (the window is not yet ready/ loaded/ shown, so setting the Presenter
has no effect)问题是我无法在
InitializeComponent
之后同步运行此代码,因为它太早了(window 尚未准备/加载/显示,因此设置Presenter
无效)
The issue is the Window.Loaded
event doesn't seem to be exposed by WinUI 3 (version 1.0).问题是
Window.Loaded
事件似乎没有被 WinUI 3(版本 1.0)公开。
So here is my current hack.所以这是我目前的技巧。 In the constructor (after
InitializeComponent
), I hook into one of the only events exposed by WinUI 3, Activated
在构造函数中(在
InitializeComponent
之后),我挂接到 WinUI 3 公开的唯一事件之一, Activated
Activated += MainWindow_Activated_FirstTime;
The issue is, this fires any time the window is focused, pretty much.问题是,只要 window 聚焦,它就会触发,几乎。 So the workaround is I just remove the event handler, inside the event handler
所以解决方法是我只删除事件处理程序内部的事件处理程序
private void MainWindow_Activated_FirstTime(object sender, WindowActivatedEventArgs args)
{
SetupWindow();
Activated -= MainWindow_Activated_FirstTime;
}
( SetupWindow
sets the window presenter) (
SetupWindow
设置 window 演示者)
Now, like I said, this works , but I think it's pretty hacky.现在,就像我说的,这行得通,但我认为它很老套。 I was wondering if there was a proper way to hook into the
Loaded
or maybe some equivalent event like Ready
or Rendered
.我想知道是否有一种正确的方法可以连接到
Loaded
或某些等效事件,例如Ready
或Rendered
。 I really don't care what I hook into as long as it's better than this.只要比这更好,我真的不在乎我喜欢什么。
If you have set the Content
property of the window to a FrameworkElement
, such as for example a Panel
of any kind, you could handle the Loaded
event of this one.如果您已将 window 的
Content
属性设置为FrameworkElement
,例如任何类型的Panel
,您可以处理此面板的Loaded
事件。
You could also use the DispatcherQueue
to enqueue some work with low priority to be done as soon as the dispatcher thread is ready:您还可以使用
DispatcherQueue
将一些低优先级的工作排入队列,以便在调度程序线程准备好后立即完成:
public MainWindow()
{
this.InitializeComponent();
Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread().TryEnqueue(
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low,
new Microsoft.UI.Dispatching.DispatcherQueueHandler(() =>
{
//...
}));
}
As of Win App Sdk 1.1.3, the event is now exposed截至Win App Sdk 1.1.3,事件现已曝光
<Window
x:Class="WindowsAppSdkTouchUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppExample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d"
Closed="Window_Closed"
Activated="Window_Activated">
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.