You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinUI3 C++/WinRT应用关闭时FirstPage析构函数未调用问题

WinUI3 C++/WinRT 应用:关闭窗口后FirstPage析构函数未调用

在Release模式下编译以下代码并运行,点击窗口的"X"按钮关闭应用后,发现FirstPage的析构函数并未被调用。

代码示例

1. App.xaml.cpp

#include "pch.h"
#include "App.xaml.h"
#include "MainWindow.xaml.h"

using namespace winrt;
using namespace Windows::Foundation;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Navigation;
using namespace AppTest;
using namespace AppTest::implementation;

App::App()
{
    InitializeComponent();
}
void App::OnLaunched(LaunchActivatedEventArgs const&)
{
    window = make<MainWindow>();
    window.Activate();
}

2. MainWindow.xaml

<Window
    x:Class="AppTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <RelativePanel>
        <Frame Name="frameContent"/>
    </RelativePanel>
    
</Window>

3. MainWindow.xaml.cpp

#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif

#include "FirstPage.xaml.h"

using namespace winrt;
using namespace Microsoft::UI::Xaml;

namespace winrt::AppTest::implementation
{
    MainWindow::MainWindow()
    {
        InitializeComponent();
        this->frameContent().Navigate(winrt::xaml_typename<AppTest::FirstPage>());
    }
}

4. FirstPage.xaml.cpp

#include "pch.h"
#include "FirstPage.xaml.h"
#if __has_include("FirstPage.g.cpp")
#include "FirstPage.g.cpp"
#endif

using namespace winrt;
using namespace Microsoft::UI::Xaml;

namespace winrt::AppTest::implementation
{
    FirstPage::FirstPage()
    {
        InitializeComponent();
    }
    FirstPage::~FirstPage()
    {
        /* 用于验证析构函数是否被调用的逻辑 */
    }
}

操作步骤

  • 运行应用
  • 点击窗口右上角的"X"按钮关闭应用

预期与实际结果

  • 预期行为:FirstPage的析构函数应被调用
  • 实际结果:FirstPage的析构函数未被调用

内容的提问来源于stack exchange,提问作者JoJo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 04:42:57