Xamarin.Forms 在 ResourceDictionary 控件中形成正确的绑定方式



我在让它工作时遇到了问题。

<ResourceDictionary>
    <ViewCell x:Key="Separator">
        <Label Text="{Binding Title}" />
    </ViewCell>
</ResourceDictionary>

类 Option 包含一个名为 Title 的属性,该属性设置为任何文本。但是,以下代码不起作用。标签中不显示任何文本。文本仅保持"空"。我做错了什么 - 如何正确设置绑定?

if (Resources.ContainsKey("Separator"))
{
    var cell = Resources["Separator"] as Cell;
    if (cell != null)
    {
        cell.BindingContext = option;
        section.Add(cell);
    }
}

ResourceDictionary中的对象仅创建一次,每次使用它们时都会共享其实例。由于ViewCellBinding不能重复使用,因此这不太可能起作用。

您可以在ResourceDictionary中定义的是包含ViewCellDataTemplate,在这种情况下,可以共享它,因为将针对每种用法重新创建DataTemplate内容。

最新更新