简体   繁体   English

Xamarin DataBinding到bindableProperty(CustomView)

[英]Xamarin DataBinding to bindableProperty (CustomView)

i made a custom view which displays a text. 我做了一个自定义视图,显示文本。

in TestView.xaml 在TestView.xaml中

<?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="EodiRoad.TestView">
    <StackLayout>

        <Label Text="{Binding Test}"/>
        <Label Text="this is test view"/>

    </StackLayout>
</ContentView>

and the codebehind 和背后的代码

in TestView.xaml.cs 在TestView.xaml.cs中

    public partial class TestView : ContentView
    {


        public static BindableProperty TestProperty =
            BindableProperty.Create(
                propertyName: "Test",
                returnType: typeof(string),
                declaringType: typeof(TestView),
                defaultValue:"???"
            );

        public string Test
        {
            get
            { return (string)GetValue(TestProperty); }
            set
            {
                Debug.WriteLine("test setted value = " + (string)value);
                SetValue(TestProperty, value); 
            }
        }



        public TestView()
        {
            InitializeComponent();
            BindingContext = this;
        }
    }

and when i use this is some other page like this 当我使用这是其他这样的页面

<local:TestView Test="hjhkhjk"/>

it would work just fine. 它会很好地工作。 But when i bind a data to it 但是当我将数据绑定到它时

<local:TestView Test="{Binding post.uploader.username}"/>

then it wont desplay anything... 然后它不会显示任何内容...

its not a problem that post.uploader.username value is wrong or some that kind of thing. post.uploader.username值错误或类似问题不是问题。 because 因为

<Label Text="{Binding post.uploader.username}"/>

i have this code right under that malfunctioning line and it just works fine too.. 我在该故障线下有此代码,它也很好..

what am i doing wrong here? 我在这里做错了什么? how to fix it ..? 如何解决..?

You can make binding only from BindableObject property object in your TestView class. 您只能从TestView类中的BindableObject属性对象进行绑定。 For example, if you will write next code: 例如,如果您要编写下一个代码:

<local:TestView Test="{Binding post.uploader.username}" />

Your BindableObject object must have post property object, which has uploader property object, which has username property string 您的BindableObject对象必须具有post属性对象,该属性具有上uploader属性对象,该属性具有username属性字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM