James Otter
James Otter

Reputation: 133

Automatically open UWP app that launches on startup

I am working on a UWP app generated by Unity. Following the documentation on StartUpTasks here I have set the app to launch at startup, however when it does it launches minimized in the taskbar. I need the app to be open when it launches, but do not know what to call and where to make this happen.

This is the code generated by Unity:

namespace UnityToUWPApp
{
    class App : IFrameworkView, IFrameworkViewSource
    {
        private WinRTBridge.WinRTBridge m_Bridge;
        private AppCallbacks m_AppCallbacks;

        public App()
        {
            SetupOrientation();
            m_AppCallbacks = new AppCallbacks();
        }

        public virtual void Initialize(CoreApplicationView applicationView)
        {
            applicationView.Activated += ApplicationView_Activated;
            CoreApplication.Suspending += CoreApplication_Suspending;

            // Setup scripting bridge
            m_Bridge = new WinRTBridge.WinRTBridge();
            m_AppCallbacks.SetBridge(m_Bridge);

            m_AppCallbacks.SetCoreApplicationViewEvents(applicationView);
        }

        private void CoreApplication_Suspending(object sender, SuspendingEventArgs e)
        {
        }

        private void ApplicationView_Activated(CoreApplicationView sender, IActivatedEventArgs args)
        {
            CoreWindow.GetForCurrentThread().Activate();
        }

        public void SetWindow(CoreWindow coreWindow)
        {

            m_AppCallbacks.SetCoreWindowEvents(coreWindow);
            m_AppCallbacks.InitializeD3DWindow();
        }

        public void Load(string entryPoint)
        {
        }

        public void Run()
        {
            m_AppCallbacks.Run();
        }

        public void Uninitialize()
        {
        }

        [MTAThread]
        static void Main(string[] args)
        {
            var app = new App();
            CoreApplication.Run(app);
        }

        public IFrameworkView CreateView()
        {
            return this;
        }

        private void SetupOrientation()
        {
            Unity.UnityGenerated.SetupDisplay();
        }
    }
}

How can I ensure that when the app starts it is open and active?

Upvotes: 1

Views: 633

Answers (1)

Stefan Wick  MSFT
Stefan Wick MSFT

Reputation: 13850

UWP apps that are set as Startup task get launched as minimized/suspended by design. This is to ensure a good user experience for Store app by default and avoid having a ton of apps launch into the user's face on boot.

Classic Win32 apps as well as desktop bridge apps can continue to start in the foreground for backwards compat/consistency reasons.

To accomplish your goal, you can include a simple launcher Win32 exe in your package and set that as startup task. Then when it starts you just launch the UWP from there into the foreground. Note that doing this will require you to declare the "runFullTrust" capability, which will require an additional onboard review for the Microsoft Store.

To declare the Win32 exe as startup task, follow the "desktop bridge" section from this doc topic: https://learn.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.StartupTask

Upvotes: 4

Related Questions