其他分享
首页 > 其他分享> > xamarin简单数据绑定实例

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>

 

标签:xamarin,PropertyChanged,TextDataModel,绑定,System,text2,实例,using,public
来源: https://blog.csdn.net/missmecn/article/details/114062481