【发布时间】:2017-11-20 23:54:23
【问题描述】:
我正在通过边缘扩展在我的 UWP 应用中提供一项新功能。 我用以下 manifest.json 编写了一个边缘扩展
{
"name": "MyExtensionName",
"version": "1.0.0.0",
"description": "MyExtension description",
"author": "Me",
"icons": {
"16": "icons/logo_16.png",
"48": "icons/logo_48.png",
"128": "icons/logo_128.png"
},
"browser_action": {
"default_icon": {
"20": "icons/logo_20.png",
"40": "icons/logo_40.png"
},
"default_title": "MyTitle"
},
"permissions": [
"contextMenus",
"tabs",
"storage",
"activeTab",
"<all_urls>",
"nativeMessaging",
"background"
],
"minimum_edge_version": "37.14316.1000.0",
"background": {
"scripts": ["js/background.js"],
"persistent": true
},
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"css": ["css/light.css"],
"js": ["js/content.js"],
"run_at": "document_end"
}]
}
我可以用这个建立连接
var port = browser.runtime.connectNative("MyAppServiceName");
我已尝试与此进行交流
port.postMessage({Text: "Hello"});
我没有收到任何错误,但我的 UWP 应用中的后台任务没有收到任何响应。我不明白为什么。
在我的应用程序的 package.appmanifest 中,我添加了以下代码:
<uap:VisualElements DisplayName="MyUWPAppName"
AppListEntry="none"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png"
Description="MyUWPApp description"
BackgroundColor="transparent">
在这
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="msghost1" />
</uap:Extension>
<uap:Extension Category="windows.appService" EntryPoint="MyAppService.Class1">
<uap:AppService Name="MyAppServiceName" />
</uap:Extension>
<uap3:Extension Category="windows.appExtension">
<uap3:AppExtension Name="com.test.edge.extension"
Id="EdgeExtension"
PublicFolder="Extension"
DisplayName="ms-resource:DisplayName">
<uap3:Properties>
<Capabilities>
<Capability Name="websiteContent" />
<Capability Name="websiteInfo" />
<Capability Name="browserStorage" />
</Capabilities>
</uap3:Properties>
</uap3:AppExtension>
</uap3:Extension>
AppService 是在同一解决方案中作为项目添加的运行时组件。 它的代码很简单
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Collections;
namespace MyAppService
{
public sealed class Class1: IBackgroundTask
{
private BackgroundTaskDeferral backgroundTaskDeferral;
private AppServiceConnection appServiceconnection;
public void Run(IBackgroundTaskInstance taskInstance)
{
this.backgroundTaskDeferral = taskInstance.GetDeferral(); // Get a deferral so that the service isn't terminated.
taskInstance.Canceled += OnTaskCanceled; // Associate a cancellation handler with the background task.
// Retrieve the app service connection and set up a listener for incoming app service requests.
var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
appServiceconnection = details.AppServiceConnection;
appServiceconnection.RequestReceived += OnRequestReceived;
}
private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
// This function is called when the app service receives a request
}
private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
if (this.backgroundTaskDeferral != null)
{
// Complete the service deferral.
this.backgroundTaskDeferral.Complete();
}
}
}
}
我尝试使用“不启动,但在启动时调试我的代码”选项来调试 AppService,但我注意到我没有得到任何响应(后台任务未被调用)。
有人知道哪里出了问题(或缺少什么)?
【问题讨论】:
-
你测试过官方样品吗?它可以很好地工作吗?我想先确认您是否以正确的方式进行测试。
-
我在这里github.com/MicrosoftEdge/MicrosoftEdge-Extensions-Demos/tree/… 尝试了示例 SecureInput,但它不起作用。
标签: c# uwp microsoft-edge