我正在创建一个移动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>
我正在尝试使用绑定(如&#34;&#34;中所示)来显示我已保存在模型中的列表中的项目。 DataContext中包含正确的数据,正如您在此断点处所看到的那样:https://i.stack.imgur.com/8TFVi.png - 请注意,此时,模拟器仍在其引导屏幕上,因此视图尚未加载。< / p>
当然,ListView应该在DataContext中填充七个项目,对吗?对我来说,除了测试文本之外,其他视图都没有显示,&#34;选择你的运动俱乐部&#34;。
我在输出窗口中收到以下错误消息:
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;
}
}
答案 0 :(得分:0)
由于错误表示在视图模型中找不到具有此名称的属性。 Binding
适用于public
属性,而不适用于字段。
在您的屏幕截图中,您的视图模型中有ListOfSportclubs
成员,但由于错误,它是字段。
您需要在视图模型中创建一些public
属性,并创建与此属性的绑定。
public List<Sportclub> ListOfSportclubs { get; set; } = new List<Sportclub>();
答案 1 :(得分:0)
更改&#34; UniversalData&#34;以下类解决了以下问题:
class UniversalData
{
public static UniversalData Instance { get; } = new UniversalData();
public List<Sportclub> ListOfSportclubs {get; set;} = 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;
}
}
是的,你看错了:我需要做的就是添加&#34; {get;设置;}&#34;它。谢谢大家的回复!