如何从 UWP 中的运行时组件调用主页函数?



我有一个UWP解决方案,其中包含一个UWP项目(主文件MainPage,index.html,这是上层(和一个窗口运行时组件(主文件Bridge.cs,这是较低级别(。

加载主页时,它将导航到包含按钮的index.html。当用户按下按钮时,它将调用函数showMessage(((在Bridge类中定义(。到目前为止,一切正常。

但是我想从较低级别的 Bridge 调用 MainPage 中定义的函数,例如 printMessage(.cs 代码,如何?

MainPage.xaml.cs:

public sealed partial class MainPage : Page
{
Bridge _bridge = new Bridge();
public MainPage()
{
this.InitializeComponent();
MyWebView.NavigationStarting += MyWebView_NavigationStarting;
this.Loaded += MainPage_Loaded;
}
private void _bridge_EReportFromBridge(string message)
{
Debug.WriteLine(message);
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Uri navigationUri = new Uri("ms-appx-web:///Assets/html/index.html", UriKind.RelativeOrAbsolute);
Debug.WriteLine("......................navigate the url");
MyWebView.Navigate(navigationUri);
}
private void MyWebView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
Debug.WriteLine(".................MyWebView_NavigationStarting() is executing");
MyWebView.AddWebAllowedObject("nativeObject", _bridge);
}
public void printMessage(string message)
{
Debug.WriteLine("message: {0}", message);
}
}

索引.html:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script>
function func1() {
// 首先判断我们对象是否正确插入
if (window.nativeObject) {
//调用的我们消息函数
window.nativeObject.showMessage("message, from index.html");
}
}
</script>
</head>
<body>
<div style="margin-top:100px">
<button id="fun1Btn" onclick="func1();">Call method 2</button>
</div>
</body>
</html>

桥.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation.Metadata;
using Windows.UI.Popups;
namespace BridgeObject
{
[AllowForWeb]
public sealed class Bridge
{
public void showMessage(string message)
{
new MessageDialog("Frome Bridge.cs: NativeMethod() is invoked!", message).ShowAsync();
// Call MainPage function printMessage(), how?
}
}
}

如果要从Windows运行时组件调用MainPage中定义的函数,则没有直接的方法可以实现。但是你可以先通知索引.html然后通知主页通过html调用函数。

桥.cs:

通过异步方法将消息传递到索引.html。

[AllowForWeb]
public sealed class Bridge​
{​
public IAsyncOperation<string> showMessage(string message)​
{​
new MessageDialog("Frome Bridge.cs: NativeMethod() is invoked!", message).ShowAsync();​
return Task.Run(() =>      {​
return message;​
}).AsAsyncOperation();​
}​
}

索引.html:

接收消息并通知主页。

function func1() { 
if (window.nativeObject) {​
var result = window.nativeObject.showMessage("message, from index.html");​
result.then(function (myMessage) {​
window.external.notify(myMessage);​ // send to MainPage
});​
}​
}

MainPage.xaml.cs:

首先订阅ScriptNotify事件,以便从 html 接收信息。触发时,您可以调用所需的函数。

public MainPage()
{​
this.InitializeComponent();​
MyWebView.NavigationStarting += MyWebView_NavigationStarting;​
MyWebView.ScriptNotify += MyWebView_ScriptNotify;​
this.Loaded += MainPage_Loaded;​
}
private void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
{​
printMessage(e.Value);​
}

最新更新