我正在尝试使用单选按钮和委托进行过滤。基本上我想要一个硬编码列表,它就像一个伪数据库,然后只显示列表中的一些项目,具体取决于按下哪个按钮。
我有一个Apartment.cs
类,它有一个Name和ID属性,没有别的。
我的另一个课程是Apts.cs
:
public class Apts
{
private ObservableCollection<Apartment> apartments;
public ObservableCollection<Apartment> Apartments { get; set; }
public Apts()
{
apartments = new ObservableCollection<Apartment>()
{
new Apartment() {Name = "Name1", ID = 1},
new Apartment() {Name = "Name2", ID = 2},
new Apartment() {Name = "Name3", ID = 3},
};
}
}
我正在尝试访问应该是列表的公寓属性(我错了吗?)并将其作为参数传递给我写的ListParis(ObservableCollection<Apartment>)
方法。此代码位于MainPage.xaml.cs
public void AssignDelegates()
{
if (parisButton.IsPressed)
{
myFunc = DelActions.ListParis(Apartments);
}
}
但我收到错误说:
CS0103 The name 'Apartments' does not exist in the current context
为什么我无法访问该列表?
答案 0 :(得分:0)
在代码的第一部分,您正在填写公寓&#34;私人公寓,而公共公司仍然是空的。然后在第二部分代码中你应该初始化类#34; Apt&#34;为了进入公共财产公寓。它应该是这样的:
myFunc = DelActions.ListParis(new Apts().Apartments);
答案 1 :(得分:0)
首先,您应该学习如何使用属性。 Using Properties
你必须在你的财产吸气器内返回你的领域,这样你才能实现你想要的目标。
public class Apts
{
private ObservableCollection<Apartment> apartments;
public ObservableCollection<Apartment> Apartments
{
get
{
return apartments;
}
set
{
apartments = value;
}
}
public Apts()
{
apartments = new ObservableCollection<Apartment>()
{
new Apartment() {Name = "Name1", ID = 1},
new Apartment() {Name = "Name2", ID = 2},
new Apartment() {Name = "Name3", ID = 3},
};
}
}
第二,你必须初始化你的&#34; Apt&#34;要将apartmets作为参数传递的类,如下所示:
public void AssignDelegates()
{
if (parisButton.IsPressed)
{
Apts apartments = new Apts();
myFunc = DelActions.ListParis(apartments.Apartments);
}
}