将对象绑定到xamarin表单中的自定义控件

时间:2017-01-09 17:38:19

标签: xamarin.forms

我试图通过填充可重用控件的页面来构建我的应用程序。我想在运行时将对象绑定到自定义控件,以便根据其中的页面填充其属性。到目前为止,我所尝试的并不正常,我想知道是否有更好的方法。我遇到的当前错误表明它无法将Binding类型转换为String。以下是我迄今为止尝试过的一个例子。

查看型号:

public class MenuItem
    {
        public Page pageTo { get; set; } 
        public string title { get; set; } 
        public string subtitle { get; set; } 
        public string iconPath { get; set; } 
    }

    public class VMMenuItem : ContentView
    {
        public MenuItem item { get; set; } 

    public static BindableProperty ItemProperty = BindableProperty.Create("Item", typeof(MenuItem), typeof(VMMenuItem), null, BindingMode.TwoWay, );

    public VMMenuItem()
    {
        var menuTapped = new TapGestureRecognizer();


        StackLayout Main = new StackLayout

        {
            BindingContext = item,
            Children = {

                new SectionLine(),
                new StackLayout
                {

                    Padding = new Thickness(10),
                    Orientation = StackOrientation.Horizontal,
                    HorizontalOptions = LayoutOptions.Fill,
                    Children = {
                        new Label {

                            Margin = new Thickness(10, 2, 0, 0),
                            HorizontalOptions = LayoutOptions.StartAndExpand,
                            Text = "{Binding title}"
                        },
                        new Label
                        {

                            Margin = new Thickness(10, 2, 10, 0),
                            FontSize = 14,
                            TextColor = Color.FromHex("#c1c1c1"),
                            HorizontalOptions = LayoutOptions.End,
                            Text = "{Binding subtitle}"
                        },
                        new Image {

                            HorizontalOptions = LayoutOptions.End,
                            Source = "{Binding iconPath}",
                            WidthRequest = 20
                        }
                    }
                }
            }
        };

        Main.GestureRecognizers.Add(menuTapped);
        Content = Main;
    }

    public MenuItem menuItem
    {
        get { return (MenuItem)GetValue(ItemProperty); } 
        set { SetValue(ItemProperty, value); } 
    }
}

页面前端:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:local="clr-namespace:TamarianApp;assembly=TamarianApp" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TamarianApp.RugPage">
    <Grid>

        <local:VMMenuItem menuItem="{Binding lengthMenuItem}"></local:VMMenuItem>

    </Grid>
</ContentPage>

Page Backend:

public partial class RugPage : ContentPage
    {

        MenuItem lengthMenuItem; 
        public RugPage()
        {
            InitializeComponent();

            App.currentPage = this;
            BindingContext = App.rug;
            lengthMenuItem = new MenuItem
            {
                title = "length",
                iconPath = "icons/blue/next",
                subtitle = "somelength"
            };
        }
   }

1 个答案:

答案 0 :(得分:1)

您需要使用可绑定属性,这是一个如何使用它们的示例:

public static readonly BindableProperty ImageProperty = 
    BindableProperty.Create(nameof(Image), typeof(ImageSource), typeof(DashEntry), null); 
public ImageSource Image
{
    get
    {
        return (ImageSource)GetValue(ImageProperty);
    }
    set
    {
        SetValue(ImageProperty, value);
    }
}

关于它的文档:

https://developer.xamarin.com/guides/xamarin-forms/xaml/bindable-properties/