Xamarin:将参数从绑定传递到自定义控件

西弗斯

我最近一直在学习 Xamarin 框架,我想创建一个可重用的控件元素来显示对象的某些属性。为了消除冗余,我想将完整的对象传递给控件并让它管理如何显示它。但是,从绑定传递对象时,我遇到了奇怪的行为。

它不接收对象实例,而是只接收 Xamarin.Forms.internals<TypedBinding<CoffeeCounter.Demo.Page, CoffeeCounter.Demo.Parameter> 的一些实例。我要么需要从中提取对象的实例,要么更改某些内容以使实际实例已经通过。

这是该问题的紧凑演示。首先是一个简单的页面视图,带有一个可重用控件的实例。

<?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:demo="clr-namespace:CoffeeCounter.Demo;assembly=CoffeeCounter"
    x:Class="CoffeeCounter.Demo.Page"
    x:DataType="demo:Page">
    <ContentPage.Content>
        <StackLayout>
            <demo:Control Parameter="{Binding Parameter}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

它接收带有要从代码隐藏显示的属性的参数对象。

using Xamarin.Forms;

namespace CoffeeCounter.Demo {
    public partial class Page : ContentPage {
        public Page() {
            InitializeComponent();
        }
        
        public Parameter Parameter => new Parameter{Foo="Football", Bar="Barricade"};
    }
}

参数类看起来像这样。

namespace CoffeeCounter.Demo {
    public class Parameter {
        public string Foo {get; set;}
        public string Bar {get; set;}
    
        public override string ToString() {
            return "Foo: " + Foo + ", " + "Bar: " + Bar;
        }
    }
}

然后像这样构建控件。

<?xml version="1.0" encoding="UTF-8"?>
<ContentView 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="CoffeeCounter.Demo.Control">
    <Label x:Name="Title" Text=""/>
</ContentView>
using Xamarin.Forms;

namespace CoffeeCounter.Demo {
    public partial class Control {
        public static readonly BindableProperty PARAMETER = BindableProperty.Create(
            "Parameter",
            typeof(object),
            typeof(Control),
            null,
            BindingMode.TwoWay,
            propertyChanged: (bindable, oldValue, newValue) => {
                var control = (Control) bindable;
                control.Title.Text = newValue.ToString();
            }
        );

        public object Parameter {
            set => SetValue(PARAMETER, value);
        }
        public Control() {
            InitializeComponent();
        }
    }
}

我知道自定义控件可能也可以使用绑定来更新其内容,但这不是这个问题的重点。

提前感谢您的任何答案:)

阿里艾哈迈德

您的代码是正确的,但错误很少。

    public Page()
    {
        InitializeComponent();
        BindingContext = this; //add this line
    }

    public Parameter Parameter => new Parameter { Foo = "Football", Bar = "Barricade" };

您的 Control.Xaml.cs 应该是这样的。

    public Control()
    {
        InitializeComponent();
    }

    public static readonly BindableProperty ParameterProperty = BindableProperty.Create(
        nameof(Parameter),
        typeof(object),
        typeof(Control),
        null,
        BindingMode.TwoWay,
        propertyChanged: (bindable, oldValue, newValue) => {
            var control = (Control)bindable;
            control.Title.Text = newValue.ToString();
        }
    );

    public object Parameter
    {
        set => SetValue(ParameterProperty, value);
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过参数将集合传递/绑定到FXML中的自定义组件(从vbox扩展)

将参数传递到自定义URI

绑定到ItemsControl的DataTemplate内部的自定义控件

将参数/参数从Google表格自定义函数传递到脚本函数

WPF自定义控件:将CollectionViewSource绑定到DependencyProperty

Angular2如何将参数传递到自定义表单控件验证器中?

将参数从XAML传递到自定义视图

WPF绑定到自定义控件的属性

绑定到自定义控件的依赖项属性

Xamarin Forms自定义控件数据绑定问题

将HTML5 <audio>标记的控件绑定到Angular中的自定义按钮

自定义控件中的Xamarin.Forms绑定集合

无法将ObservableCollection绑定到包含自定义控件的listView

将ControlTemplate子项绑定到自定义控件上的DependencyProperty无效

如何将对象作为可绑定属性传递给Xamarin.Forms自定义控件

将数据传递到自定义绑定处理程序的最佳方法是什么

将集合绑定到自定义控件属性

将ObservableCollection <int>绑定到自定义控件的IEnumerable <object>

将参数从控件传递到自定义授权

将参数传递到自定义的Rails路线中

将参数从自定义URL方案传递到Webview

自定义控件,绑定到背后的代码

将参数传递到自定义网址路由

将属性绑定到自定义控件的另一个属性

无法将对象的属性绑定到自定义控件

如何将 ObservableCollection 正确绑定到自定义控件属性?

将通用对象 (List<T>) 绑定到自定义 Xamarin 控件

Xamarin 自定义控件绑定不起作用

将 GeometryDrawing Brush 绑定到自定义控件依赖属性