Dictionary Xamarin中的字典绑定

Dictionary Xamarin中的字典绑定,dictionary,xamarin,xamarin.forms,binding,Dictionary,Xamarin,Xamarin.forms,Binding,我的模型如下所示: class Puzzle { public string Name {get; set;} public string Id {get; set;} public PuzzleKind Kind {get; set;} public Dictionary<string, string> Details {get; set;} } 类谜题 { 公共字符串名称{get;set;} 公共字符串Id{get;set;} 公共拼图种

我的模型如下所示:

class Puzzle
{
     public string Name {get; set;}
     public string Id {get; set;}
     public PuzzleKind Kind {get; set;}
     public Dictionary<string, string> Details {get; set;}
}
类谜题
{
公共字符串名称{get;set;}
公共字符串Id{get;set;}
公共拼图种类{get;set;}
公共字典详细信息{get;set;}
}
“详细信息”字段与我的不同谜题不同。在这些模板的UI表示中,我使用DateTemplateSelector来选择使用哪个模板,一个数据模板或另一个数据模板取决于数据类型。它就像一个符咒

我绑定到名称、Id和种类,没问题。我的问题是如何绑定到细节[“key”]

我知道什么样的细节将取决于谜题的类型,我创建了如下数据模板:

<DataTemplate x:Key="myFirstTemplate">
    <ViewCell>
        <Grid ... with definitions...>
             <Label Text="{Binding Path=Details["expectedKey"],
                    Converter={StaticResource myConverter}}"/>
        </Grid>
    </ViewCell>
</DataTemplate>
public class FirstClockConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {            
        return (value as Dictionary<string,string>)[parameter as string];
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这段代码只是在启动期间引发未处理的异常。。。我的问题是如何绑定到这一点,以及如何根据输入值更改字体颜色

如果“expectedKey”是硬编码的,那么您可以采用下一种方法:

<Label Text="{Binding Path=Details.[expectedKey]}" />
<ListView
    ItemsSource="{Binding Details}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Key}" Detail="{Binding Value}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

语法有点复杂,但没那么糟糕

您还可以通过下一种方式迭代字典:

<Label Text="{Binding Path=Details.[expectedKey]}" />
<ListView
    ItemsSource="{Binding Details}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Key}" Detail="{Binding Value}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

如果“expectedKey”是硬编码的,那么您可以用下一种方法:

<Label Text="{Binding Path=Details.[expectedKey]}" />
<ListView
    ItemsSource="{Binding Details}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Key}" Detail="{Binding Value}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

语法有点复杂,但没那么糟糕

您还可以通过下一种方式迭代字典:

<Label Text="{Binding Path=Details.[expectedKey]}" />
<ListView
    ItemsSource="{Binding Details}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Key}" Detail="{Binding Value}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我最终只是绑定到json,并在转换器中使用一个参数,以了解需要使用哪些预期参数。像这样:

<Label Text="{Binding Path=Details, Converter={StaticResource FirstClockConverter}, ConverterParameter=expectedKey}"/>

然后在后面的代码中,我处理这样的事情:

<DataTemplate x:Key="myFirstTemplate">
    <ViewCell>
        <Grid ... with definitions...>
             <Label Text="{Binding Path=Details["expectedKey"],
                    Converter={StaticResource myConverter}}"/>
        </Grid>
    </ViewCell>
</DataTemplate>
public class FirstClockConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {            
        return (value as Dictionary<string,string>)[parameter as string];
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
公共类FirstClockConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{            
返回(值作为字典)[参数作为字符串];
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}

这里的好处是,我可以根据参数调整转换(知道ExpectedParameter的类型,或者我甚至可以从我的主模型的静态属性中检查其他值,以便在屏幕上显示正确的反馈。所有绑定只有一个转换器。

我只绑定到json,并在转换器中使用一个参数,以了解需要哪些预期参数用过。像这样:

<Label Text="{Binding Path=Details, Converter={StaticResource FirstClockConverter}, ConverterParameter=expectedKey}"/>

然后在后面的代码中,我处理这样的事情:

<DataTemplate x:Key="myFirstTemplate">
    <ViewCell>
        <Grid ... with definitions...>
             <Label Text="{Binding Path=Details["expectedKey"],
                    Converter={StaticResource myConverter}}"/>
        </Grid>
    </ViewCell>
</DataTemplate>
public class FirstClockConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {            
        return (value as Dictionary<string,string>)[parameter as string];
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
公共类FirstClockConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{            
返回(值作为字典)[参数作为字符串];
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}

这里的好处是,我可以根据参数调整转换(知道ExpectedParameter的类型,或者我甚至可以从主模型的静态属性中检查其他值,以便在屏幕上显示正确的反馈。所有绑定只有一个转换器。

详细信息[expectedKey](不带引号)为我工作…

详细信息[expectedKey](不带引号)为我工作…

每个拼图都有多个详细信息?是的,不同的数字和类型(尽管它们都是json字符串)。每个拼图都有多个详细信息?是的,不同的数字和类型(尽管它们都是json字符串)最后,我将期望的键作为参数发送到ValueConverter。您是否赞成您的解决方案?听起来像是一个可行的解决方案。除此之外,很难说什么更适合您,这完全取决于您的需要。我只是分享了一些字典绑定示例来给您一个想法。最后,我将期望的键作为参数发送到ValueConverterValueConverter。你喜欢你的解决方案吗?听起来像是一个可行的解决方案。除此之外,很难说什么更适合你,这完全取决于你的需要。我只是分享了几个字典绑定示例来给你一个想法。