如何在不使用 Uno.UI 的情况下从 Xamarin.Forms 应用程序调用 JavaScript?
How to Invoke JavaScript from Xamarin.Forms app without using Uno.UI?
我有一个 Xamarin.Forms 应用程序,它通过 Uno Platform 投射到 WASM。单击或点击后,我需要打开一个网页 link。我解决它:
try // Uwp & iOS & Android
{
await Browser.OpenAsync(new Uri("http://localhost"), BrowserLaunchMode.SystemPreferred); // Open url in-app browser for iOS & Android- native in UWP
}
catch (NotImplementedInReferenceAssemblyException ex) //Wasm falls here because lack of Xamarin.Essentials.
{
await DisplayAlert("Hata", "Not supported on WASM", "Anladım"); // Show the info about exception.
}
对于 Xamarin。 Android & iOS 和 UWP(Windows)。但是,Xamarin.Essentials 还不能在 WASM 上 运行。所以我需要调用 Javascript 和 运行 这个代码:
function openPage(x) {
window.open(x);
}
我尝试将 UNO.Foundation 用于
WebAssemblyRuntime.InvokeJS("(function(){location.href=\"https://www.wikipedia.com/\";})();");
但是,它干扰了 UWP UI classes 并破坏了我的项目。如何在不使用 UNO.Foundation class 的情况下从 Xamarin.Forms 页 C# 后端调用 Javascript ?谢谢。
您可以使用混合网络视图,然后注入 JavaScript 代码来检测对此网络视图的每次点击
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview
我有一个 Xamarin.Forms 应用程序,它通过 Uno Platform 投射到 WASM。单击或点击后,我需要打开一个网页 link。我解决它:
try // Uwp & iOS & Android
{
await Browser.OpenAsync(new Uri("http://localhost"), BrowserLaunchMode.SystemPreferred); // Open url in-app browser for iOS & Android- native in UWP
}
catch (NotImplementedInReferenceAssemblyException ex) //Wasm falls here because lack of Xamarin.Essentials.
{
await DisplayAlert("Hata", "Not supported on WASM", "Anladım"); // Show the info about exception.
}
对于 Xamarin。 Android & iOS 和 UWP(Windows)。但是,Xamarin.Essentials 还不能在 WASM 上 运行。所以我需要调用 Javascript 和 运行 这个代码:
function openPage(x) {
window.open(x);
}
我尝试将 UNO.Foundation 用于
WebAssemblyRuntime.InvokeJS("(function(){location.href=\"https://www.wikipedia.com/\";})();");
但是,它干扰了 UWP UI classes 并破坏了我的项目。如何在不使用 UNO.Foundation class 的情况下从 Xamarin.Forms 页 C# 后端调用 Javascript ?谢谢。
您可以使用混合网络视图,然后注入 JavaScript 代码来检测对此网络视图的每次点击 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview