zoukankan      html  css  js  c++  java
  • Xamarin Forms MVVM实现效果说明

    实体对象定义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>
  • 相关阅读:
    【转】Wrapper配置详解及高级应用
    【转】Eclipse4.4.1安装velocity插件Veloeclipse.ui_2.0.8
    【转】创建SVN仓库的步骤
    【转】Hibernate 常见异常
    [转]Java中Map的用法详解
    [转]CocoaPods安装和使用教程
    [转]struts1.2的action参数配置
    [转]hibernateTools工具安装及使用总结(eclipse 3.6)
    [转]iOS开发中@property的属性weak nonatomic strong readonly等介绍
    [转]使用 Xcode 5 和 Interface Builder创建 Hello World App
  • 原文地址:https://www.cnblogs.com/jasondun/p/9923523.html
Copyright © 2011-2022 走看看