如何使 AddWebAllowedObject() 函数在 UWP 项目中工作?
How to make AddWebAllowedObject() function works in UWP project?
我想做一个在WebView中调用js中的native方法的demo,但是失败了
下面是 C# 代码:
namespace webviewDemo
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
[AllowForWeb]
public sealed class MyNativeClass
{
public void NativeMethod()
{
Debug.WriteLine(".................MyNativeClass::NativeMethod() is invoked!");
}
}
public MainPage()
{
this.InitializeComponent();
MyWebView.NavigationStarting += MyWebView_NavigationStarting;
Uri navigationUri = new Uri(@"http://10.119.116.160/test/tom/test.html");
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", new MyNativeClass());
}
}
}
下面是test.html源代码:
<html>
<head>
<script type='text/javascript'>
nativeObject.NativeMethod(); // Call the projected WinRT method.
</script>
</head>
</html>
应用程序启动时运行,输出日志如下:
......................navigate the url
.................MyWebView_NavigationStarting() is executing
Package.appxmanifest:
<uap:ApplicationContentUriRules>
<uap:Rule Type="include" Match="http://10.119.116.160/test/tom/test.html" WindowsRuntimeAccess="all"/>
</uap:ApplicationContentUriRules>
如您所见,没有打印 NativeMethod() 函数日志,意味着它根本没有被调用。
如何解决?
谢谢!
为此,"nativeObject" 需要成为一个 Windows 运行时组件。在您的代码示例中,它只是一个普通的 C# class.
实现 C# WinRT 组件的最简单方法是向类型为 "C# - Windows Runtime Component" 的解决方案添加一个新项目,并从您的应用项目中添加对此项目的引用。然后像往常一样在新项目中实现您的 C# 代码。
我想做一个在WebView中调用js中的native方法的demo,但是失败了
下面是 C# 代码:
namespace webviewDemo
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
[AllowForWeb]
public sealed class MyNativeClass
{
public void NativeMethod()
{
Debug.WriteLine(".................MyNativeClass::NativeMethod() is invoked!");
}
}
public MainPage()
{
this.InitializeComponent();
MyWebView.NavigationStarting += MyWebView_NavigationStarting;
Uri navigationUri = new Uri(@"http://10.119.116.160/test/tom/test.html");
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", new MyNativeClass());
}
}
}
下面是test.html源代码:
<html>
<head>
<script type='text/javascript'>
nativeObject.NativeMethod(); // Call the projected WinRT method.
</script>
</head>
</html>
应用程序启动时运行,输出日志如下:
......................navigate the url
.................MyWebView_NavigationStarting() is executing
Package.appxmanifest:
<uap:ApplicationContentUriRules>
<uap:Rule Type="include" Match="http://10.119.116.160/test/tom/test.html" WindowsRuntimeAccess="all"/>
</uap:ApplicationContentUriRules>
如您所见,没有打印 NativeMethod() 函数日志,意味着它根本没有被调用。 如何解决? 谢谢!
为此,"nativeObject" 需要成为一个 Windows 运行时组件。在您的代码示例中,它只是一个普通的 C# class.
实现 C# WinRT 组件的最简单方法是向类型为 "C# - Windows Runtime Component" 的解决方案添加一个新项目,并从您的应用项目中添加对此项目的引用。然后像往常一样在新项目中实现您的 C# 代码。