[UWP] Web Hosted UWP最小化后没有连接 [英] [UWP]Web Hosted UWP no connection after minimized

查看:56
本文介绍了[UWP] Web Hosted UWP最小化后没有连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前,我创建了一个UWP,它只连接到我的网站地址,其startPage设置为我的网站。


这个站点在几分钟内发送一个POST到一个地址,还有另一个套接字服务(Socket.IO)告诉你它是否连接到服务器。

当应用程序最小化时,它停止与服务器通信,因此代码停止工作。


我是否应该向appxmanifest添加一些特殊权限,以便在最小化时不会停止工作?

此应用程序在Microsoft Store中分发。


这是appxmanifest的一部分:


< Applications> 
< Application Id =" App"起始页= QUOT; HTTPS://app.example.com/">
< uap:ApplicationContentUriRules>
< uap:Rule Type =" include"匹配= QUOT; HTTPS://app.example.com/" WindowsRuntimeAccess = QUOT;所有" />
< / uap:ApplicationContentUriRules>
< / Application>
< / Applications>
<功能>
< Capability Name =" internetClient" />
< / Capabilities>




解决方案


>>当应用程序最小化时,它会停止与服务器通信,因此代码停止工作。


此行为是预期的。根据UWP应用程序的生命周期,UWP应用程序在用户最小化或切换到另一个应用程序后不久暂停。 


我建议您可以尝试使用扩展执行在您的应用暂停时推迟,以便在最小化时或在锁定屏幕下运行。请求扩展执行并不复杂。您需要创建一个  ExtendedExecutionSession  对象,
设置一些属性,然后调用  ExtendedExecutionSession.RequestExtensionAsync
方法
。它看起来像这样:

 var newSession = new ExtendedExecutionSession(); 
newSession.Reason = ExtendedExecutionReason.Unspecified;
newSession.Revoked + = SessionRevoked;
ExtendedExecutionResult result = await newSession.RequestExtensionAsync();

开关(结果)
{
案例ExtendedExecutionResult.Allowed:
DoLongRunningWork();
休息;

默认值:
case ExtendedExecutionResult.Denied:
DoShortRunningWork();
休息;
}

但是有一个提示:任何时候只有一个会话将被批准用于应用程序,导致对RequestExtensionAsync的额外调用导致会话被拒绝。


有关扩展执行的更多信息,请参阅此文档:推迟暂停执行的应用程序暂停


祝你好运,


Roy


Some years ago I created a UWP that connects only to the address of my website, whose startPage is set to my site.

This site in a few minutes sends a POST to an address and there is also another socket service (Socket.IO) that tells you if it is connected to the server.
When the application is minimized, it stops communicating with the server, so the code stops working.

Should I add some special permission to the appxmanifest so it does not stop working when minimized?
This application is distributed in the Microsoft Store.

This is a part of the appxmanifest:

<Applications>
	<Application Id="App" StartPage="https://app.example.com/">
		<uap:ApplicationContentUriRules>
			<uap:Rule Type="include" Match="https://app.example.com/" WindowsRuntimeAccess="all"/>
		</uap:ApplicationContentUriRules>
	</Application>
</Applications>
<Capabilities>
	<Capability Name="internetClient" />
</Capabilities>


解决方案

Hi,

>>When the application is minimized, it stops communicating with the server, so the code stops working.

This behavior is expected. According to the life cycle of UWP app, the UWP app is suspended shortly after the user minimizes it or switches to another app. 

I suggest that you could try to use extended execution to postpone when your app is suspended so that it can run while minimized or under the lock screen. It not complex to request a extended execution. You need to create an ExtendedExecutionSession object, set some properties and then call ExtendedExecutionSession.RequestExtensionAsync Method. It looks like this:

var newSession = new ExtendedExecutionSession();
newSession.Reason = ExtendedExecutionReason.Unspecified;
newSession.Revoked += SessionRevoked;
ExtendedExecutionResult result = await newSession.RequestExtensionAsync();

switch (result)
{
    case ExtendedExecutionResult.Allowed:
        DoLongRunningWork();
        break;

    default:
    case ExtendedExecutionResult.Denied:
        DoShortRunningWork();
        break;
}

But there is a tip:Only one session will be approved for an app at any time, causing additional calls to RequestExtensionAsync to result in the session being denied.

For more information about extended execution, please refer this document:Postpone app suspension with extended execution

Best regards,

Roy


这篇关于[UWP] Web Hosted UWP最小化后没有连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆