【问题标题】:Cannot resolve DataType XAML C#无法解析 DataType XAML C#
【发布时间】:2016-03-31 14:26:55
【问题描述】:

我正在编写一个示例项目,但我的代码如下所示。

...
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 是什么

我希望有人可以帮助我解决这个问题。谢谢。

【问题讨论】:

    标签: c# xaml datatemplate xbind


    【解决方案1】:

    这是 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- 运行应用程序。

    【讨论】:

      【解决方案2】:

      假设您的命名空间正确,正确的导入方式是:

      xmlns:data="clr-namespace:OrnekUygulama.Model"
      

      【讨论】:

      • 不幸的是,这给了我更多的错误。但是我创建了一个用户控件并将它们全部放入其中。我想它解决了。
      猜你喜欢
      • 1970-01-01
      • 2022-10-21
      • 2013-08-06
      • 2016-01-13
      • 1970-01-01
      • 2020-03-16
      • 2021-11-22
      • 1970-01-01
      • 2021-01-08
      相关资源
      最近更新 更多