В настоящее время у меня есть проблема, похожая на эту
Как я могу остановить запуск события на переключателе при загрузке страницы?
Кроме моей ошибки, то, что когда я переключаю свой переключатель, это вызывает бесконечный цикл в InitGroupMethod. Это потому, что я меняю переключаемое событие программно во время этого. Что снова вызывает toggleEvent. Я хочу, чтобы при отключении категории все нижние категории больше не добавлялись, а страница обновлялась.
В настоящее время загружается правильно при открытии страницы, и если вы сохраняете список, а затем снова его посещаете. Но не удается обновить представление, когда переключатель переключается без бесконечного цикла.
Изображение здесь https://gyazo.com/eedd4ab2ab3cd892ef57cd2d245d7dd1?token=d39388a4b5b726f6c5e2f44219dd4b6f
Как получить вид для обновления? Я пытался вызвать пользовательские switchcell и desubb из события toggleevent, прежде чем изменить это, но не смог получить доступ к x: name, потому что это список. Я тоже пытался превратить его в коммутатор. Xa
Список брендов viewmodel
public class BrandViewModel : ObservableCollection<SubBrandViewModel>, INotifyPropertyChanged
{
private bool isEnabledBrand;
public event PropertyChangedEventHandler PropertyChanged;
public string HouseCode { get; set; }
public List<SubBrandViewModel> SubBrands { get; set; }
public bool IsEnabledBrand
{
get => isEnabledBrand;
set
{
isEnabledBrand = value;
NotifyPropertyChanged();
}
}
}
PageViewModel
public ObservableCollection<Grouping<BrandViewModel, SubBrandViewModel>> GroupedItems { get; set; }
public List<BrandViewModel> Categories { get; set; }
public List<string> AllowedBrands { get; set; }
public List<string> AllowedSubBrands { get; set; }
public void ComputeUiBrandStatus()
{
Dictionary<string, bool> tableWithStrings = new Dictionary<string, bool>();
foreach (string code in AllowedBrands)
{
if (!tableWithStrings.ContainsKey(code))
{
tableWithStrings.Add(code, true);
}
}
foreach (BrandViewModel category in Categories)
{
if (tableWithStrings.ContainsKey(category.HouseCode))
{
category.IsEnabledBrand = true;
}
if (category.SubBrands != null)
{
foreach (SubBrandViewModel subCategory in category.SubBrands)
{
if (tableWithStrings.ContainsKey(subCategory.Code))
{
subCategory.IsEnabledSubBrand = true;
}
}
}
}
}
private void InitGrouppedData()
{
GroupedItems.Clear();
var items = new ObservableCollection<Grouping<BrandViewModel, SubBrandViewModel>>();
foreach (BrandViewModel category in Categories)
{
List<SubBrandViewModel> subCategory = new List<SubBrandViewModel>();
if (category.SubBrands != null && category.IsEnabledBrand)
{
subCategory.AddRange(category.SubBrands);
}
Grouping<BrandViewModel, SubBrandViewModel> group = new Grouping<BrandViewModel, SubBrandViewModel>(category, subCategory);
items.Add(group);
}
GroupedItems = items;
}
private void OnToggleSwitch(object obj)
{
RemoveOrAddSubBrands();
}
Страница
<ListView
ItemsSource="{Binding GroupedItems}"
IsGroupingEnabled="True"
GroupDisplayBinding="{Binding Key.HouseCode}"
GroupShortNameBinding="{Binding Key.HouseCode}"
HasUnevenRows="False"
Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalOptions="FillAndExpand"
SelectionMode="None">
<ListView.GroupHeaderTemplate >
<DataTemplate>
<ViewCell >
<FlexLayout Direction="Row" JustifyContent="SpaceBetween">
<FlexLayout.Triggers>
<DataTrigger TargetType="FlexLayout" Binding="{Binding Source={x:Reference SwitchParent}, Path=IsToggled}" Value="True">
<Setter Property="BackgroundColor" Value="{StaticResource ThemeBkColor}" />
</DataTrigger>
</FlexLayout.Triggers>
<Label Text="{Binding Key.HouseCode}" VerticalOptions="Center" VerticalTextAlignment="Center" Margin="20,0,0,0" >
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference SwitchParent},Path=IsToggled}" Value="True">
<Setter Property="TextColor" Value="White" />
</DataTrigger>
</Label.Triggers>
</Label>
<Switch x:Name="SwitchParent" IsToggled="{Binding Key.IsEnabledBrand}" >
<Switch.Behaviors>
<controls:EventToCommandBehavior EventName="Toggled" Command="{Binding Source={x:Reference BrandPage}, Path=ViewModel.SwitchToggledCommand}" CommandParameter="{Binding .}"/>
</Switch.Behaviors>
</Switch>
</FlexLayout>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<FlexLayout Direction="Row" JustifyContent="SpaceBetween" Margin="30,0,0,0">
<FlexLayout.Triggers>
<DataTrigger TargetType="FlexLayout" Binding="{Binding Source={x:Reference Switch}, Path=IsToggled}" Value="True">
<Setter Property="BackgroundColor" Value="{StaticResource ThemeBkColor}" />
</DataTrigger>
</FlexLayout.Triggers>
<Label Text="{Binding Code}" VerticalOptions="Center" VerticalTextAlignment="Center" Margin="20,0,0,0" >
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference Switch},Path=IsToggled}" Value="True">
<Setter Property="TextColor" Value="White" />
</DataTrigger>
</Label.Triggers>
</Label>
<Switch x:Name="Switch" IsToggled="{Binding IsEnabledSubBrand}" />
</FlexLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>