【发布时间】:2017-03-06 12:37:55
【问题描述】:
我正在创建一个移动 Windows 应用。我在 UWP 中有一个视图,其中 xaml.cs 文件以以下代码开头:
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using EnergyBattle.Helpers;
namespace EnergyBattle.Views.Tutorial
{
public sealed partial class TutorialPage1 : Page
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
UniversalData VM = UniversalData.Instance;
public TutorialPage1()
{
this.InitializeComponent();
DataContext = VM;
}
对应的视图有如下标记代码:
<Page
x:Class="EnergyBattle.Views.Tutorial.TutorialPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:EnergyBattle.Views.Tutorial"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<TextBlock>Select your sportclub</TextBlock>
<ListView ItemsSource="{Binding ListOfSportclubs}" IsItemClickEnabled="True" ItemClick="listItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"><Run Text="{Binding name}" /></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</Page>
我正在尝试使用绑定(如“”中所示)来显示我保存在模型中的列表中的项目。 DataContext 中包含正确的数据,您可以在此断点处看到:https://i.stack.imgur.com/8TFVi.png - 请注意,此时模拟器仍在其启动屏幕上,因此尚未加载视图。
当然,ListView 应该填充 DataContext 中的所有七个项目,对吗?对我来说,该视图只显示测试文本“选择您的运动俱乐部”。
我在输出窗口中收到以下错误消息:
Error: BindingExpression path error: 'ListOfSportclubs' property not found on 'EnergyBattle.Helpers.UniversalData'. BindingExpression: Path='ListOfSportclubs' DataItem='EnergyBattle.Helpers.UniversalData'; target element is 'Windows.UI.Xaml.Controls.ListView' (Name='null'); target property is 'ItemsSource' (type 'Object')
我做错了什么?非常相似的代码在我见过和使用过的其他程序中运行良好。
编辑:人们想了解更多关于 ListOfSportclubs 的信息。这里定义:
class UniversalData
{
public static UniversalData Instance { get; } = new UniversalData();
private List<Sportclub> ListOfSportclubs = new List<Sportclub>();
private List<Stoplicht> stoplichtList = new List<Stoplicht>();
public List<Sportclub> getSportclubList() { return ListOfSportclubs; }
public List<Stoplicht> getStoplichtList() { return stoplichtList; }
public void setSportclubList(List<Sportclub> sportclubList)
{
this.ListOfSportclubs = sportclubList;
}
public void setStoplichtList(List<Stoplicht> stoplichtList)
{
this.stoplichtList = stoplichtList;
}
}
【问题讨论】:
-
是
ListOfSportclubs财产还是归档?显示为声明它的类。 -
我用这些信息编辑了我的帖子。
-
您的
ListOfSportclubs必须是具有合适吸气剂的公共财产。 -
公开 ListOfSportclubs 没有任何改变。我当前的 getSportclubList() 是否存在缺陷?
标签: c# data-binding uwp datacontext