C# 由于Windows Phone 8.1中的保护级别错误,无法访问Listbox
我试图从另一个表单向表单中的列表框添加一些文本,但代码中有红色下划线,应该添加到列表框中。难道我不可能从另一个类向一个类中的表单传递数据吗?我正在使用VisualStudio 2013 我在MainForm类中使用了以下代码:C# 由于Windows Phone 8.1中的保护级别错误,无法访问Listbox,c#,windows-phone-8.1,C#,Windows Phone 8.1,我试图从另一个表单向表单中的列表框添加一些文本,但代码中有红色下划线,应该添加到列表框中。难道我不可能从另一个类向一个类中的表单传递数据吗?我正在使用VisualStudio 2013 我在MainForm类中使用了以下代码: // Local object of SearchResultForm SearchResultForm frmSearchResult = new SearchResultForm(); frmSearchResult.Show(); f
// Local object of SearchResultForm
SearchResultForm frmSearchResult = new SearchResultForm();
frmSearchResult.Show();
frmSearchResult.listBox.IsEnabled == false;
错误消息:由于其受保护级别而无法访问看起来您的
列表框
是搜索结果表单
的私有成员;这意味着它不能从SearchResultForm
类外部访问。您可以使列表框
变量
公开或内部
,但更好的方法是在SearchResultForm
类中创建一个mutator方法,如下所示:
public void AddItemToListBox(string text)
{
listBox.Items.Add(text);
}
然后从你的主要形式你会做:
// Local object of SearchResultForm
SearchResultForm frmSearchResult = new SearchResultForm();
frmSearchResult.Show();
frmSearchResult.AddItemToListBox("test");
通过这种方式,您可以更新列表框,而无需公开您的
SearchResultForm
类实现详细信息。在您的类SearchResultForm类中添加一个参数此处我的类是showvideolbx
public class showvideolbx
{
public ListBox SVListBox { get; set; }
}
将列表框Id分配给类参数
showvideolbx lbbox = new showvideolbx();
lbbox.SVListBox = lbxSongsList;
然后调用Create类,然后调用ListBox参数
showvideolbx svlbox;
if (svlbox.SVListBox.IsEnabled == false)
svlbox.SVListBox.IsEnabled = true;
它可以用,请再试一次 您是否在SearchResultForm中手动创建了listbox?是的,我在SearchResultForm中手动创建了listbox,以便将您自己的类更改为
public
。你说的“形式”是什么意思?只要我使用WP8,我就没有任何表格。你所看到的被称为“页面”。你在使用Xamarin表单吗?不,我在使用XAML来设计Hello Dan Dinu谢谢你的重播,请再次查看问题它是经过编辑的回答很复杂谢谢Chinni你真的不想为了传递引用而引入另一个类。是的,它“会起作用”,但这是一个非常糟糕且不必要的复杂解决方案。它会简单得多,如果已在frmSearchResult:public ListBox formList{get{return ListBox;}}}
中将ListBox作为属性公开。