I wanted to try toggling a series of StackLayout's IsVisible property using a list of boolean. To better elaborate, here are my codes:
View
<ContentPage.Content>
<StackLayout>
<StackLayout IsVisible="{Binding pageVisible[0]}">
<Label Text="Page 1"/>
<Button Text="Next"
Command="{Binding ToNextCommand}"
CommandParameter="{Binding currentPage}"/>
</StackLayout>
<StackLayout IsVisible="{Binding pageVisible[1]}">
<Label Text="Page 2"/>
<Button Text="Next"
Command="{Binding ToNextCommand}"
CommandParameter="{Binding currentPage}"/>
</StackLayout>
<StackLayout IsVisible="{Binding pageVisible[2]}">
<Label Text="Page 3"/>
<Button Text="Next"
Command="{Binding ToNextCommand}"
CommandParameter="{Binding currentPage}"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
View Model
private List<bool> _pageVisible;
public List<bool> pageVisible
{
get => _pageVisible;
set
{
_pageVisible = value;
OnPropertyChanged();
}
}
public ProfileCompletionViewModel()
{
pageVisible = new List<bool>();
pageVisible.Add(true); //to make the first stacklayout visible
for (var i = 0; i < 2; i++)
{
pageVisible.Add(false);
}
}
public ICommand ToNextCommand { get; }
private int _currentPage = 0;
public int currentPage
{
get => _currentPage;
set => SetProperty(ref _currentPage, value);
}
private async Task ToNextFunc()
{
for (var i = 0; i < 3; i++)
pageVisible[i] = false; //Set all to false
pageVisible[_currentPage + 1] = true;
currentPage += 1;
}
}
The list is updating well when I clicked 'Next' (I found out using breakpoints). But the View is not updating despite having OnPropertyChange() on the List. Is there something wrong in my codes? T_T
Thank you in advance
1条答案
按热度按时间a14dhokn1#
我明白了,我只是试了一下它是否能用最少的代码行工作。我想我需要给每个属性一个单独的属性。
是的,您可以为每个属性定义几个单独的变量,但是您也可以为列表的项创建一个模型并实现接口
INotifyPropertyChanged
。我创建了一个demo来模拟这个函数,它在我这边工作正常。你可以参考下面的代码:
1.为列表项创建一个类
Item
。