App or Code to Suspend Resume UWP app without Visual Studio

我是研究僧i 提交于 2019-12-21 22:12:05

问题


My question is similar to How to make UWP app to suspend and resume state, but I need an application that I can give to my QA team so that they can more easily invoke Suspend and Resume in my app.

Since Visual Studio has a "LifeCycle Events" toolbar that lets you suspend and resume your App, I figure that there must be an app that ships with Visual Studio that does this. However, perusing through the Visual Studio files, I was not able to find such an executable.

Does anyone know of a stand-alone application (installed with Visual Studio or not) that can suspend or resume a windows store app?

If not, does anyone have sample code that can suspend or resume an arbitrary UWP app? I understand that there are some C++ libraries for building a debugger, but I'm not a C++ programmer. If there's a C# way to do this, please post some code. If it must be a C++ application, please post a complete example that is easy to build.


回答1:


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>



回答2:


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?



来源:https://stackoverflow.com/questions/53788142/app-or-code-to-suspend-resume-uwp-app-without-visual-studio

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