简体   繁体   English

C#声明静态列表

[英]C# Declare a Static List

I am using a Static List to store some values, the idea is declare the list, then a button in page 1 adds some values, then a button in page 2 adds some more values, to do this I am triying declare it in each page but the list is newly created: 我正在使用静态列表存储一些值,这个想法是声明列表,然后第1页上的按钮添加一些值,然后第2页上的按钮添加更多值,为此,我尝试在每页中声明它但是列表是新创建的:

public static List _songlist = new List(); 公共静态列表_songlist = new List(); in each page. 在每个页面中。

Can you help me?, how I can declare the static list then have the same list with the values in all the pages? 您能帮我吗?如何声明静态列表,然后在所有页面中使用包含相同值的列表?

public static List<Song> _songlist = new List<Song>();

public class Song
{
private string _name;
public string name
{
get { return _name; }
set { SetProperty(ref _name, value); }
}
private string _artist;
public string artist
{
get { return _artist; }
set { SetProperty(ref _artist, value); }
}
}

Any help is appreciated. 任何帮助表示赞赏。

If I understand you, you want an Application wide static list. 据我了解,您需要一个应用程序范围的静态列表。

Why not create a new file called ApplicationServices.cs and put a static class inside of it with a static List<Song> 为什么不创建一个名为ApplicationServices.cs的新文件,并使用静态List<Song>在其中放置一个静态类。

public static class ApplicationServices{
      public static List<Song> Songs {get; set;}
}

This way you can access it everywhere by calling ApplicationServices.Songs 这样,您可以通过调用ApplicationServices.Songs在任何地方访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM