专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

Xamarin Forms MVVM实现效果说明

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:56:18       共计:3596 浏览

实体对象定义Model2  和Model均可 实现响应效果

public class BaseModel : INotifyPropertyChanged
    {
        private bool _selected;
        public bool Selected
        {
            get { return _selected; }

            set
            {
                if (Selected != value)
                {
                    _selected = value;
                    OnPropertyChanged();
                    OnPropertyChanged("StatusFormat");
                }
            }
        }

        public string StatusFormat
        {
            get
            {
                return Selected ? "选中了" : "没选中";
            }
        }



        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class BaseModel2 : BindableObject
    {
        public static readonly BindableProperty SelectedProperty =
       BindableProperty.Create("Selected",
           typeof(bool),
           typeof(bool),
          false

           );
        public bool Selected
        {
            get
            {
                return (bool)this.GetValue(SelectedProperty);
            }
            set
            {
                if (Selected != value)
                {
                    this.SetValue(SelectedProperty, value);
                    //通知下另外的依赖属性跟着变
                    OnPropertyChanged("StatusFormat");
                }
            }
        }

        public string StatusFormat
        {
            get
            {
                return Selected ? "选中了" : "没选中";
            }
        }
    }


public partial class MainPage : ContentPage
    {
        /// <summary>
        /// 我实现了2个mvvm方式 BaseModel  和 BaseModel2  都可以实现响应
        /// </summary>
        private ObservableCollection<BaseModel2> Models;
        public int Tick { get; set; }
        public MainPage()
        {
            InitializeComponent();
            Models = new ObservableCollection<BaseModel2>();
            lst.ItemsSource = Models;
            this.BindingContext = this;
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            Tick++;
            this.OnPropertyChanged("Tick");
            if (Models.Count==0)
            {
                Models.Add(new BaseModel2 { Selected = false });
                Models.Add(new BaseModel2 { Selected = true });
                Models.Add(new BaseModel2 { Selected = true });
                Models.Add(new BaseModel2 { Selected = false });
            }
            

            Models[new Random().Next(Models.Count)].Selected = !Models[new Random().Next(Models.Count)].Selected;

        }
    }


<?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:local="clr-namespace:App5Mvvm"
             x:Class="App5Mvvm.MainPage">

    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="Xamarin MVVM哪些事"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
        <Label Text="{Binding Tick, StringFormat='{0}次点击'}}"></Label>
        <Button Clicked="Button_Clicked" Text="点我点我"></Button>
        <ListView x:Name="lst">
            <ListView.ItemTemplate>
                <DataTemplate >
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <BoxView WidthRequest="22" Color="Red" IsVisible="{Binding Selected}"></BoxView>
                            <Label Text="{Binding StatusFormat}"></Label>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

</ContentPage>


版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:Xamarin.Forms——尺寸大小 | ·下一条:XamarinSQLite教程在Xamarin.Android项目中使用数据库

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有