xamarin简单数据绑定实例

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

namespace App2
{
    public partial class MainPage : ContentPage
    {

        public class TextDataModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            private string _text1;
            private string _text2;

            public string text1
            {
                get { return _text1; }
                set
                {
                    _text1 = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("text1"));
                    }
                }
            }

            public string text2
            {
                get { return _text2; }
                set
                {
                    _text2 = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("text2"));
                    }
                }
            }
        }

        public TextDataModel TextData1 = new TextDataModel();
        public TextDataModel TextData2 = new TextDataModel();

        public MainPage()
        {
            InitializeComponent();

            edit1.BindingContext = TextData1;
            label1.BindingContext = TextData1;
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App2.MainPage">

    <StackLayout>
        <Editor x:Name="edit1" Text="{Binding Path=text1, Mode=OneWayToSource}"/>
        <Label x:Name="label1" Text="{Binding Path=text1, Mode=Default}" FontSize="Title"/>
        <Label x:Name="label2" FontSize="Title"/>
    </StackLayout>

</ContentPage>

 

上一篇:Android程序员必备!给后辈的一点建议,值得收藏!


下一篇:Xamarin Android 8.0 创建前景服务的个人粗略总结(不是详细教程)