【问题标题】:How do you modify platform specific properties from a Xamarin.Forms application?如何从 Xamarin.Forms 应用程序修改平台特定属性?
【发布时间】:2017-02-28 07:28:01
【问题描述】:

我有一个Xamarin.Forms 便携式应用程序,其中包含一个Page 和一个WebView 控件。

public class MainPage : ContentPage
{
    private WebView webView;

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

WebView 类在每个平台上的实现方式不同——每个平台都有自己的一组额外属性。例如在 iOS 上,the WebView control is rendered as a UIWebView control,它有 additional platform specific properties,其中一些不是通过 WebView 类显示的。

当我的应用在 iOS 上运行时,我想在特定的 WebView 控件上设置其中的一些属性。 (同样,我想为 Android 和 UWP 项目做同样的事情;以及其他不是 WebViews 的 Xamarin.Forms 控件。)

那么,如何在 Xamarin.Forms 中设置这些平台特定控件的属性?

【问题讨论】:

  • This 来自我的书签。想测试这个,但一直没有机会。也许在这种情况下会有用。
  • @AVKNaidu Device 类在这里没有帮助。 -- 用于根据当前平台将表面属性设置为不同的值,或用于运行不同的代码块等。

标签: c# android ios xamarin.forms uwp


【解决方案1】:

发布答案以帮助他人,因为我终于想通了。 -- 看来你需要实现一个Custom Renderer

本质上,在您的设备特定项目中,您需要创建一个继承给定控​​件的默认渲染器的类(有some tables here

不幸的是,对于像 WebView 这样更具体的控件,您必须自己解决。

最后,你必须添加一个程序集属性来“导出”你的渲染器。

例如,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;
            }
        }
    }
}

注意:此渲染器将应用于现在在 iOS 上渲染的所有 WebView 控件。因此,如果您只想将其应用于特定的,您将需要一种方法来向自定义渲染器指示哪些应用附加设置;我发现最简单的方法是使用 BindablePropertys(类似于 WPF 中的 DependencyPropertys)。

为此,您需要在共享 PCL 项目中的某处声明 BindableProperty,如下所示:

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

您可以通过以下方式在 WebView 对象上设置它:

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

并且可以修改渲染器的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;
        }
    }
}

【讨论】:

    【解决方案2】:

    您要么需要创建一个custom renderer,要么使用Effects 来设置特定于平台的属性。

    【讨论】:

      猜你喜欢
      • 2015-04-25
      • 2016-11-20
      • 1970-01-01
      • 2021-03-19
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      相关资源
      最近更新 更多