WinUI 3如何在按钮点击时创建新的应用程序窗口。

huangapple go评论112阅读模式
英文:

Winui 3 how to make a new app window at button click

问题

正在使用WinUI 3构建一个应用程序,使用XAML和C#。

在运行时如何创建应用程序的新实例?在其中一个页面上,我有一个按钮,内容为“打开新窗口”。在按钮点击时,我想从我正在构建的应用程序中打开一个新的自身/窗口。在WinUI 3示例应用程序中有一个示例,但对于使用最新版本的WinUI 3并从应用程序设计器创建应用程序的人来说,它不起作用。此外,在搜索Microsoft文档时,未能找到我想要的信息。

已经在互联网上搜索到以下示例,但均未成功:
https://learn.microsoft.com/en-us/windows/apps/design/layout/app-window
https://github.com/microsoft/WinUI-Gallery/blob/main/WinUIGallery/Navigation/NavigationRootPage.xaml.cs

我遇到了一次又一次的问题。

英文:

Am building an app with Winui 3 so XAML and C#

Ok, how to make a new instance of the app at runtime. So in one of the pages I have a button with content "Open new window". At button click I want to open a new self/ window from the app I am building. In the winui 3 sample app there is an example, but that doesn't work for people who uses the newest version of winui 3 and have the app created from the app designer.
Also when looking around for Microsoft documentation didn't get me where I want to be.

Have searched the internet found the following examples that didn't work:
https://learn.microsoft.com/en-us/windows/apps/design/layout/app-window
https://github.com/microsoft/WinUI-Gallery/blob/main/WinUIGallery/Navigation/NavigationRootPage.xaml.cs

What I got was breakdown after breakdown.

答案1

得分: 2

这有点复杂,但试试看。

  1. Assets 文件夹中创建一个带有唯一扩展名的虚拟文件,并确保将其 Build Action 属性设置为 Content。在这种情况下,我的虚拟文件是一个名为 "dummy.test" 的文本文件。

  2. 转到 Package.appxmanifestDeclarations 并添加一个 File Type Associations 声明,设置以下属性:

    • 显示名称:Test
    • 名称:test
    • 文件类型:.test
  3. 在按钮点击事件处理程序中,启动该文件。这将启动与 "test" 扩展名关联的应用程序,本例中是您的应用程序。

private async void LaunchAppButton_Click(object sender, RoutedEventArgs e)
{
    StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\dummy.test");
    _ = await Windows.System.Launcher.LaunchFileAsync(file);
}

更新

如果您想要做与 WinUI 3 Gallery 应用程序相同的事情,您可以从存储库中获取所有代码。

以下是其中的一部分:

因此,基本上您只需要创建一个 Window,设置您的主要内容并激活它。

Window newWindow = new();
newWindow.Content = /* 您的主要内容 */;
newWindow.Activate();
英文:

This is a bit tricky but give it a try.

  1. Create a dummy file with a unique extension in the Assets folder and make sure to set its Build Action property as Content. In this case, my dummy file is a text file named "dummy.test".

  2. Go to Package.appxmanifest, Declarations and add a File Type Associations declaration and set these properties:

    • Display name: Test
    • Name: test
    • File type: .test
  3. In your button click event handler, launch the file. This should launch the app associated with the "test" extension, which is your app in this case.

private async void LaunchAppButton_Click(object sender, RoutedEventArgs e)
{
    StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\dummy.test");
    _ = await Windows.System.Launcher.LaunchFileAsync(file);
}

UPDATE

If you want to do the same thing that the WinUI 3 Gallery app does, you can get all the code from the repo.

Here's some part of it:

So, basically you just need to create a Window, set your main content and activate it.

Window newWindow = new();
newWindow.Content = /* your main content*/
newWindow.Activate();

huangapple
  • 本文由 发表于 2023年7月27日 21:16:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定