0
在從MainWindow.xaml.cs
調用的WPF
窗口中,我有一個用多個元素定義的類。我創建了這個類類型的array
。如何從一個WPF窗口將類數組返回到另一個窗口?
在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];
而在一個例程中,我嘗試獲得VA梅毒從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'而不是搞亂數組。 –
#Jeroen van Langen - 你能解釋一下怎麼做? – Cass