WP 8.1:DataTemplateSelector无法正常工作 [英] WP 8.1: DataTemplateSelector isn't working

查看:73
本文介绍了WP 8.1:DataTemplateSelector无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows Phone 8.1 Pivot App项目.

我正在尝试在xaml列表视图中创建DataTemplate的选择,但是它失败了.我可以运行该应用程序,但是我休息了:global::System.Diagnostics.Debugger.Break().

I'm trying to create DataTemplate's selection in a xaml listview but it fail. I can run the app but i got a break: global::System.Diagnostics.Debugger.Break().

这是一些代码.

我的DataTemplateSelector

namespace DMI
{
    public class ExploreTemplateSelector : DataTemplateSelector
    {
        public DataTemplate FolderTemplate { get; set; }
        public DataTemplate DocumentTemplate { get; set; }

        protected override DataTemplate SelectTemplateCore(object item)
        {
            if (item.GetType() == typeof(FolderExplore))
                return FolderTemplate;
            else 
                return DocumentTemplate;
        }
    }
}

我的数据透视XAML

<Page
    x:Class="DMI.PivotPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DMI"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:DMI.Data"
    mc:Ignorable="d"
    Background="#424242">
    <!--Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">-->
    <Page.Transitions>
        <TransitionCollection>
            <NavigationThemeTransition>
                <NavigationThemeTransition.DefaultNavigationTransitionInfo>
                    <CommonNavigationTransitionInfo IsStaggeringEnabled="True"/>
                </NavigationThemeTransition.DefaultNavigationTransitionInfo>
            </NavigationThemeTransition>
        </TransitionCollection>
    </Page.Transitions>

    <Grid>
        <Image Margin="0,2,1,523" Source="Assets/WideLogo.scale-240.png" Stretch="Fill" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <CompositeTransform ScaleX="-1"/>
            </Image.RenderTransform>
        </Image>
        <Pivot 
            x:Uid="Pivot"
            x:Name="pivot" 
            CommonNavigationTransitionInfo.IsStaggerElement="True" 
            Margin="0,20,0,0"
            SelectionChanged="Pivot_SelectionChanged">

            <PivotItem
                x:Uid="PivotItem1"
                x:Name="PivotItem1"
                Header="Dashboard"
                CommonNavigationTransitionInfo.IsStaggerElement="True">

                <ListView
                    x:Name="ListDocuments"
                    IsItemClickEnabled="True"
                    ItemClick="ItemView_ItemClick"
                    ContinuumNavigationTransitionInfo.ExitElementContainer="True">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <local:ExploreTemplateSelector>
                                <local:ExploreTemplateSelector.FolderTemplate>
                                    <DataTemplate>
                                        <Canvas Background="Transparent">
                                            <Image Source="Assets/StoreLogo.scale-240.png"  VerticalAlignment="Stretch" Margin="0,0,0,0" Width="80" Height="30" Stretch="Fill" />
                                            <TextBlock TextWrapping="Wrap" Text="{Binding folderName}" FontSize="20" FontWeight="Normal" VerticalAlignment="Center" Margin="80,0,0,0" />
                                        </Canvas>
                                    </DataTemplate>
                                </local:ExploreTemplateSelector.FolderTemplate>
                                <local:ExploreTemplateSelector.DocumentTemplate>
                                    <DataTemplate>
                                        <Canvas Background="Transparent">
                                            <Image Source="Assets/StoreLogo.scale-240.png"  VerticalAlignment="Stretch" Margin="0,0,0,0" Width="80" Height="30" Stretch="Fill" />
                                            <TextBlock TextWrapping="Wrap" Text="{Binding documentName}" FontSize="20" FontWeight="Normal" VerticalAlignment="Center" Margin="80,0,0,0" />
                                        </Canvas>
                                    </DataTemplate>
                                </local:ExploreTemplateSelector.DocumentTemplate>
                            </local:ExploreTemplateSelector>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </PivotItem>

            <PivotItem
                x:Uid="PivotItem2"
                x:Name="PivotItem2"/>

            <PivotItem
                x:Uid="PivotItem3"
                x:Name="PivotItem3"/>

            <PivotItem
                x:Uid="PivotItem4"
                x:Name="PivotItem4"/>
        </Pivot>
    </Grid>
</Page>

我在xaml中有两个错误: 错误1:

I've got two erros in the xaml: Error 1:

The specified value cannot be assigned. The following type was expected: "DependencyObject".    
PivotPage.xaml  49

错误2:

Property 'VisualTree' does not support values of type 'ExploreTemplateSelector'.    
PivotPage.xaml  49

而且看来<local:ExploreTemplateSelector>在我的xaml中甚至没有被识别. 有什么想法吗?

And it seems that <local:ExploreTemplateSelector> is not even recognized in my xaml. Any ideas?

谢谢!

推荐答案

您不能在DataTemplate的内部 中使用DataTemplateSelector.除了设置ListView的ItemTemplate之外,您还可以设置其ItemTemplateSelector属性,最好通过StaticResource表达式进行设置:

You can't use a DataTemplateSelector inside a DataTemplate. Instead of setting the ListView's ItemTemplate you would set its ItemTemplateSelector property, preferrably by a StaticResource expression:

<Page.Resources>
    <local:ExploreTemplateSelector x:Key="ExploreTemplateSelector">
        ...
    </<local:ExploreTemplateSelector>
</Page.Resources>
...
<ListView ... ItemTemplateSelector="{StaticResource ExploreTemplateSelector}">
    ...
</ListView>

这篇关于WP 8.1:DataTemplateSelector无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆