简体   繁体   English

如何从Xamarin.Forms应用程序修改平台特定的属性?

[英]How do you modify platform specific properties from a Xamarin.Forms application?

I have a Xamarin.Forms Portable Application which contains a Page with a WebView control on it. 我有一个Xamarin.Forms便携式应用程序,其中包含带有WebView控件的Page

public class MainPage : ContentPage
{
    private WebView webView;

    public MainPage()
    {
        Content = (webView = new WebView());
    }
}

The WebView class is implemented differently on each platform -- each platform has it's own set of extra properties. WebView类在每个平台上的实现方式有所不同-每个平台都有自己的一组额外属性。 For example on iOS, the WebView control is rendered as a UIWebView control , which has additional platform specific properties , some of which are not surfaced via the WebView class. 例如,在iOS上, WebView控件呈现为UIWebView控件 ,该控件具有特定于平台的其他属性 ,其中某些属性不会通过WebView类浮出水面。

I would like to set some of those properties on a specific WebView control when my app is running on iOS. 当我的应用程序在iOS上运行时,我想在特定的WebView控件上设置其中一些属性。 (Likewise, I'd like to do the equivalent for the Android, and UWP projects as well; as well as for other Xamarin.Forms controls that are not WebViews.) (同样,我也想对Android和UWP项目以及与其他非WebView的Xamarin.Forms控件进行相同的操作。)

So, how do you set the properties of those platform specific controls in Xamarin.Forms? 那么,如何在Xamarin.Forms中设置这些平台特定控件的属性?

Posting an answer to help others, because I finally figured it out. 发布答案以帮助他人,因为我终于弄清楚了。 -- It looks like you need to implement a Custom Renderer . –看来您需要实现自定义渲染器

Essentially, in your device specific projects, you need to create a class that inherits the default renderer of a given control (there are some tables here . 本质上,在设备特定的项目中,您需要创建一个类,该类继承给定控​​件的默认渲染器(此处有一些表

Unfortunately, for more specific controls like WebView you will have to figure it out on your own. 不幸的是,对于像WebView这样的更具体的控件,您将不得不自己解决。

Finally, you have to add an assembly attribute to "export" your renderer. 最后,您必须添加一个程序集属性以“导出”渲染器。

For example, the class in the iOS project might look like this: 例如,iOS项目中的类可能如下所示:

using My.Project.iOS.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))]

namespace My.Project.iOS.Renderers
{
    internal class CustomWebViewRenderer : Xamarin.Forms.Platform.iOS.WebViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            var view = NativeView as UIKit.UIWebView;

            if (view != null)
            {    
                view.ScrollView.ScrollEnabled = false;
                view.ScrollView.Bounces = false;
            }
        }
    }
}

Note: that this renderer will be applied to ALL WebView controls rendered on iOS now. 注意:此渲染器将立即应用于iOS上渲染的所有WebView控件。 So if you want to only apply it to specific ones, you will need a way to signify to the custom renderer which ones to apply the additional settings to; 因此,如果您只想将其应用于特定的设置,则需要一种方法向自定义渲染器表示要将附加设置应用于哪些设置; the easiest way I've found to do this is with BindableProperty s (similar to DependencyProperty s in WPF). 我发现最简单的方法是使用BindableProperty (类似于WPF中的DependencyProperty )。

To do this, you declare your BindableProperty somewhere in the shared PCL project like so: 为此,您可以在共享PCL项目中的某处声明BindableProperty,如下所示:

namespace My.Project
{
    public static class Properties
    {
        public static readonly BindableProperty EnableScrollingProperty = BindableProperty.Create
        (
            "EnableScrolling",
            typeof(bool),
            typeof(WebView),
            true
        );
    }
}

And you can set it on your WebView object via: 您可以通过以下方法在WebView对象上进行设置:

webView.SetValue(My.Project.Properties.EnableScrollingProperty, false);

And the renderer's OnElementChanged method can be modified to get the value of that property and check it like so: 并且可以修改渲染器的OnElementChanged方法以获取该属性的值并进行如下检查:

protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
    base.OnElementChanged(e);

    var enableScrolling = e?.NewElement?.GetValue(Properties.EnableScrollingProperty) as bool?;

    if (enableScrolling.HasValue)
    {
        var view = NativeView as UIKit.UIWebView;
        if (view != null)
        {
            view.ScrollView.ScrollEnabled = enableScrolling.Value;
            view.ScrollView.Bounces = enableScrolling.Value;
        }
    }
}

您要么需要创建一个自定义渲染器 ,要么使用Effects来设置平台特定的属性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM