WP 8.1:DataTemplateSelector不工作(WP 8.1: DataTemplat

2019-10-20 21:39发布

我已经得到了Windows Phone 8.1应用枢纽工程

我试图创建DataTemplate中的一个XAML列表视图选择,但它失败。 我可以运行应用程序,但我得到了断裂: 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 2个误差修改:错误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的认可。 有任何想法吗?

谢谢!

Answer 1:

你不能使用DataTemplateSelector一个DataTemplate 。 相反设置ListView的的ItemTemplate ,你会设置其ItemTemplateSelector由StaticResource的表达特性,preferrably:

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


文章来源: WP 8.1: DataTemplateSelector isn't working