【问题标题】:Xamarin: passing parameters from binding to custom controlXamarin:将参数从绑定传递到自定义控件
【发布时间】:2020-06-23 20:00:45
【问题描述】:

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

它只接收 Xamarin.Forms.internals 的一些实例,而不是接收对象实例。我要么需要从中提取对象的实例,要么更改一些东西以使实际实例已经通过。

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

<?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();
        }
    }
}

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

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

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

    您的代码是正确的,但几乎没有错误。

        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);
        }
    

    【讨论】:

    • 这确实有效,但 BindableProperty 的名称并未在其他任何地方直接使用。这种隐式命名确实是唯一且有意的方法吗?对我来说似乎很奇怪..
    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多