首页 > 解决方案 > Xamarin C# 将双嵌套集合绑定到代码中的 Switch IsToggledProperty

问题描述

我想Switch.IsToggledProperty使用 option 绑定到 View Model 的双嵌套集合属性BindingMode.TwoWay。在从控件绑定到视图模型的一种方式中,它运行良好,但是当我更新视图模型控件中的属性时,值不会改变。视图模型实现INotifyPropertyChanged

这是一些最重要的代码:

查看模型的属性(尝试了 2 个数组,但仍然无效):

    private Dictionary<int, Dictionary<string, UniversalSwitchViewModel>> _categoriesList;

    public Dictionary<int, Dictionary<string, UniversalSwitchViewModel>> CategoriesList
    {
        get
        {
            return _categoriesList;
        }
        set
        {
            SetProperty(ref _categoriesList, value);
        }
    }

我要绑定的开关控制:

var checkbox = new Switch { VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center };
//it works well only from View to View Model's property            
checkbox.SetBinding(Switch.IsToggledProperty, $"{nameof(_pageViewModel.CategoriesList)}[{regionId}].Value[{categoryNameOf}].{nameof(UniversalSwitchViewModel.Value)}", BindingMode.TwoWay);

从代码更改数据的示例(查看模型):

    public void SetCategoriesOption(Dictionary<string, int> categories, int regionId)
    {
        foreach (var item in categories)
        {
            CategoriesList[regionId][item.Key].Value = true;
        }

        CategoriesList = CategoriesList;
        //this.OnPropertyChanged(nameof(CategoriesList)); to be 100% sure I tried to run OnPropertyChanged event manually, but ofc it doesn't change anything
    }

完美的类似示例:

    private Dictionary<string, UniversalPickerViewModel> _selectedEnumsSettings;
    public Dictionary<string, UniversalPickerViewModel> SelectedEnumSettings
    {
        get
        {
            return _selectedEnumsSettings;
        }
        set
        {
            SetProperty(ref _selectedEnumsSettings, value);
        }
    }

checkbox.SetBinding(Switch.IsToggledProperty, $"{nameof(_pageViewModel.SelectedEnumSettings)}[{enumSettingKey}].{nameof(UniversalSwitchViewModel.Value)}", BindingMode.TwoWay);

编辑:忘记了 UniversalSwitchViewModel

public class UniversalSwitchViewModel
{
    public long Id { get; set; }

    public bool Value { get; set; }
}

编辑 2 以更好地重现:这是 CategoriesList 的工作原理。CategoriesList 是 UniversalSwitchViewModel 字典的字典,其中第一个键存储区域的 id(我有多个带多个开关的区域)。CategoriesList[regionId](例如:CategoriesList[0])为我们提供了开关 ViewModels 的列表,其中 key 是从服务器下载的每个类别的 nameOf(不要存储在这个地方 DisplayName)。CategoriesList[regionId][categoryNameOf](例如:CategoriesList[0][discount])返回 UniversalSwitchViewModel 存储开关值(bool)和 id 以更新服务器上的数据并保存设备上的设置。

通过回复@Jack Hua - MSFT 在绑定表达式中的评论 I 这部分代码我将从服务器(或设置)下载的每个类别绑定到其属性中的视图模型,其中 CategoriesList 是开关视图模型的存储库。regionId 是当前从选取器中选择的区域 ID,您可以假设它等于 0,并且 CategoriesList 有字典 CategoriesList[0]。categoryNameOf 是 category.NameOf 其中 category 是服务器设置中的项目,您可以假设如果您绑定 CategoriesList[0][xyz] 有 CategoriesList 包含 CategoriesList[0][xyz] 它将返回您具有 Id = 0 的样本 UniversalSwitchViewModel(可以是任何int) 和 Value = true(检查它是否有效更简单)。

最后一个例子:你可以绑定:

checkbox.SetBinding(Switch.IsToggledProperty, "CategoriesList[0].Value[xyz].Value", BindingMode.TwoWay);

它只能作为从视图到视图模型的单向投标,所以我认为绑定表达式很好。

我会很高兴有任何建议。

编辑 3: https ://github.com/xamarin/Xamarin.Forms/issues/9074 https://github.com/Sobek0/BindingBugExampleXamarinForms

标签: c#xamarincollectionsbindingnested

解决方案


推荐阅读