如何将类数组从一个WPF窗口返回到另一个窗口?

时间:2017-02-15 07:13:36

标签: c# arrays wpf class

在从WPF调用的MainWindow.xaml.cs窗口中,我有一个用多个元素定义的类。我创建了这个类类型的array

FieldLengths.xaml.cs 中我有:

public partial class FieldLengths : Window
{
    public FieldJustifyFill[] fjfFields = new FieldJustifyFill[0];
    public class FieldJustifyFill
    {
        public int ColumnNumber { get; set; }
        public bool RightJustify { get; set; }
        public bool LeftJustify { get; set; }
        public bool LeftZeroFill { get; set; }
        public bool RightZeroFill { get; set; }
    }

我这样加载:

try
        {
            dtFields = ((DataView)dtGrid.ItemsSource).ToTable();
            intNumFields = 0;

            for (int intRowCnt = 0; intRowCnt < dtFields.Rows.Count; intRowCnt++)
            {
                bool blnJustifyRight = Convert.ToBoolean(dtFields.Rows[intRowCnt][2]);
                bool blnJustifyLeft = Convert.ToBoolean(dtFields.Rows[intRowCnt][3]);
                bool blnLeftZeroFill = Convert.ToBoolean(dtFields.Rows[intRowCnt][4]);
                bool blnRightZeroFill = Convert.ToBoolean(dtFields.Rows[intRowCnt][5]);

                if (blnJustifyRight || blnJustifyLeft || blnLeftZeroFill || blnRightZeroFill)
                {

                    Array.Resize(ref fjfFields, intNumFields + 1);
                    fjfFields[intNumFields] = new FieldJustifyFill
                    {
                        ColumnNumber = intRowCnt,
                        RightJustify = blnJustifyRight,
                        LeftJustify = blnJustifyLeft,
                        LeftZeroFill = blnLeftZeroFill,
                        RightZeroFill = blnRightZeroFill
                    };

                    intNumFields += 1;
                }
            }
        }
        catch (Exception ex)
        {
            string strMsg;

            strMsg = "RefreshRowSize, error '" + ex.Message + "' has occurred.";
            System.Windows.MessageBox.Show(strMsg);
        }

在MainWindow.xaml.cs 我有这个:

public partial class MainWindow : Window
{
    FieldJustifyFillDest[] fjfFieldsDest = new FieldJustifyFillDest[0];

在例程中,我尝试从 FixedLengths.xaml.cs 中获取值,如下所示:

FieldLengths flWin = new FieldLengths(strInputName, strFieldInfo, null, null, null, strMappingMetadata);
                flWin.Left = System.Windows.Application.Current.MainWindow.Left + 15;
                flWin.Top = desktopWorkArea.Top + 25;
                flWin.ShowDialog();

                if (flWin.blnFLCreateFile)
                {
                    string strPrgFileName;

                    Array.Resize(ref fjfFieldsDest, flWin.fjfFields.Length);

                    for (int i = 0; i < flWin.fjfFields.Length; i++)
                    {
                        int intColumnNumber = flWin.fjfFields[i].ColumnNumber;
                        bool blnRightJustify = flWin.fjfFields[i].RightJustify;
                        bool blnLeftJustify = flWin.fjfFields[i].LeftJustify;
                        bool blnLeftZeroFill = flWin.fjfFields[i].LeftZeroFill;
                        bool blnRightZeroFill = flWin.fjfFields[i].RightZeroFill;

                        fjfFieldsDest[i] = new FieldJustifyFillDest
                        {
                            ColumnNumber = intColumnNumber,
                            RightJustify = blnRightJustify,
                            LeftJustify = blnLeftJustify,
                            LeftZeroFill = blnLeftZeroFill,
                            RightZeroFill = blnRightZeroFill
                        };
                    }

变量intColumnNumberblnRightJustifyblnLeftJustifyblnLeftZeroFillblnRightZeroFill具有正确的值,但当它加载到fjfFieldsDest [i]时,它们是不正确。

如何正确返回类数组?我无法在任何地方找到一个好的例子。

1 个答案:

答案 0 :(得分:0)

道歉,我的代码在编码时正常工作。调试器按字母顺序重新排序类元素,我没有注意到。对不起。但是,如果我编码它的方式不正确,我欢迎任何反馈正确编码。