gpt4 book ai didi

c# - Windows 通用项目不支持数据类型 "AnyName"(x :DataType for DataTemplate)

转载 作者:行者123 更新时间:2023-11-30 23:30:42 24 4
gpt4 key购买 nike

努力做到简单example在 Windows 通用项目中使用主细节。但是 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 中使用 App1.ViewModels using viewmodel: 中的类。否则,解析器甚至不知道这些类的存在。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com