xamarin - 为什么不同平台无法绑定(bind)背景色?

标签 xamarin xamarin.forms

这是 XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App1.MainPage">

    <BoxView>
        <BoxView.BackgroundColor>
            <OnPlatform x:TypeArguments="Color">
                <On Platform="Android" Value="{Binding First}"></On>
                <On Platform="iOS" Value="{Binding Second}"></On>
            </OnPlatform>
        </BoxView.BackgroundColor>
    </BoxView>
</ContentPage>

这是隐藏代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App1
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.BindingContext = this;
        }
        public Color First {
            get {
                return Color.FromHex("#575757");
            }
        }
        public Color Second
        {
            get
            {
                return Color.FromHex("#ffffff");
            }
        }
    }
}

运行程序后,报错: 指定的 Actor 阵容无效。

enter image description here

有什么问题吗?

最佳答案

我必须深入研究文档才能找到它。 OnPlatform无法为 Color 获取可绑定(bind)值属性(property)。

来自OnPlatform markup extension文档:

The XAML parser expects that values of the correct type will be provided to properties consuming the OnPlatform markup extension. If type conversion is necessary, the OnPlatform markup extension will attempt to perform it using the default converters provided by Xamarin.Forms. However, there are some type conversions that can't be performed by the default converters and in these cases the Converter property should be set to an IValueConverter implementation.

当您打开标签<BoxView.BackgroundColor>时,xaml 分析器将期望类型为 Color 的值。 ,就像您在下一行中定义的那样。之后,您将传递一个扩展 BindingBase 的值类并返回该类型的值。与解析器期望的可绑定(bind)值和 Color 对象存在强制转换冲突。

幕后发生的事情是(看评论):

<BoxView>
    <BoxView.BackgroundColor> <!-- You are saying that we want to modify a property of type Color -->
        <OnPlatform x:TypeArguments="Color"> <!-- Here the analyzer expects type Color, nothing else will work | checked build time -->
            <On Platform="Android" Value="{Binding First}"></On> <!-- The parser expects type Color, but is receiving type BindingBase | checked run time -->
            <On Platform="iOS" Value="{Binding Second}"></On> <!-- The parser expects type Color, but is receiving type BindingBase | checked run time -->
        </OnPlatform>
     </BoxView.BackgroundColor>
</BoxView>

根据文档,您可以尝试在转换器中定义转换,这不是最好的方法,因为您将在幕后隐藏这部分转换。另一种(也是更好的)方法是使用 BackgroundColor绑定(bind)到一个属性并定义其中的逻辑,如下所示:

<BoxView BackgroundColor="{Binding BgColor}" />

并在隐藏代码中:

public Color BgColor => Device.RuntimePlatform == Device.Android ? Color.FromHex("#575757") : Color.FromHex("#ffffff");

注意:最后的代码假设您仅针对 2 个平台进行开发 - iOS 和 Android。如果您正在/计划开发到其他平台,则需要扩展RuntimePlatform检查。

关于xamarin - 为什么不同平台无法绑定(bind)背景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62132549/

相关文章:

xaml - Xamarin - CollectionView,将当前项目作为命令参数传递

c# - 简单的 Mvvm 数据绑定(bind) - xamarin 表单

uitableview - 为什么在 iOS Xamarin 项目中使用 ViewCellRenderer 时出现 System.InvalidCastException

xaml - Xlabs ExtendedPicker SelectedItem 属性未绑定(bind)

c# - Xamarin Forms Android 中任何页面的默认背景颜色是什么?

c# - 如何在 c# Xamarin 中实现 ios 委托(delegate)/数据源模式?

xamarin - 如何将点击处理程序添加到 xamarin 表单的内容页面

android - 将监听器添加到 Google map

c# - 标签 TapGesture 不会触发 Xamarin Forms

c# - ASP.NET Identity 中使用的哈希方法