0

我在 app.xaml 中创建了一个属性(公共静态列表 id {get set})我可以向它添加数据(例如:App.id.Add(user.id))。

这样我就可以从页面中获取添加的数据并将其用于任何导航页面(例如:App.id [3])。

初始化是页面刷新数据回到 App.id[0] 时的问题

4

2 回答 2

0

或者,您可以在此处使用单例模式,这将确保创建列表并且仅存在一个实例。

在您的 App.cs 文件中写入以下内容:

    private static List<string> _id;

    public static List<string> id
    {
        get
        {
            if (_id == null)
                _id = new List<string>();

            return _id;
        }

        set
        {
            _id = value;
        }
    }
于 2013-01-16T11:41:16.037 回答
0

是的,您可以从您应用中的所有页面访问 app.cs 中定义的公共属性。

在你的 App.cs

    public List<String> id = new List<string>();

    // Call this method somewhere so you create some data to use... 
    // eg in your App() contructor
    public void CreateData()
    {
        id.Add("ID1");
        id.Add("ID2");
        id.Add("ID3");
        id.Add("ID4");
        id.Add("ID5");
    }

当您需要使用它时,您可以从例如获取数据。OnNavigateTo 事件处理程序

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    // Sets the data context for the page to have a list of strings
    this.DataContext = ((App)Application.Current).id;

   // Or you can index to the data directly
   var a = ((App)Application.Current).id[2];
}
于 2013-01-15T12:16:31.547 回答