gpt4 book ai didi

c# - 无法在 Xamarin 中设置 SwitchCell 的绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 21:47:57 25 4
gpt4 key购买 nike

我正在尝试创建一个带有元素列表的 SwitchCell。尽管由于 stackoverflow,我发现了如何使用普通字符串列表来做到这一点,但当我尝试将 Cell-Properties 绑定(bind)到自制结构时,我无法找出我做错了什么。

这是我的代码:

public class RestaurantFilter
{
public List<FilterElement> Types;

public RestaurantFilter(List<string> types)
{
Types = new List<FilterElement>();

foreach (string type in types)
Types.Add(new FilterElement { Name = type, Enabled = false });
}
}

public struct FilterElement
{
public string Name;
public bool Enabled;
}

public FilterPage()
{
List<string> l = new List<string>(new string[] { "greek", "italian", "bavarian" });
RestaurantFilter filter = new RestaurantFilter(l);

ListView types = new ListView();
types.ItemTemplate = new DataTemplate(() =>
{
var cell = new SwitchCell();
cell.SetBinding(SwitchCell.TextProperty, "Name");
cell.SetBinding(SwitchCell.IsEnabledProperty, "Enabled");
return cell;
});
types.ItemsSource = filter.Types;

Content = types;

}

但应用程序中的 SwitchCell 不显示名称或 bool 值。

最佳答案

关于 IsEnabledProperty - IsEnabled 属性似乎存在一个已知错误,该错误将在 Xamarin.Forms 2.3.0-pre1 版本中修复,因此这可能与您的情况有关:

https://bugzilla.xamarin.com/show_bug.cgi?id=25662

关于 Name 属性 - 尝试将您的 FilterElement 结构更改为具有属性和 PropertyChangedEventHandler 的类,这样它会起作用:

public class FilterElement
{
public event PropertyChangedEventHandler PropertyChanged;

private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;

if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}

private bool _enabled;
public bool Enabled
{
get { return _enabled; }
set
{
_enabled = value;

if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Enabled"));
}
}
}
}

这样您就可以更新类型列表,它会自动更新 ListView。

顺便说一句,如果你想根据你的 ViewModels 打开或关闭过滤器(而不是启用或禁用它),你需要使用 OnProperty 进行绑定(bind):

https://developer.xamarin.com/api/field/Xamarin.Forms.SwitchCell.OnProperty/

关于c# - 无法在 Xamarin 中设置 SwitchCell 的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38030732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com