c# - 方法结束后窗口关闭
问题描述
我正在为我工作的公司在 Visual Studio 2017 中开发一个 WPF 项目,我遇到了一个我无法理解的错误......
这就是问题所在:该应用程序用于更改和检索有关我们工人的储物柜的信息(很多人来来去去,所以每天都有很多变化)所以应用程序打开时带有一个包含列表(DataGrid)的 MainWindow(显然)所有现有储物柜以及一些关于它们的最重要信息。
要更改有关储物柜的任何内容(清空它,更改状态等),您可以双击一个条目以打开另一个窗口,其中包含有关所选储物柜的所有详细信息。到目前为止,一切都很好!
当我尝试保存详细信息并关闭窗口时出现问题,因为它没有到达我的 SaveData 方法。它需要在保存之前通过一些方法,但它似乎只是在特定方法(addHistory)结束后才结束。甚至无需访问 SaveData。
代码:
public void AddHistory(Locker l) //[2.1.2.0]
{
Debug("AddHistory() Start [2.1.2.0]");
History h = new History();
h.HistID = hID;
hID++;
h.LockerNum = l.Nr;
h.Vis = visPop;
h.LockerID = l.ID;
h.TnFrstNameHist = l.TnFrstName;
h.TnNameHist = l.TnName;
h.FullName = l.TnName + " " + l.TnFrstName;
h.Date = DateTime.Now.ToShortDateString();
h.Time = DateTime.Now.ToLongTimeString();
Debug("Before GetAction() [2.1.2.0]");
h.ActionTaken = GetAction();
if (h.ActionTaken == "")
{
h.ActionTaken = "Keine";
}
for (int i = 0; i < allLoc.Count(); i++)
{
if (l.ID == allLoc[i].ID)
{
allLoc[i].LocHist.Add(h);
historyList.Add(h);
if (allLoc[i].LocHist[0].ActionTaken == "Keine" || allLoc[i].LocHist[0].ActionTaken == "none")
{
allLoc[i].LocHist.RemoveAt(0);
}
changedHist = allLoc[i].LocHist;
DataGridHist.Items.Clear();
foreach (History his in changedHist)
{
DataGridHist.Items.Add(his);
}
break;
}
}
(Application.Current.MainWindow as MainWindow).SaveData();
Debug("AddHistory() End [2.1.2.0]");
}
public string GetAction() //[2.1.2.1]
{
Debug("GetAction Start [2.1.2.1]");
string histOut = "";
//loc.Nr throws up a reference exception!
if (checkLoc.Nr != locker.Nr)
{
histOut = "GarderobenNummer Geändert";
}
if (checkLoc.SecKey != locker.SecKey)
{
if (locker.SecKey == true)
{
histOut = "Ersatzschlüssel Herausgegeben";
}
else
{
histOut = "Ersatzschlüssel zurück erhalten";
}
}
if (checkLoc.Status != locker.Status)
{
if (checkLoc.Status == statusArr[3])
{
histOut = "Kontrolliert";
}
else if (checkLoc.Status == statusArr[4])
{
histOut = "Schloss Ausgetauscht";
}
else if (checkLoc.Status == statusArr[2])
{
histOut = "Vorbereitet für neue Teilnehmer";
}
}
Debug("GetAction() End [2.1.2.1]");
return histOut;
}
private void Save_Button_Click(object sender, RoutedEventArgs e) //[2.2.0.1]
{
if (IsLockerChanged())
{
//Make Pop-up with field to enter a visum.
NamePopUp visPop = new NamePopUp();
visPop.ShowDialog();
//Save the data and close the window.
if (visCheck == true)
{
Debug("Before AddHistory() [2.2.0.1]");
AddHistory(locker);
Debug("After AddHistory() [2.2.0.1]");
foreach(Locker l in mw.lockerList)
{
if(l.ID == locker.ID)
{
Debug("Locker Found in Loop [2.2.0.1]");
l.Nr = locker.Nr;
l.TnName = locker.TnName;
l.TnFrstName = locker.TnFrstName;
l.TnWorkingGroup = locker.TnWorkingGroup;
l.Status = locker.Status;
l.SecKey = locker.SecKey;
l.LocHist = locker.LocHist;
Debug("Locker in loop copied [2.2.0.1]");
break;
}
}
Debug("Before SaveData() [2.2.0.1]");
mw.SaveData();
Debug("After SaveData() [2.2.0.1]");
checkLoc.Nr = locker.Nr;
checkLoc.TnName = locker.TnName;
checkLoc.TnFrstName = locker.TnFrstName;
checkLoc.TnWorkingGroup = locker.TnWorkingGroup;
checkLoc.Status = locker.Status;
checkLoc.SecKey = locker.SecKey;
checkLoc.LocHist = locker.LocHist;
visCheck = false;
}
}
else
{
Msg("Es wurden keine Änderungen gefunden.");
}
}
Debug 是一种在调试时输出信息的方法,它基本上只是一个 MessageBox.Show()。
当我尝试测试它时得到的输出是:
Before AddHistory() [2.2.0.1]
AddHistory() Start [2.1.2.0]
Before GetAction() [2.1.2.0]
GetAction Start [2.1.2.1]
GetAction() End [2.1.2.1]
AddHistory() End [2.1.2.0]
之后,窗口就关闭了......
我已经尝试清理代码并重建它,但结果保持不变。
有谁知道什么可能导致这样的问题和/或我如何解决它?
如果您需要更多信息,请询问!
提前致谢!
根据要求,这是 SaveData() 方法的代码:
public void SaveData(int id = -1)
{
if(isDebug && id != -1)
{
string output = "";
foreach(Locker l in lockerList)
{
if(l.ID == id)
{
output += "ID: " + l.ID + "\r\n";
output += "Nr: " + l.Nr + "\r\n";
output += "Room: " + l.Room + "\r\n";
output += "RoomName: " + l.RoomName + "\r\n";
output += "Seckey: " + l.SecKey + "\r\n";
output += "Status: " + l.Status + "\r\n";
output += "FrstName: " + l.TnFrstName + "\r\n";
output += "Name: " + l.TnName + "\r\n";
output += "WorkingGroup: " + l.TnWorkingGroup;
break;
}
}
Debug(output);
}
string fp = saveFile; //The used Filepath
if (!Directory.Exists(savePath)) //Check if the directory in AppData exists
{
//Directory doesn't exist
Directory.CreateDirectory(savePath); //Create the directory
}
//prepate to create or open a file
BinaryFormatter bf = new BinaryFormatter();
FileStream fs;
if (!File.Exists(fp)) //Check if there is a file
{
//No File
fs = File.Create(fp); //Create a File
FillEmptyData();
}
else
{
//File Exists
fs = File.Open(fp, FileMode.Open); //Open the File
}
DefineSaveData();
bf.Serialize(fs, saveData); //save the Locker Data into the file.
fs.Close();
}
解决方案
推荐阅读
- terraform - 变量的继承在 terraform 中不起作用
- android - 如何启动来自图书馆的另一个应用程序的活动?
- c++ - 当 ogl::Arrays 的默认构造函数运行时,Opencv2 崩溃
- c# - Json.net 无法序列化从 Monobehaviour 派生的类
- recaptcha - 从 reCAPTCHA 服务中删除域
- java - Java很长的字符串比较似乎不起作用
- sql - ODBC Excel SQL 条件
- python - 在 docker 容器内为 django 运行 cron 作业时,manage.py 脚本出错
- python - 如何在python中取消setInterval?
- c# - 分割字符串的最快方法?