Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将带有一个参数的函数传递给ICommand?_C#_Wpf_Icommand_Relaycommand - Fatal编程技术网

C# 如何将带有一个参数的函数传递给ICommand?

C# 如何将带有一个参数的函数传递给ICommand?,c#,wpf,icommand,relaycommand,C#,Wpf,Icommand,Relaycommand,这是我的命令: public ICommand ConfirmLotSavedCommand { get { return new RelayCommand(ConfirmLotSaved); } } 问题是,在用户单击“确认”按钮后,我有反序列化的数据要存储到数据库中。如果用户没有单击确认,或者批号已经存在,那么我不想将反序列化字符串保存在db中 由于作用域的原因,在ConfirmLotSaved()方法中调用带

这是我的命令:

public ICommand ConfirmLotSavedCommand {
        get
        {
            return new RelayCommand(ConfirmLotSaved);
        }
    }
问题是,在用户单击“确认”按钮后,我有反序列化的数据要存储到数据库中。如果用户没有单击确认,或者批号已经存在,那么我不想将反序列化字符串保存在db中

由于作用域的原因,在ConfirmLotSaved()方法中调用带有一个参数的函数时遇到问题

因此,我创建了一个集合,将反序列化的批次设置为一个字段,并将要保存到db的代码放入ConfirmLotSaved()中。但是,由于某种奇怪的原因,该字段为空。。。我不知道为什么

以下是我的尝试:

private LotInformation lot; //field that is supposed to contain all the deserialized info 

private void ConfirmLotSaved()
    {

        using (var db = new DDataContext())
        {
            bool lotNumDbExists = db.LotInformation.Any(r => r.lot_number == DeserialLotNumber);
            if (lotNumDbExists == false)
            {
                successWindow.Message = "Successfully Saved Lot";
                dialogService.ShowDialog(successWindow.Message, successWindow);

                LotInformation newLot = new LotInformation();

                if (newLot != null)
                {

                    newLot.Id = lot.Id;
                    newLot.lot_number = lot.lot_number;
                    newLot.exp_date = lot.exp_date;

                    LotNumber = Lot.lot_number;
                    ExpirationDate = Lot.exp_date.ToString();

                    foreach (Components comp in lot.Components)
                    {
                        newLot.Components.Add(comp);

                    }
                    ComponentsList = newLot.Components;

                    foreach (Families fam in lot.Families)
                    {

                        newLot.Families.Add(fam);
                    }
                    FamiliesList = newLot.Families;

                    try
                    {
                        db.LotInformation.Add(newLot);
                        db.SaveChanges();

                        //Grabs the lot_number column from db that is distinct
                        var lotNum = db.LotInformation.GroupBy(i => i.lot_number).Select(group => group.FirstOrDefault());

                        //Loops through the lot numbers column in db and converts to list 
                        foreach (var item in lotNum)
                        {
                            Console.WriteLine(item.lot_number);
                        }
                        LotNumList = lotNum.ToList();

                        Console.WriteLine("successfully");
                    }
                    catch
                    {
                        //TODO: Add a Dialog Here

                    }
                }
                else if (lotNumDbExists == true)
                {
                    // Inform user that the lot_number already exists
                    errorWindow.Message = LanguageResources.Resource.Lot_Exists_Already;
                    dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
                    logger.writeErrLog(LanguageResources.Resource.Lot_Exists_Already);
                    return;


                }
            }

        }
    }
反序列化函数以查看批次在何处获取数据:

 public void DeserializedStream(string filePath)
    {

        XmlRootAttribute xRoot = new XmlRootAttribute();
        xRoot.ElementName = "lot_information";
        xRoot.IsNullable = false;

        // Create an instance of lotinformation class.
       LotInformation lot = new LotInformation();

        // Create an instance of stream writer.
        TextReader txtReader = new StreamReader(filePath);

        // Create and instance of XmlSerializer class.
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LotInformation), xRoot);

        // DeSerialize from the StreamReader
        lot = (LotInformation)xmlSerializer.Deserialize(txtReader);

        // Close the stream reader
        txtReader.Close();

        LotInformation newList = new LotInformation();

        using (var db = new DDataContext())
        {

            bool isDuplicate = db.LotInformation.Any(r => r.lot_number == lot.lot_number);

            if (newList != null && isDuplicate == false)
            {

                newList.Id = lot.Id;
                newList.lot_number = lot.lot_number;
                newList.exp_date = lot.exp_date;

                DeserialLotNumber = newList.lot_number;
                DeserialExpirationDate = newList.exp_date.ToString();

                foreach (Component comp in lot.Components)
                {
                    newList.Components.Add(comp);

                }
                DeserialComponentsList = newList.Components;

                foreach (Families fam in lot.Families)
                {

                    newList.Families.Add(fam);
                }
                DeserialFamiliesList = newList.Families;
            }
            else if (isDuplicate == true)
            {
                DeserialAnalytesList = null;
                DeserialFamiliesList = null;
                // Inform user that the lot_number already exists
                errorWindow.Message = LanguageResources.Resource.Lot_Exists_Already;
                dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
                logger.writeErrLog(LanguageResources.Resource.Lot_Exists_Already);
                return;
            }
        }

    }

我建议您使用RelayCommand的通用版


它将允许您将lot从视图传递给命令,您只需在当前数据上下文中存储lot即可。

我发现了问题所在:

设置
专用信息批次后构造函数之前的字段,我在本地重新声明了我的错误:

 LotInformation lot = new LotInformation();
将其更改为:

lot = new LotInformation(); 

它是有效的

@JohnSaunders,对不起。谢谢你的编辑。我在这里没有看到足够的信息来解释为什么外部字段为空。你能在你认为应该设置的地方设置一个断点并确定哪里出了问题吗?@BradleyDotNET,我补充了一些信息。。。当我试图设置newLot=lot.lot\u number etc时,该批次本身似乎为空!!因为我在当地重新申报了很多!