由于 Windows Phone 8.1 中的保护级别错误,列表框无法访问
Listbox is inaccessible due to its protection level Error in Windows Phone 8.1
我试图从另一个表单向表单中的列表框添加一些文本,但我在应该添加到列表框的代码中出现红色下划线。难道不能将数据从另一个 class 传递到 class 中的表单吗?我正在使用 VisualStudio 2013
我在 MainForm 中使用了这段代码 class:
// Local object of SearchResultForm
SearchResultForm frmSearchResult = new SearchResultForm();
frmSearchResult.Show();
frmSearchResult.listBox.IsEnabled == false;
错误消息:由于其受保护级别而无法访问
看起来你的 listBox
是 SearchResultForm
的 private
成员;这意味着无法从 SearchResultForm
class 外部访问它。您可以使 listBox
变量 public
或 internal
,但更好的方法是在 SearchResultForm
class 中创建一个 mutator 方法,例如:
public void AddItemToListBox(string text)
{
listBox.Items.Add(text);
}
然后从您的主表单中您将执行:
// Local object of SearchResultForm
SearchResultForm frmSearchResult = new SearchResultForm();
frmSearchResult.Show();
frmSearchResult.AddItemToListBox("test");
通过这种方式,您可以在不公开 SearchResultForm
class 实施细节的情况下更新列表框。
向您的 class SearchResultForm class 添加一个参数,我的 class 是 showvideolbx
public class showvideolbx
{
public ListBox SVListBox { get; set; }
}
将列表框 ID 分配给 Class 参数
showvideolbx lbbox = new showvideolbx();
lbbox.SVListBox = lbxSongsList;
然后现在你调用Create class 然后调用ListBox Parameter as
showvideolbx svlbox;
if (svlbox.SVListBox.IsEnabled == false)
svlbox.SVListBox.IsEnabled = true;
它会起作用,请尝试一次。
我试图从另一个表单向表单中的列表框添加一些文本,但我在应该添加到列表框的代码中出现红色下划线。难道不能将数据从另一个 class 传递到 class 中的表单吗?我正在使用 VisualStudio 2013
我在 MainForm 中使用了这段代码 class:
// Local object of SearchResultForm
SearchResultForm frmSearchResult = new SearchResultForm();
frmSearchResult.Show();
frmSearchResult.listBox.IsEnabled == false;
错误消息:由于其受保护级别而无法访问
看起来你的 listBox
是 SearchResultForm
的 private
成员;这意味着无法从 SearchResultForm
class 外部访问它。您可以使 listBox
变量 public
或 internal
,但更好的方法是在 SearchResultForm
class 中创建一个 mutator 方法,例如:
public void AddItemToListBox(string text)
{
listBox.Items.Add(text);
}
然后从您的主表单中您将执行:
// Local object of SearchResultForm
SearchResultForm frmSearchResult = new SearchResultForm();
frmSearchResult.Show();
frmSearchResult.AddItemToListBox("test");
通过这种方式,您可以在不公开 SearchResultForm
class 实施细节的情况下更新列表框。
向您的 class SearchResultForm class 添加一个参数,我的 class 是 showvideolbx
public class showvideolbx
{
public ListBox SVListBox { get; set; }
}
将列表框 ID 分配给 Class 参数
showvideolbx lbbox = new showvideolbx();
lbbox.SVListBox = lbxSongsList;
然后现在你调用Create class 然后调用ListBox Parameter as
showvideolbx svlbox;
if (svlbox.SVListBox.IsEnabled == false)
svlbox.SVListBox.IsEnabled = true;
它会起作用,请尝试一次。