方法结束后窗口关闭

时间:2019-01-11 13:08:03

标签: c# wpf visual-studio

我正在为正在工作的公司在Visual Studio 2017中进行WPF项目工作,但遇到一个我根本无法理解的错误。

这是问题所在: 该应用程序用于为我们的工作人员更改和检索有关储物柜的信息(许多人来来往往,所以每天都有很多更改) 因此,该应用程序将打开一个MainWindow(显然),其中包含所有现有储物柜的列表(DataGrid)以及有关它们的一些最重要的信息。

要更改有关储物柜的任何内容(清空,更改状态等),您可以双击一个条目以打开另一个窗口,其中包含有关所选储物柜的所有详细信息。到目前为止一切顺利!

当我尝试保存细节并关闭窗口时会出现问题,因为它没有到达我的SaveData方法。在保存之前,它需要经过几种方法,但似乎只是在特定方法(addHistory)结束之后才结束。甚至都没有保存到数据。

代码:

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();
    }

0 个答案:

没有答案