buttons are lost after redirection of the page using ObservableCollection in windows phone 8 c#

In my windows phone application I am creating dynamic buttons using ObservableCollection into a btn_groupname_Click event like below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.UserData;
using System.Windows.Media;
using Microsoft.Phone.Maps.Controls;
using System.Collections.ObjectModel;
using System.Collections;

namespace GetContacts
{
    public partial class createGroups : PhoneApplicationPage
    {
        string buttonName = "";
        public ObservableCollection<Group> groupbtn;
        List<CustomContact> contact = new List<CustomContact>();
        List<CustomContact> listOfContact2 = new List<CustomContact>();
        //List<Group> buttons = new List<Group>(); 
        List<Button> buttons = new List<Button>();
        List<List<CustomContact>> lls = new List<List<CustomContact>>();
        public createGroups()
        {
            InitializeComponent();
            groupbtn = new ObservableCollection<Group>();
        }

        private void btn_groupname_Click(object sender, RoutedEventArgs e)
        {
            if (tb_groupname.Text != string.Empty)
            {
                groupbtn.Add(new Group { Name = tb_groupname.Text });
                buttonName = tb_groupname.Text;
                lb_groupofcontacts.DataContext = groupbtn;
                tb_groupname.Text = string.Empty;
            }
        }      
    }
}

And below is my Group class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GetContacts.Resources;
using Microsoft.Phone.UserData;

namespace GetContacts
{
    public class Group
    {
        public string Name { get; set; }
        public Group()
        { 

        }
        public Group(Button btn)
        {
            Name = btn.Name;

        }
    }
}

Its working fine and create multiple buttons when I am working on the current page but when I go to the next page and after come back to the previous page the buttons are lost, How can I get these buttons after redirection of the page. Kindly suggest me. Waiting for your reply. Thanks.

Answers


If you want to maintain state of the collection through out your app You can take collection as Static data Member,

        public createGroups()
        {
            InitializeComponent();
            if(groupbtn!== null)
            groupbtn = new ObservableCollection<Group>();
        }
public static ObservableCollection<Group> groupbtn;

Need Your Help

Detect if app is in foreground and focus changing to app

focus clipboard winjs

I'm am writing a WinJS app that deals with data on the clipboard. I see you can use

Two escape characters in a SQL query

sql-server syntax count escaping special-characters

So I have a bunch of strings in records that include the '/' character, eg: