App or Code to Suspend Resume UWP app without Visual Studio

我的未来我决定 提交于 2019-12-04 15:45:32

UWP provides dedicated APIs for suspending and resuming apps: StartSuspendAsync StartResumeAsync

Here is for example how you can suspend the FeedbackHub app:

var diag = await AppDiagnosticInfo.RequestInfoForPackageAsync("Microsoft.WindowsFeedbackHub_8wekyb3d8bbwe");
if (diag.Count > 0)
{
    var resourceGroups = diag[0].GetResourceGroups();
    if (resourceGroups.Count > 0)
    {
        await resourceGroups[0].StartSuspendAsync();
    }
}

Note that you will need to declare the 'appDiagnostics' capability to call these APIs:

<Package
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp rescap">
  ...

  <Capabilities>
    <rescap:Capability Name="appDiagnostics" />
  </Capabilities>
</Package>

One possibility would be for the tester to simply minimize and maximize the application. This would trigger a suspension and resumption.

To check whether the application has really been suspended and resumed, you can use logging, e.g. MetroLog or any other logging solution.

For a quick test one could do this:

MetroLog

In the Package Manager Console enter:

Install-Package MetroLog 

Code

In the App constructor add something like:

LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new StreamingFileTarget());
log = LogManagerFactory.DefaultLogManager.GetLogger<App>();

this.Suspending += OnSuspending;
this.Resuming += OnResuming;

Then there are these two methods:

private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    log.Trace("OnSuspending called");
    deferral.Complete();
}

private void OnResuming(object sender, object e)
{
    log.Trace("OnResuming called");
}

Test

  • deploy App
  • quit VS
  • call the application from Windows menu
  • minimize and maximize the appliction

In the folder ApplicationData.Current.LocalFolder you will find a new folder MetroLogs with a file named similar to Log - 20181216.log.

Open it in a text editor:

As you can see the application was suspended and resumed.

Is that what you're looking for?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!