无法使用带有Flipview的ItemTemplateSelector

时间:2015-05-05 13:54:59

标签: c# xaml windows-phone-8.1 winrt-xaml winrt-xaml-toolkit

我正在尝试使用带有Flipview的ItemTemplateSelector,就像我将ListTemplateSelector与Listview一样使用但它不起作用,我无法找到有关如何使用Flipview的示例。

这是我的XAML:

<Page.Resources>
    <DataTemplate x:Key="textview">
        <TextBlock Text="{Binding textstring}" FontSize="140"/>
    </DataTemplate>
    <DataTemplate x:Key="imgview">
        <Image Source="{Binding imageurl}" />
    </DataTemplate>
    <local:flipviewselector x:Key="myselector" 
                            imageview="{StaticResource imgview}"
                            stringview="{StaticResource textview}"/>
</Page.Resources>

<FlipView Name="flip"
          ItemsSource="{Binding}"
          ItemTemplateSelector="{StaticResource myselector}"/>

这是我的C#代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var source = new ObservableCollection<flipviewitem>();
        source.Add(new flipviewitem("test1"));
        source.Add(new flipviewitem("image", new Uri("http://theheightsanimalhospital.com/clients/15389/images/playful-kitten-6683.jpg")));
        source.Add(new flipviewitem("test2"));
        source.Add(new flipviewitem("test3"));
        source.Add(new flipviewitem("test4"));
        flip.DataContext = source;
    }
}
public class flipviewselector : DataTemplateSelector
{
    public DataTemplate imageview { get; set; }
    public DataTemplate stringview { get; set; }

    protected override DataTemplate SelectTemplateCore(object item)
    {
        flipviewitem decide = item as flipviewitem;
        if (decide.imageurl != null)
        {
            return imageview;
        }
        return stringview;
    }
}
public class flipviewitem
{
    public flipviewitem(String text)
    {
        this.textstring = text;
    }
    public flipviewitem(String text, Uri url)
    {
        this.textstring = text;
        this.imageurl = url;
    }
    public String textstring { get; set; }
    public Uri imageurl { get; set; }
}

可以在没有ItemTemplateSelector的情况下正常绑定到集合。

1 个答案:

答案 0 :(得分:0)

在搜索了更多内容后,我发现SelectTemplateCore功能缺失了。 它需要看起来像这样:

@Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

        View vi = convertView;
        if (vi == null)
        {
            vi = m_inflater.inflate(R.layout.item_view, null);
        }

        ImageView theIcon = (ImageView) vi.findViewById(R.id.icon);
        if (dataItems.get(position).isChecked)
        {
            theIcon.setImageResource(R.drawable.icon1);
        } else
        {
            contactIcon.setImageResource(R.drawable.icon2);
        }
        return vi;
    }

不确定原因。