这是我第一次这样做。 内部模型: Menu.cs
public class Menu
{
private string _iconThumb;
public string IconThumb
{
get
{
return _iconThumb;
}
set
{
if(value!=_iconThumb)
{
_iconThumb = value;
}
}
}
}
和MainModels.cs
public class MainModels
{
public ObservableCollection<Menu> Menu { get; private set; }
/// <summary>
/// Loading all of information menus to Listbox.
/// </summary>
/// <returns></returns>
///
public ObservableCollection<Menu> MenuLoad()
{
Menu = new ObservableCollection<Menu>();
{
new Menu() { IconThumb = "/Photos/align_justify-50.png" };
new Menu() { IconThumb = "/Photos/music_video-50.png" };
new Menu() { IconThumb = "/Photos/christmas_star-50.png" };
new Menu() { IconThumb = "/Photos/user-50.png" };
}
return Menu;
}
MainPages.cs
Models.MainModels Main = new Models.MainModels();
public MainPage()
{
this.InitializeComponent();
loaddata();
Menu();
}
//Loading menu in main
private void Menu()
{
lstMenu.ItemsSource = Main.Menu;
}
错误:
非可调用成员&#34; .... Models.MainModels.Menu&#34; canot可以像方法一样使用。
我不知道解决这个问题,因为我第一次这样做。
答案 0 :(得分:0)
Menu
是MainModels
类的属性,而不是方法。因此,请删除方法调用的括号:lstMenu.ItemsSource = Main.Menu;
。
请考虑使用MainModels
类的以下实现:
public class MainModels
{
public MainModels()
{
Menu = new ObservableCollection<Menu>(GetMenuItems());
}
public ObservableCollection<Menu> Menu { get; private set; }
/// <summary>
/// Factory method to return all "known" menu items.
/// </summary>
///
private static IEnumerable<Menu> GetMenuItems()
{
var items = new[]
{
new Menu() { IconThumb = "/Photos/align_justify-50.png" };
new Menu() { IconThumb = "/Photos/music_video-50.png" };
new Menu() { IconThumb = "/Photos/christmas_star-50.png" };
new Menu() { IconThumb = "/Photos/user-50.png" };
}
.ToList()
.AsReadOnly();
return items;
}
}