【发布时间】: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
};
}
变量intColumnNumber、blnRightJustify、blnLeftJustify、blnLeftZeroFill、blnRightZeroFill 具有正确的值,但是当它被加载到 fjfFieldsDest[i] 时它们不正确。
如何正确返回类数组?我在任何地方都找不到很好的例子。
【问题讨论】:
-
使用调试器查看为什么值不正确。在执行代码之前放置断点。按 F11 逐步查看会发生什么。
-
你应该检查通用的
List<FieldJustifyFillDest>而不是搞乱数组。 -
#Jeroen van Langen - 你能解释一下怎么做吗?
-
@Cass 你可以在这里查看:
List<T>Class - msdn