【问题标题】:How do I return a class array from one WPF window to another window?如何将类数组从一个 WPF 窗口返回到另一个窗口?
【发布时间】:2017-02-15 07:13:35
【问题描述】:

在从MainWindow.xaml.cs 调用的WPF 窗口中,我定义了一个包含多个元素的类。我创建了一个此类类型的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] 时它们不正确。

如何正确返回类数组?我在任何地方都找不到很好的例子。

【问题讨论】:

  • 使用调试器查看为什么值不正确。在执行代码之前放置断点。按 F11 逐步查看会发生什么。
  • 你应该检查通用的List&lt;FieldJustifyFillDest&gt; 而不是搞乱数组。
  • #Jeroen van Langen - 你能解释一下怎么做吗?
  • @Cass 你可以在这里查看:List&lt;T&gt; Class - msdn

标签: c# arrays wpf class


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多