简体   繁体   中英

How to shutdown/restart my UWP-Application?

I'm working on a programm for RaspberryPi with Windows IoT core 17763. My IDE is Visual Studio 2019. I use C# and UWP for my programm.

Restart and shutdown doesn't work. For these functions the app has 3 buttons.

  • Button Exit: termination of the app and start the IoT core app.
  • Button Restart: full reboot of the system.
  • Button Shutdown: full shutdown the system.

How does my app behave?

  1. while debugging: All 3 buttons just terminate the app and the debugging ends.
  2. when app is in autostart: All 3 Buttons perform a restart of the app. No shutdown, no reboot of the system, no IoT core app.

Here the relevant code of my application

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System;
using Windows.System;

public sealed partial class MainPage : Page
{

    public MainPage()
    {
        InitializeComponent();
    }

    //button to exit application
    private void FlyoutExit_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Exit();
    }

    //button to restart system
    private void FlyoutReboot_Click(object sender, RoutedEventArgs e)
    {
        ShutdownManager.BeginShutdown(ShutdownKind.Restart, TimeSpan.FromSeconds(0));
    }

    //button to shutdown system
    private void FlyoutShutdown_Click(object sender, RoutedEventArgs e)
    {
        ShutdownManager.BeginShutdown(ShutdownKind.Shutdown, TimeSpan.FromSeconds(0));
    }

}

Here is the Package.appxmanifest

<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
IgnorableNamespaces="uap mp iot">
<Capabilities>
<iot:Capability Name="systemManagement"/>
<Capability Name="internetClient" />
<iot:Capability Name="lowLevelDevices"/>
<DeviceCapability Name="serialcommunication">
  <Device Id="any">
    <Function Type="name:serialPort" />
  </Device>
</DeviceCapability>
</Capabilities>
</Package>
  1. When the application exits in debugging mode, visual studio will dis-connect the remote debugging. The behavior is correct. After the application exits, Windows IoT Core will start the default app which is configured as start-up.

  2. ShutdownManager requires the use of the IoT systemManagement capability. You need to add the following to Package.appmanifest, otherwise, the app will exit with an exception.

    <iot:Capability Name="systemManagement"/>

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