简体   繁体   English

如何将Xamarin.Forms WebView对象转换为平台特定的WebView对象?

[英]How to convert Xamarin.Forms WebView object to platform-specific WebView object?

I have a WebView object created in Xamarin.Forms: 我在Xamarin.Forms中创建了一个WebView对象:

WebView webView = new WebView {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

I need to do some platform-specific operations on this object: call some functions that aren't available in Xamarin.Forms because of different nature of this on different platforms. 我需要对该对象执行一些特定于平台的操作:调用Xamarin.Forms中不可用的一些函数,因为此特性在不同平台上不同。

The problem is, when I pass the Xamarin.Forms object: 问题是,当我传递Xamarin.Forms对象时:

public interface IWebViewCallCSharp
{
    void passWebView(WebView webView);
}

I can do on "native side" exactly the same things as I could in Xamarin.Forms. 我可以在“本机方面”执行与Xamarin.Forms中完全相同的操作。 How can I get some platform-specific functions of WebView on each platform? 如何在每个平台上获得某些特定于平台的WebView功能? How to convert this to this ? 如何转换这个

You will need to implement a so-called custom renderer. 您将需要实现一个所谓的自定义渲染器。 Start here: Implementing a HybridWebView . 从这里开始: 实现HybridWebView

In short you will need to: 简而言之,您将需要:

  1. Create the HybridWebView custom control. 创建HybridWebView自定义控件。
  2. Consume the HybridWebViewfrom Xamarin.Forms. 使用Xamarin.Forms中的HybridWebView。
  3. Create the custom renderer for the HybridWebView on each platform. 在每个平台上为HybridWebView创建自定义渲染器。

In the last step you need to 在最后一步中,您需要

  1. Create a subclass of the ViewRenderer class that renders the custom control. 创建用于渲染自定义控件的ViewRenderer类的子类。 The first type argument should be the custom control the renderer is for, in this case HybridWebView. 第一个类型参数应该是渲染器所针对的自定义控件,在这种情况下为HybridWebView。 The second type argument should be the native control that will implement the custom view. 第二个类型参数应该是将实现自定义视图的本机控件。
  2. Override the OnElementChanged method that renders the custom control and write logic to customize it. 重写呈现自定义控件的OnElementChanged方法并编写逻辑以对其进行自定义。 This method is called when the corresponding Xamarin.Forms custom control is created. 创建相应的Xamarin.Forms自定义控件时,将调用此方法。
  3. Add an ExportRenderer attribute to the custom renderer class to specify that it will be used to render the Xamarin.Forms custom control. 向自定义渲染器类添加一个ExportRenderer属性,以指定将其用于呈现Xamarin.Forms自定义控件。 This attribute is used to register the custom renderer with Xamarin.Forms. 此属性用于向Xamarin.Forms注册自定义渲染器。

The starting point where you begin using native controls is the OnElementChanged override. 开始使用本机控件的起点是OnElementChanged覆盖。

protected override void OnElementChanged (ElementChangedEventArgs<NativeListView> e)
{
  base.OnElementChanged (e);

  if (Control == null) {
    // Instantiate the native control and assign it to the Control property
  }

  if (e.OldElement != null) {
    // Unsubscribe from event handlers and cleanup any resources
  }

  if (e.NewElement != null) {
    // Configure the control and subscribe to event handlers
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin.forms无法使用Xamarin.Auth和特定的android平台从对象转换为system.type - Xamarin.forms cannot convert from object to system.type with Xamarin.Auth and specific android platform Xamarin.Forms Prism:特定于平台的服务的Android依赖项注入 - Xamarin.Forms Prism: Android dependency injection of platform-specific service Xamarin.Forms: UWP platform: Unable to cast object of type 'Windows.UI.Xaml.Controls.WebView' to type 'Windows.UI.Xaml.Controls.IWebView6' - Xamarin.Forms : UWP platform : Unable to cast object of type 'Windows.UI.Xaml.Controls.WebView' to type 'Windows.UI.Xaml.Controls.IWebView6' 如何应用Xamarin平台特定的EnableTranslucentNavigationBar - How to apply Xamarin Platform-Specific EnableTranslucentNavigationBar 如何在WebView Xamarin.Forms中加载带有javascript内容的HTML页面? - How to load HTML page with javascript content in a WebView Xamarin.Forms? 如何处理Xamarin.forms Webview中的加载错误 - How to handle load errors in Xamarin.forms webview 如何滚动到Xamarin Forms WebView中的特定位置? - How to scroll to a specific position into Xamarin Forms WebView? Xamarin.Forms中的WebView的透明背景(便携式) - Transparent background of WebView in Xamarin.Forms (Portable) 在 Xamarin.Forms for iOS 中缓存 WebView - Cache WebView in Xamarin.Forms for iOS WebView 事件未在 Xamarin.Forms 中触发 - WebView events not firing in Xamarin.Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM