在windows phone 8 c#中使用ObservableCollection重定向页面后,按钮会丢失
本文关键字:按钮 重定向 ObservableCollection phone windows | 更新日期: 2023-09-27 18:07:31
在我的windows phone应用程序中,我使用ObservableCollection
创建动态按钮到btn_groupname_Click
事件,如下所示:
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;
}
}
}
}
下面是我的Group
类:
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;
}
}
}
当我在当前页面上工作时,它的工作很好,并创建多个按钮,但当我转到下一页并回到上一页后,按钮丢失了,我如何才能在页面重定向后获得这些按钮。请给我推荐一下。等待您的回复。谢谢。
如果你想在整个应用程序中保持集合的状态,你可以将集合作为Static
数据成员,
public createGroups()
{
InitializeComponent();
if(groupbtn!== null)
groupbtn = new ObservableCollection<Group>();
}
public static ObservableCollection<Group> groupbtn;