c# - 无法解析数据类型 XAML C#

标签 c# xaml datatemplate xbind

我正在编写一个示例项目,但我的代码在下面出现了问题。

...
xmlns:data="using:OrnekUygulama.Model"
...
<GridView Name="NewsArea"
                      Background="LightGray"
                      ItemsSource="{x:Bind NewsCollection}"
                      HorizontalAlignment="Stretch"
                      Margin="10,0,0,0">
                <GridView.ItemTemplate>
                    <DataTemplate x:DataType="data:NewsCollection">
                        <Grid Background="White" Margin="10" Height="275" Width="200">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Image Name="NewsImages" Source="{x:Bind Image}" />
                            <RelativePanel Grid.Row="1">
                                <TextBlock Text="{x:Bind Headline}" />
                                <TextBlock Text="{x:Bind Subhead}" />
                                <TextBlock Text="{x:Bind DateLine}" />
                            </RelativePanel>
                        </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>

这是我的 mainpage.xaml,我有错误 x:DataType="data:NewsCollection"说 Cannot Resolve DataType data:NewsCollection 但我这里有:

public class NewsCollection
{
    public int ID { get; set; }
    public string Category { get; set; }
    public string Headline { get; set; }
    public string Subhead { get; set; }
    public string DateLine { get; set; }
    public string Image { get; set; }
}

哪个是 NewsCollection.cs

希望有人能帮我解决这个问题。谢谢。

最佳答案

这是 Visual Studio 2015 中的一个错误,解决它只需注释该部分代码并运行它。之后取消注释,它将运行而不会出现任何错误。

1- 注释这部分代码:

                <!--<GridView.ItemTemplate>
                    <DataTemplate x:DataType="data:NewsCollection">
                        <Grid Background="White" Margin="10" Height="275" Width="200">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Image Name="NewsImages" Source="{x:Bind Image}" />
                            <RelativePanel Grid.Row="1">
                                <TextBlock Text="{x:Bind Headline}" />
                                <TextBlock Text="{x:Bind Subhead}" />
                                <TextBlock Text="{x:Bind DateLine}" />
                            </RelativePanel>
                        </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>-->

2- 运行您的应用。

3-取消注释这部分代码:

                <GridView.ItemTemplate>
                    <DataTemplate x:DataType="data:NewsCollection">
                        <Grid Background="White" Margin="10" Height="275" Width="200">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Image Name="NewsImages" Source="{x:Bind Image}" />
                            <RelativePanel Grid.Row="1">
                                <TextBlock Text="{x:Bind Headline}" />
                                <TextBlock Text="{x:Bind Subhead}" />
                                <TextBlock Text="{x:Bind DateLine}" />
                            </RelativePanel>
                        </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>

4- 运行应用。

关于c# - 无法解析数据类型 XAML C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36336250/

相关文章:

c# - C++ 新手,想在 MFC 应用程序中创建后台线程

wpf - 如何在对项目进行分组的 WPF ItemsControl 上使用 UI 自动化?

c# - 绑定(bind)不适用于数据模板中的数据模板

wpf - 如何在 Mahapps.MetroWindow 中更改窗口标题的 FontWeigth

wpf - 水平显示 ItemsControl 内的项目集合

c# - 在 C# 中构建数据模板

c# - 如何从窗体上的用户控件获取数据

c# - 在配置中指定条件跟踪监听器

c# - var 与具体类型用法

c# - .Net Maui - 如何返回根页面