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

时间:2014-05-14 19:34:37

标签: c# wpf icommand relaycommand

这是我的ICommand:

public ICommand ConfirmLotSavedCommand {
        get
        {
            return new RelayCommand(ConfirmLotSaved);
        }
    }

问题是我在用户单击确认按钮后将要存储到数据库中的数据反序列化。如果用户没有点击确认或批号已经存在,那么我不想在db中保存反序列化的字符串。

由于范围原因,我在ConfirmLotSaved()方法中使用一个参数调用函数时遇到问题。

所以我创建了一个反序列化的集合作为字段,并将代码保存到ConfirmLotSaved()内部的db。但是,由于一些奇怪的原因,该字段为空...我不确定原因。

这是我的尝试:

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

    }

2 个答案:

答案 0 :(得分:1)

我弄清楚出了什么问题:

在构造函数之前设置private LotInformation lot;字段后,我在本地重新声明了我的错误:

 LotInformation lot = new LotInformation();

将其更改为:

lot = new LotInformation(); 

它有效。

答案 1 :(得分:0)

我建议您使用RelayCommand的通用版http://www.kellydun.com/wpf-relaycommand-with-parameter/

它允许您从视图中将批次传递给命令,只需将所有内容存储在当前的DataContext中。