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

Flo*_*ian 0 xaml binding xamarin xamarin.forms

我在解决这个问题上遇到了问题.

<ResourceDictionary>
    <ViewCell x:Key="Separator">
        <Label Text="{Binding Title}" />
    </ViewCell>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

类Option包含名为Title的属性,该属性设置为任何文本.但是,以下代码无效.标签中不显示任何文字.文本只是保持"null".我做错了什么 - 我怎样才能正确设置Binding?

if (Resources.ContainsKey("Separator"))
{
    var cell = Resources["Separator"] as Cell;

    if (cell != null)
    {
        cell.BindingContext = option;

        section.Add(cell);
    }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*oix 8

a ResourceDictionary中的对象只创建一次,并且每次使用它们时都会共享它们的实例.由于ViewCell并且Binding不能重复使用,这不太可行.

可以在定义ResourceDictionaryDataTemplate包含ViewCell,在这种情况下,它可以共享的DataTemplate内容将被重新创建为每个使用.