Xamarin изменить или обновить ListView - PullRequest
0 голосов
/ 05 мая 2018

Мне нужен серьезный метод, который действительно может обновить мои свойства Xamarin ListView.
Все посты сообщают ObservableCollection<>, и это работает только на Remove.

.

По указанному коду:
Как изменить текст Button сразу после включения Switch?
(Внутри aa_Toggled())

DemoLists.XAML.CS

public partial class DemoLists : ContentPage/*, INotifyCollectionChanged*/
{

    private ObservableCollection<Contact> _contacts;

    public DemoLists()
    {
        InitializeComponent();

        _contacts = GetContactList();
        listView.ItemsSource = _contacts;
    }

    private ObservableCollection<Contact> GetContactList()
    {
        var l = new ObservableCollection<Contact> 
        {
            new Contact{Name="Mosh", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="t of child", MainTxtClr = PS.MainTextColor()},
            new Contact{Name="Mos", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="e of child", MainTxtClr = PS.MainTextColor()},
            new Contact{Name="Mosad", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="f of child", MainTxtClr = PS.MainTextColor()},
            new Contact{Name="Mosasd", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="g of child", MainTxtClr = PS.MainTextColor()},
            new Contact{Name="Masd", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
            new Contact{Name="Mash", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
            new Contact{Name="Mesh", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
            new Contact{Name="Mffsh", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
            new Contact{Name="Moasfgh", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
            new Contact{Name="John", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
            new Contact{Name="Jasdfn", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
            new Contact{Name="Joasfhn", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
            new Contact{Name="Johhedfhren", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
            new Contact{Name="Johewrgn", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
            new Contact{Name="Johasdn", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
            new Contact{Name="Johnzxv", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"}
        };
        return l;
    }

    private void aa_Toggled(object sender, ToggledEventArgs e)
    {
        var contact = (sender as Switch).BindingContext as Contact;
        var esd = contact.IsON;
        contact.ButtonText = esd.ToString();
    }
}



DemoLists.XAML

<ListView x:Name="listView"
          HasUnevenRows="True"
          IsGroupingEnabled="False">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal" x:Name="cellLayout" Padding="0,0,20,0">
                    <Switch x:Name="aa" IsToggled="{Binding IsON}" Toggled="aa_Toggled" />
                    <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" > 
                        <Label Text="{Binding Name}" TextColor="{Binding MainTxtClr}" FontSize="Small" FontAttributes="Bold"/>
                        <Label Text="{Binding Status}" />
                    </StackLayout>
                    <Button Text="{Binding ButtonText}"/>
                </StackLayout>
                <!--<ViewCell.ContextActions>
                    <MenuItem Text="Call" Clicked="Call_Clicked" CommandParameter="{Binding .}" />
                    <MenuItem Text="Delete" Clicked="Delete_Clicked" IsDestructive="True" CommandParameter="{Binding .}" />
                </ViewCell.ContextActions>-->
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>



Contact.CS

class Contact : INotifyPropertyChanged
{
    public bool IsON { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
    public string ImageUrl { get; set; }
    public Color MainTxtClr { get; set; }
    public string ButtonText { get; set; }
    public Contact()
    {
        MainTxtClr = PS.MainTextColor();
        IsON = true;
        ButtonText = "Follow Text";
    }
}



Скриншоты enter image description here

1 Ответ

0 голосов
/ 05 мая 2018

Измени свою модель

    class Contact : INotifyPropertyChanged
    {
        private bool _IsON;
        public bool IsON { get => _IsON; set { _IsON = value;  MainTxtClr = _IsON ? Color.Green : Color.Red; ; OnPropertyChanged(nameof(MainTxtClr)); } }

        public string Name { get; set; }
        public string Status { get; set; }
        public string ImageUrl { get; set; }
        public Color MainTxtClr { get; set; }
        public string ButtonText { get; set; }
        public Contact()
        {
            MainTxtClr = PS.MainTextColor();
            IsON = true;
            ButtonText = "Follow Text";
        }
public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName]string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }

Вы должны реализовать INotifyPropertyChanged в своей модели, чтобы уведомлять о любых изменениях в представлении.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...