我使用的是适用于Windows Phone的相同类型的代码,但现在在WPF桌面应用程序中使用它。
这会读取一个目录,然后读取目录中的每个xml,然后读取xml并将详细信息广告到_item类。
它似乎正在向_item添加每个单独的项目(比如它从目录中读取3 xml),但是列表中填充了三个具有相同细节的项目 - (从它读取的最后一个xml)。 我在这里做错了什么?
C#
foreach (string fileNameXML in fileEntries)
{
//read 1 xml at a time.
XmlDocument doc = new XmlDocument();
doc.Load(fileNameXML);
XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Job");
foreach (XmlNode node in nodes)
{
_item.Input = node.SelectSingleNode("Input").InnerText;
_item.OutputFolder = node.SelectSingleNode("OutputFolder").InnerText;
_item.OutputFile = node.SelectSingleNode("OutputFile").InnerText;
_item.Format = node.SelectSingleNode("Format").InnerText;
_item.Name = node.SelectSingleNode("Name").InnerText;
_item.Effects = node.SelectSingleNode("Effect").InnerText;
_item.Type = node.SelectSingleNode("Type").InnerText;
_item.Output = _item.OutputFolder + _item.OutputFile + "." + _item.Format.ToLower();
lstBoxQueue.Items.Add(_item);
}
}
}
XAML:
<ListBox Name="lstBoxQueue" BorderBrush="{x:Null}" Background="#FFA09F9F" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="80" Width="629" HorizontalAlignment="Left" Background="#FFCDCDCD" >
<Border BorderBrush="Gray" BorderThickness="1"/>
<TextBlock Text="Job name: " Margin="9,10,432,49" FontWeight="Bold" Foreground="#FF323232" FontSize="12" />
<TextBlock Foreground="Black" HorizontalAlignment="Left" MinWidth="50" FontSize="10" TextWrapping="Wrap" Text="{Binding Input}" Margin="76,33,0,0" VerticalAlignment="Top" MaxWidth="270" MaxHeight="15"/>
<TextBlock Foreground="Black" HorizontalAlignment="Left" FontSize="10" TextWrapping="Wrap" Text="{Binding Output}" Margin="77,54,0,0" MinWidth="100" MaxWidth="270" VerticalAlignment="Top" />
<TextBlock Foreground="Black" HorizontalAlignment="Left" MinWidth="50" FontSize="10" TextWrapping="Wrap" Text="{Binding Name}" Margin="76,12,0,0" VerticalAlignment="Top" />
<TextBlock Foreground="Black" HorizontalAlignment="Left" FontSize="10" TextWrapping="Wrap" Text="{Binding Effects}" Margin="374,31,0,0" MinWidth="100" MaxWidth="270" VerticalAlignment="Top" Height="40" />
<TextBlock Foreground="Black" HorizontalAlignment="Left" FontSize="10" TextWrapping="Wrap" Text="{Binding Type}" Margin="374,11,0,0" MinWidth="100" MaxWidth="270" VerticalAlignment="Top" Height="15" />
<Button Content="More Info.." Width="80" Height="25" Margin="538,10,11,45"/>
<Button Content="Cancel Job" Width="80" Height="25" Click="ClickCancelJob" Margin="539,43,10,12"/>
<TextBlock Text="Output: " Margin="25,52,414,7" FontWeight="SemiBold" Foreground="#FF323232"/>
<TextBlock Text="Effects: " Margin="351,29,102,30" FontWeight="SemiBold" Foreground="#FF323232"/>
<TextBlock Text="Input: " Margin="33,30,430,29" FontWeight="SemiBold" Foreground="#FF323232"/>
<TextBlock Text="Export Type: " Margin="318,10,100,49" FontWeight="SemiBold" Foreground="#FF323232"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
项目类:
class XMLItems
{
//Accessor class for properties of jobs. Enables all pages to access the job properties.
private string _input;
private string _outputFolder;
private string _outputFile;
private string _output;
private string _name;
private string _effects;
private string _type;
private string _format;
public string Input
{
get { return _input; }
set { _input = value; }
}
public string OutputFolder
{
get { return _outputFolder; }
set { _outputFolder = value; }
}
public string OutputFile
{
get { return _outputFile; }
set { _outputFile = value; }
}
public string Output
{
get { return _output; }
set { _output = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Effects
{
get { return _effects; }
set { _effects = value; }
}
public string Type
{
get { return _type; }
set { _type = value; }
}
public string Format
{
get { return _format; }
set { _format = value; }
}
}
答案 0 :(得分:2)
您的foreach
每次都会添加相同的_item
引用,并在下次循环运行时对其进行修改。结果很可能是最后一份工作在所有职位上都显示出来。
在内部_item
循环中将foreach
创建为局部变量,这样每个项目都独立于最后一个:
foreach (string fileNameXML in fileEntries)
{
//read 1 xml at a time.
XmlDocument doc = new XmlDocument();
doc.Load(fileNameXML);
XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Job");
foreach (XmlNode node in nodes)
{
var item = new MyItem(); // Whatever type _item was.
item.Input = node.SelectSingleNode("Input").InnerText;
item.OutputFolder = node.SelectSingleNode("OutputFolder").InnerText;
item.OutputFile = node.SelectSingleNode("OutputFile").InnerText;
item.Format = node.SelectSingleNode("Format").InnerText;
item.Name = node.SelectSingleNode("Name").InnerText;
item.Effects = node.SelectSingleNode("Effect").InnerText;
item.Type = node.SelectSingleNode("Type").InnerText;
item.Output = item.OutputFolder + item.OutputFile + "." + item.Format.ToLower();
lstBoxQueue.Items.Add(item);
}
}