uli*_*ess 5 c++ windows winui-3
我正在将一种编程语言移植到 Windows,该语言具有“创建一个窗口”和“在该窗口中创建一个按钮”等命令。编程语言本身是用C++实现的。
我听说 Windows 上最新的、推荐的 UI API 是 WinUI 3,但我确实找不到任何关于如何在代码中定义 GUI 而不是从 XAML 文件加载 GUI 的好信息。
如何用代码创建 WinUI 3 GUI?
此示例是用 C# 编写的,但它也应该可以在 C++ 中运行。
执行以下步骤:
OnLaunched。请参阅下面的示例该示例代码创建一个Window类型的实例,其内容为 a StackPanel。包含StackPanelaTextBlock和 a Button。如果单击按钮,事件处理代码将使用 编写一些内容Debug.WriteLine。
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
// The original version of the method just contained these two lines:
//m_window = new MainWindow();
//m_window.Activate();
m_window = new Window();
StackPanel stackPanel = new StackPanel();
TextBlock textBlock = new TextBlock();
textBlock.Text = "Text of the TextBlock";
Button button = new Button();
button.Content = "Click Me";
button.Click += (object sender, RoutedEventArgs e) => { Debug.WriteLine("Button clicked"); };
stackPanel.Children.Add(textBlock);
stackPanel.Children.Add(button);
m_window.Content = stackPanel;
m_window.Activate();
}
Run Code Online (Sandbox Code Playgroud)