【问题标题】:Open a Browser Window with Javascript on Xamarin.Forms在 Xamarin.Forms 上使用 Javascript 打开浏览器窗口
【发布时间】:2020-02-10 14:42:49
【问题描述】:

我有一个 Xamarin.Forms 应用程序。它包括一个像这样的按钮:

 <Button x:Name="Buy_Button" Text="Satın Al" FontAttributes="Bold" TextColor="#e2e2e2" BackgroundColor="#2A52BE" FontFamily="Segoe UI" Grid.Column="2" Grid.ColumnSpan="1" Grid.RowSpan="1"  CornerRadius="5"  VerticalOptions="Start" HorizontalOptions="Center" FontSize="15.667" Grid.Row="0" Margin="0,10,10,0"  Clicked="Buy_Button_ClickedAsync" CommandParameter="{Binding  Buy_URL}" />

我正在发送一个 URL 链接来单击事件以打开特定网页。代码是:

  private async void Buy_Button_ClickedAsync(object sender, EventArgs e)
    {
        Button btn = (Button)sender; // Coming button from click event handler.
        var buylink = btn.CommandParameter.ToString(); // Get the CommandParameter.
                                                       //  await DisplayAlert("Satın alma linki", buylink, "Anladım"); // Show the link.
        try // Uwp & iOS & Android
        {
            await Browser.OpenAsync(new Uri(buylink), 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", ex.Message, "Anladım"); // Show the info about exception.

            // Jint - nt is a Javascript interpreter for .NET which provides full ECMA 5.1 compliance and can run on any .NET platform. 
            //Because it doesn't generate any .NET bytecode nor use the DLR it runs relatively small scripts faster. 
            //https://github.com/sebastienros/jint

            var engine = new Engine();
            engine.SetValue("log", new Action<object>(Console.WriteLine));

            engine.Execute(@"function openurl() { log('" + buylink + "'); }; openurl(); ");
        }
    }

在 UWP、Xamarin.iOS 和 Xamarin 中。 Android 此代码通过 Xamarin.Esssentials 运行:

 await Browser.OpenAsync(new Uri(buylink), BrowserLaunchMode.SystemPreferred); // Open url in-app browser for iOS & Android- native in UWP

但是,我的 Xamarin.Forms 应用程序使用 Uno 平台投影到 WebAssembly 代码,因此此代码块未运行。因此。我将Jint 安装到 Xamarin.Forms 应用程序。此 catch 块打印到浏览器控制台的链接,但 API 参考中没有 window.open 函数跟踪:

 catch (NotImplementedInReferenceAssemblyException ex) //Wasm falls here because lack of Xamarin.Essentials.
            {
                // await DisplayAlert("Hata", ex.Message, "Anladım"); // Show the info about exception.

                       // Jint - nt is a Javascript interpreter for .NET which provides full ECMA 5.1 compliance and can run on any .NET platform. 
                //Because it doesn't generate any .NET bytecode nor use the DLR it runs relatively small scripts faster. 
                //https://github.com/sebastienros/jint

                var engine = new Engine();
                engine.SetValue("log", new Action<object>(Console.WriteLine));

                engine.Execute(@"function openurl() { log('" + buylink + "'); }; openurl(); ");
            }
        }

如何通过 Javascript 表单 Xamarin.Forms C# 代码在 WASM 上打开 WebBrowser 页面?谢谢。

【问题讨论】:

    标签: javascript xamarin xamarin.forms uno-platform


    【解决方案1】:

    两件事:

    1。使用浏览器!

    在 Wasm 上,您在 webassembly 环境中运行,该环境在 javascript 虚拟机中运行(这并不完全准确,但足够接近我的观点)。也就是说你可以直接调用运行环境(浏览器)的javascript。

    调用 native javascript...

    WebAssemblyRuntime
        .InvokeJS("(function(){location.href=\"https://www.wikipedia.com/\";})();");
    

    在您的情况下,由于您想打开浏览器窗口,因此需要使用这种方法,因为 Jint 无法从浏览器本身访问任何内容。

    2。无论如何,您仍然可以调用 Jint(但不能打开新窗口)

    如果您仍想使用 Jint 调用代码(因为您可以!!),您需要从链接过程中排除 Jint.dll 程序集。可能是因为它使用反射来操作。 再一次,它不能按照你的要求打开一个窗口,但是如果你出于任何其他原因需要调用 Jint,它会像在其他平台上一样工作!

    将此添加到您的 LinkerConfig.xml(在 Wasm 项目中):

      <assembly fullname="Jint" />
    

    还有……你给了我一个想法,我用 Jint 做了一些很酷的事情……

    我把整个解决方案放在那里:https://github.com/carldebilly/TestJint

    它可以工作,即使在 Wasm 上:

    有趣的代码: https://github.com/carldebilly/TestJint/blob/master/TestJint/TestJint.Shared/MainPage.xaml.cs#L18-L40

            private void BtnClick(object sender, RoutedEventArgs e)
            {
                void Log(object o)
                {
                    output.Text = o?.ToString() ?? "<null>";
                }
    
                var engine = new Engine()
                        .SetValue("log", new Action<object>(Log));
    
                engine.Execute(@"
          function hello() { 
            log('Hello World ' + new Date());
          };
    
          hello();
        ");
    
    #if __WASM__
                output2.Text =
                    WebAssemblyRuntime.InvokeJS("(function(){return 'Hello World ' + new Date();})();");
    #else
                output2.Text = "Not supported on this platform.";
    #endif
            }
    

    最后说明

    UWP/WinUI XAML上,您可以直接在您的 XAML 中添加&lt;Hyperlink /&gt;。我对 Xamarin Forms 不够熟悉,不知道是否有等价物。

    【讨论】:

    • 嗨@Carl e Billy。感谢您的回答。我可以通过 Xamarin 做到这一点。 UWP/WinUI XAML 的表单标签(Xamarin.Forms 在 Label 类中具有等效的 )。我会尝试你的建议,让你学习输出。
    • 感谢@Carl de Billy,如何在 Xamarin 上使用它。表格应用程序? WebAssemblyRuntime.InvokeJS 在 Uno.UI 类中可用。如果我将它的 NuGet 包安装到 Forms 项目中,VS 由于类歧义而无法构建和部署 UWP 应用端。
    【解决方案2】:

    我正在使用 Device.OpenUri,它可以在带有 Xamarin.Forms 的 WASM 中使用

    Device.OpenUri(new Uri("https://www.bing.com"));
    

    【讨论】:

    • 谢谢。我会再试一次:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    相关资源
    最近更新 更多