尝试在Windows通用项目中使用主要细节进行简单example。但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;
}
}
}
需要更多此代码吗?
答案 0 :(得分:2)
您在Xaml中缺少ViewModels命名空间的命名空间声明。
在顶部添加:
xmlns:viewmodels="using:App1.ViewModels"
想象一下它就像是Xml的using语句。设置完成后,可以使用viewmodel:在Xml中使用App1.ViewModels中的类。没有它,解析器甚至不知道这些类存在。