尝试做简单的example在Windows Universal项目中使用主细节。但是VS显示错误:DataType "Any Name" is not supported in a Windows Universal project。或No Data Type defined for Data Template(如果没有数据类型)。在stackoverflow或其他地方找不到答案。你能帮助我吗?

码:

MasterDetailPage.xaml

<Page
x:Class="App1.Pages.MasterDetailPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
NavigationCacheMode="Enabled"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Page.Transitions>
    <TransitionCollection>
        <NavigationThemeTransition />
    </TransitionCollection>
</Page.Transitions>

<Page.Resources>
    <DataTemplate x:Key="MasterListViewItemTemplate" x:DataType="viewmodels:ItemViewModel">
        <Grid Margin="0,11,0,13">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <TextBlock Text="{x:Bind Title}" Style="{ThemeResource BaseTextBlockStyle}" />

            <TextBlock
                Text="{x:Bind Text}"
                Grid.Row="1"
                MaxLines="1"
                Style="{ThemeResource ListBodyTextBlockStyle}" />

            <TextBlock
                Text="{x:Bind DateCreatedHourMinute}"
                Grid.Column="1"
                Margin="12,1,0,0"
                Style="{ThemeResource ListCaptionAltTextblockStyle}" />
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="DetailContentTemplate" x:DataType="viewmodels:ItemViewModel">
        <StackPanel>
            <TextBlock
                Margin="0,8"
                Style="{ThemeResource TitleTextBlockStyle}"
                HorizontalAlignment="Left"
                Text="{x:Bind Title}"/>

            <TextBlock
                Margin="0,9"
                HorizontalAlignment="Left"
                MaxWidth="560"
                Style="{ThemeResource BodyTextBlockStyle}"
                Text="{x:Bind Text}" />
        </StackPanel>
    </DataTemplate>
</Page.Resources>

<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="AdaptiveStates" CurrentStateChanged="AdaptiveStates_CurrentStateChanged">
            <VisualState x:Name="DefaultState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="720" />
                </VisualState.StateTriggers>
            </VisualState>

            <VisualState x:Name="NarrowState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>

                <VisualState.Setters>
                    <Setter Target="MasterColumn.Width" Value="*" />
                    <Setter Target="DetailColumn.Width" Value="0" />
                    <Setter Target="MasterListView.SelectionMode" Value="None" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="MasterColumn" Width="320" />
        <ColumnDefinition x:Name="DetailColumn" Width="*" />
    </Grid.ColumnDefinitions>

    <TextBlock
        Text="My Items"
        Margin="12,8,8,8"
        Style="{ThemeResource TitleTextBlockStyle}" />

    <ListView
        x:Name="MasterListView"
        Grid.Row="1"
        ItemContainerTransitions="{x:Null}"
        ItemTemplate="{StaticResource MasterListViewItemTemplate}"
        IsItemClickEnabled="True"
        ItemClick="MasterListView_ItemClick">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

    <ContentPresenter
        x:Name="DetailContentPresenter"
        Grid.Column="1"
        Grid.RowSpan="2"
        BorderThickness="1,0,0,0"
        Padding="24,0"
        BorderBrush="{ThemeResource SystemControlForegroundBaseLowBrush}"
        Content="{x:Bind MasterListView.SelectedItem, Mode=OneWay}"
        ContentTemplate="{StaticResource DetailContentTemplate}">
        <ContentPresenter.ContentTransitions>
            <!-- Empty by default. See MasterListView_ItemClick -->
            <TransitionCollection />
        </ContentPresenter.ContentTransitions>
    </ContentPresenter>
</Grid>




ItemViewModel

using App1.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Globalization.DateTimeFormatting;

namespace App1.ViewModels
{
    public class ItemViewModel
    {
        private int _itemId;

        public int ItemId
        {
            get
            {
                return _itemId;
            }
        }

        public string DateCreatedHourMinute
        {
            get
            {
                var formatter = new DateTimeFormatter ("hour minute");
                return formatter.Format (DateCreated);
            }
        }

        public string Title
        {
            get; set;
        }
        public string Text
        {
            get; set;
        }
        public DateTime DateCreated
        {
            get; set;
        }

        public ItemViewModel ()
        {
        }

        public static ItemViewModel FromItem (Item item)
        {
            var viewModel = new ItemViewModel ();

            viewModel._itemId = item.Id;
            viewModel.DateCreated = item.DateCreated;
            viewModel.Title = item.Title;
            viewModel.Text = item.Text;

            return viewModel;
        }
    }
}


需要更多此代码?

最佳答案

您在Xaml中缺少ViewModels命名空间的命名空间声明。

在顶部添加:

xmlns:viewmodels="using:App1.ViewModels"


想象一下,就像Xml的using语句一样。设置完之后,您可以在Xml中使用viewmodel:使用App1.ViewModels中的类。没有这些,解析器甚至都不知道这些类的存在。

关于c# - Windows Universal项目不支持DataType“AnyName”(DataTemplate的x:DataType),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34878302/

10-12 00:11