简体   繁体   English

如何覆盖启动 WinUI 3.0 桌面应用程序

[英]How to Override Start WinUI 3.0 Desktop Application

I'm interested in overriding the entry point in my WinUI 3.0 Desktop App to control the message pump.我有兴趣覆盖我的 WinUI 3.0 桌面应用程序中的入口点来控制消息泵。 It doesn't appear to be as simple as redefining the static function看起来不像重新定义静态函数那么简单

static auto Start(winrt::Microsoft::UI::Xaml::ApplicationInitializationCallback const& callback);

in the applications child class.在应用程序子类中。 I'm a bit confused even, if the entry point isn't still wWinMain, because if it is, it isn't defined in the solution.即使入口点仍然不是 wWinMain,我也有点困惑,因为如果是,则它没有在解决方案中定义。 I haven't tried setting the Linker option to some other entry point, because I saw mentioned that doing so skips static member pre-processing, and I figured I'd find out what that meant before I started messing around.我没有尝试将 Linker 选项设置为其他入口点,因为我看到提到这样做会跳过静态成员预处理,我想我会在开始搞砸之前找出这意味着什么。 So how do I capture the entry point?那么如何捕获入口点呢?

This has probably been generated by the tooling you use.这可能是由您使用的工具生成的。 If you don't use any special tooling or/and have chosen a fancy compiler, you should be able to do it yourself.如果您不使用任何特殊工具或/并且选择了精美的编译器,您应该可以自己完成。

If you choose Microsoft tooling, typically C++/WinRT with C++ and Visual Studio, then WinMain is generated probably in a file named App.xaml.g.hpp (to find that, just start debug, it should get you right in that WinMain).如果您选择 Microsoft 工具,通常是带有 C++ 和 Visual Studio 的C++/WinRT ,则可能会在名为App.xaml.g.hpp的文件中生成 WinMain(要找到它,只需开始调试,它应该可以让您在该 WinMain 中正确) .

To use your own, define DISABLE_XAML_GENERATED_MAIN somewhere (note it works for C# too):要使用您自己的,请在某处定义DISABLE_XAML_GENERATED_MAIN (注意它也适用于 C#):

在此处输入图像描述

And add for example this to your appp.xaml.cpp file:例如,将此添加到您的 appp.xaml.cpp 文件中:

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd)
{
  winrt::init_apartment(winrt::apartment_type::single_threaded);

  // put your fancy code somewhere here
  ::winrt::Microsoft::UI::Xaml::Application::Start(
    [](auto&&)
    {
      // and here (default is like this)
      // ::winrt::make<::winrt::MyNamespace::MyApp::implementation::App>();
    });

  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM