简体   繁体   中英

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. 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. 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. 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).

To use your own, define DISABLE_XAML_GENERATED_MAIN somewhere (note it works for C# too):

在此处输入图像描述

And add for example this to your appp.xaml.cpp file:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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