在 UWP 中使用绑定和 DataContext 不起作用
Using binding and DataContext in UWP not working
我正在创建一个移动 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 中的所有七个项目,对吗?对我来说,视图只显示测试文本 "Select your sportclub".
我在输出中收到以下错误消息 window:
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;
}
}
如错误所述,在您的视图模型中找不到具有此名称的 属性。 Binding
适用于 public
属性,不适用于字段。
在您的屏幕截图中,您的视图模型中有 ListOfSportclubs
个成员,但由于错误它是字段。
您需要在视图模型中创建一些 public
属性 并创建对此 属性 的绑定。
public List<Sportclub> ListOfSportclubs { get; set; } = new List<Sportclub>();
将 "UniversalData" class 更改为以下内容修复了问题:
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;
}
}
是的,您没看错:我需要做的就是向其中添加“{get;set;}”。谢谢大家的回复!
我正在创建一个移动 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 中的所有七个项目,对吗?对我来说,视图只显示测试文本 "Select your sportclub".
我在输出中收到以下错误消息 window:
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;
}
}
如错误所述,在您的视图模型中找不到具有此名称的 属性。 Binding
适用于 public
属性,不适用于字段。
在您的屏幕截图中,您的视图模型中有 ListOfSportclubs
个成员,但由于错误它是字段。
您需要在视图模型中创建一些 public
属性 并创建对此 属性 的绑定。
public List<Sportclub> ListOfSportclubs { get; set; } = new List<Sportclub>();
将 "UniversalData" class 更改为以下内容修复了问题:
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;
}
}
是的,您没看错:我需要做的就是向其中添加“{get;set;}”。谢谢大家的回复!