1. 程式人生 > >Xamarin Forms MVVM實現效果說明

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>