【发布时间】:2020-02-23 16:45:35
【问题描述】:
我有一个多列列表框,里面充满了结构:
List<SoftwareAssetStruct> software = new List<SoftwareAssetStruct>();
foreach (DataRow dataRow in dt.Rows)
{
software.Add(new SoftwareAssetStruct()
{
Softwareid = dataRow["id"].ToString(),
Softwarename = dataRow["software_name"].ToString(),
Softwareversion = dataRow["software_version"].ToString(),
Softwaregroup = dataRow["software_group"].ToString(),
Softwarelicensenr = dataRow["software_license_nr"].ToString()
});
}
softListBox.ItemsSource = software;
softListBox.SelectionMode = SelectionMode.Multiple;
选择完成后,我导航到一个新页面。
private void SelectSoftware(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new MailPage(softListBox));
}
现在我可以可视化这些条目,但我无法将其转换为结构,因此我可以访问单独的数据字段
public MailPage(ListBox lbx)
{
InitializeComponent();
Array komisch;
if (lbx != null)
{
for (int i = 0; i < lbx.SelectedItems.Count; i++)
{
softListBox12.Items.Add(lbx.SelectedItems[i]);
}
}
foreach( SoftwareAssetStruct structAsset in softListBox12.Items)
{
MessageBox.Show(structAsset.Softwarename);
}
}
我在两个页面上都定义了这样的结构:
private class SoftwareAssetStruct
{
public string Softwareid { get; set; }
public string Softwarename { get; set; }
public string Softwareversion { get; set; }
public string Softwaregroup { get; set; }
public string Softwarelicensenr { get; set; }
}
当我尝试将其转换回来时,我收到以下错误消息:
System.InvalidCastException:“[A]SoftwareAssetStruct 无法转换为 [B]SoftwareAssetStruct。类型 A 源自“Share-Me,版本=1.0.0.0,Culture=neutral,PublicKeyToken=null”上下文“默认”在位置 'C:\Users\XXX\source\repos\Share-Me\Share-Me\bin\Debug\netcoreapp3.0\Share-Me.dll'。类型 B 源自 'Share-Me,版本 = 1.0。 0.0,Culture=neutral,PublicKeyToken=null' 在位置 'C:\Users\XXX\source\repos\Share-Me\Share-Me\bin\Debug\netcoreapp3.0\Share-Me 的上下文 'Default' 中。 dll'。”
【问题讨论】: