我需要一个可以真正更新我的Xamarin ListView属性的严肃方法。
每个帖子都在告诉ObservableCollection<>
,除了Remove
之外什么都不行。
引用代码:
如何在我打开Button
后立即更改Switch
文字?
(内部{{1 }})
的 DemoLists.XAML.CS
aa_Toggled()
的 DemoLists.XAML
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();
}
}
的 Contact.CS
<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>
答案 0 :(得分:2)
更改模型
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以通知对视图的任何更改。