gpt4 book ai didi

c# - 在WPF中从后面的代码中删除代码

转载 作者:行者123 更新时间:2023-12-03 10:24:37 26 4
gpt4 key购买 nike

我正在尝试将项目中的某些代码移到单独的类中。我感兴趣的部分是GetData方法。我想将其从代码中移到一个单独的类DataAccess中。多谢您的协助。谢谢!

MainWindow.xaml.cs

namespace Contact_Interests
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<AggregatedLabel> myAggLabels;

public ObservableCollection<AggregatedLabel> AggLabels
{
get { return myAggLabels; }
}

private ObservableCollection<ContactList> myContactLists;

public IEnumerable<ContactList> ContactLists
{
get { return myContactLists; }
}

public MainWindow()
{
GetData();
this.InitializeComponent();

// Insert code required on object creation below this point.
}

public void GetData()
{
myAggLabels = new ObservableCollection<AggregatedLabel>();
myContactLists = new ObservableCollection<ContactList>();

DB2Connection conn = null;
try
{
conn = new DB2Connection("XXXX;");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message + " " + ex.InnerException);
}

//get all contactLists and their labels
DB2Command command = new DB2Command("SQL SELECT statement");
command.Connection = conn;

conn.Open();

//get all labels from database
using (DB2DataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
AggregatedLabel aggLabel = new AggregatedLabel();

aggLabel.ID = Convert.ToInt32(dr["LABEL_ID"]);
aggLabel.Name = dr["LABEL_NAME"].ToString();

myAggLabels.Add(aggLabel);

}
}
//Add unique contactLists to dictionary
Dictionary<int, ContactList> myContactDictionary = new Dictionary<int, ContactList>();

using (DB2DataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
int id = Convert.ToInt32(dr["CONTACT_LIST_ID"]);

if (!myContactDictionary.ContainsKey(id))
{

ContactList contactList = new ContactList();

contactList.ContactListID = id;
contactList.ContactListName = dr["CONTACT_LIST_NAME"].ToString();
contactList.AggLabels = new ObservableCollection<AggregatedLabel>()
{
new AggregatedLabel()
{
ID = Convert.ToInt32(dr["LABEL_ID"]),
Name = dr["LABEL_NAME"].ToString()
}

};
myContactDictionary.Add(id, contactList);
}
else
{
//populate existing contact lists with remaining labels
ContactList contactList = myContactDictionary[id];

contactList.AggLabels.Add
(
new AggregatedLabel()
{
ID = Convert.ToInt32(dr["LABEL_ID"]),
Name = dr["LABEL_NAME"].ToString()
}
);
}
}
}

//add to observable collection
foreach (KeyValuePair<int, ContactList> contactKeyValue in myContactDictionary)
{
ContactList contactList = contactKeyValue.Value;

myContactLists.Add(contactList);
}

conn.Close();
}
}

最佳答案

您应该为此创建一个类,然后将所有代码移入其中。

然后创建该类的实例,并将其设置为此窗口的DataContext

如果您对机制和动机感兴趣(而不仅仅是退出背后的代码),则可以引用我的series on MVVM或许多很棒的MVVM introductions之一。

例如,上面的代码可能是:

namespace Contact_Interests
{
public partial class MainWindowViewModel // : INotifyPropertyChanged
{
private ObservableCollection<AggregatedLabel> myAggLabels;

public ObservableCollection<AggregatedLabel> AggLabels
{
get { return myAggLabels; }
}

private ObservableCollection<ContactList> myContactLists;

public IEnumerable<ContactList> ContactLists
{
get { return myContactLists; }
}

public MainWindowViewModel()
{
GetData();

// Insert code required on object creation below this point.
}

public void GetData()
{
myAggLabels = new ObservableCollection<AggregatedLabel>();
myContactLists = new ObservableCollection<ContactList>();

DB2Connection conn = null;
try
{
conn = new DB2Connection("XXXX;");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message + " " + ex.InnerException);
}

//get all contactLists and their labels
DB2Command command = new DB2Command("SQL SELECT statement");
command.Connection = conn;

conn.Open();

//get all labels from database
using (DB2DataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
AggregatedLabel aggLabel = new AggregatedLabel();

aggLabel.ID = Convert.ToInt32(dr["LABEL_ID"]);
aggLabel.Name = dr["LABEL_NAME"].ToString();

myAggLabels.Add(aggLabel);

}
}
//Add unique contactLists to dictionary
Dictionary<int, ContactList> myContactDictionary = new Dictionary<int, ContactList>();

using (DB2DataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
int id = Convert.ToInt32(dr["CONTACT_LIST_ID"]);

if (!myContactDictionary.ContainsKey(id))
{

ContactList contactList = new ContactList();

contactList.ContactListID = id;
contactList.ContactListName = dr["CONTACT_LIST_NAME"].ToString();
contactList.AggLabels = new ObservableCollection<AggregatedLabel>()
{
new AggregatedLabel()
{
ID = Convert.ToInt32(dr["LABEL_ID"]),
Name = dr["LABEL_NAME"].ToString()
}

};
myContactDictionary.Add(id, contactList);
}
else
{
//populate existing contact lists with remaining labels
ContactList contactList = myContactDictionary[id];

contactList.AggLabels.Add
(
new AggregatedLabel()
{
ID = Convert.ToInt32(dr["LABEL_ID"]),
Name = dr["LABEL_NAME"].ToString()
}
);
}
}
}

//add to observable collection
foreach (KeyValuePair<int, ContactList> contactKeyValue in myContactDictionary)
{
ContactList contactList = contactKeyValue.Value;

myContactLists.Add(contactList);
}

conn.Close();
}
}
}

然后,您的主窗口变为:
namespace Contact_Interests
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
}

关于c# - 在WPF中从后面的代码中删除代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4807955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com