C# 如何构建listbox';一个列表框中具有不同控件的s项
在我的应用程序中,我显示了来自社交网络vkontakte(俄罗斯facebook:)的新闻提要。我向服务器发送请求并接收一个xml文件,其中包含大约50个新闻提要条目。xml示例(2项,共50项) 它只有16个模板中的2个。好。。。它起作用了。 但是,有时候listbox开始工作时会出现一些奇怪的行为,我认为这是因为这个模板。现在,我必须在不同的页面上创建类似的列表框,我认为使用模板选择器不是个好主意。有人能告诉我,有没有什么不同的方法可以用不同的内容构建C# 如何构建listbox';一个列表框中具有不同控件的s项,c#,xml,silverlight,windows-phone-7,listbox,C#,Xml,Silverlight,Windows Phone 7,Listbox,在我的应用程序中,我显示了来自社交网络vkontakte(俄罗斯facebook:)的新闻提要。我向服务器发送请求并接收一个xml文件,其中包含大约50个新闻提要条目。xml示例(2项,共50项) 它只有16个模板中的2个。好。。。它起作用了。 但是,有时候listbox开始工作时会出现一些奇怪的行为,我认为这是因为这个模板。现在,我必须在不同的页面上创建类似的列表框,我认为使用模板选择器不是个好主意。有人能告诉我,有没有什么不同的方法可以用不同的内容构建listboxs项目 UPD:解析
listbox
s项目
UPD:解析xml的方式
void c_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
// string str = null;
XDocument xml = XDocument.Load(e.Result); //полученый xml от апи вконтакта
MessageBox.Show(xml.ToString());
var inf = from c in xml.Descendants("item") //
select c;
int j = -1;
int i = 0;
App.New_From = (string)xml.Element("response").Element("new_from").Value;
foreach (var item in inf)
{
#region static info
i++;
if (inf.Count() > i)
{
if ((string)item.Element("type").Value != "friend")
{
New new1 = new New();
if ((string)item.Element("source_id").Value != null)
{
new1.SourseId = (string)item.Element("source_id").Value;
}
if ((string)item.Element("date").Value != null)
{
// MessageBox.Show(item.Element("date").Value);
new1.Time = App.GetTime(item.Element("date").Value);
// MessageBox.Show(App.GetTime(item.Element("date").Value));
}
if (item.Element("photos") != null)
{
var photos = from c in item.Descendants("photo") //
select c;
foreach (var ph in photos)
{
PhotoAttachment photo = new PhotoAttachment();
photo.OwnerId = ph.Element("owner_id").Value;
photo.PId = ph.Element("pid").Value;
photo.Small = ph.Element("src_small").Value;
photo.Big = ph.Element("src_big").Value;
if (ph.Element("src_xxbig") != null)
photo.xBig = ph.Element("src_xxbig").Value;
else if (ph.Element("src_xbig") != null)
photo.xBig = ph.Element("src_xbig").Value;
else
photo.xBig = ph.Element("src_big").Value;
new1.photoAttachments.Add(photo);
}
}
if (item.Element("copy_owner_id") != null)
{
new1.Copy_owner_id = (string)item.Element("copy_owner_id").Value;
}
if (item.Element("post_id") != null)
{
new1.Post_id = (string)item.Element("post_id").Value;
}
if ((string)item.Element("text") != null)
{
new1.Texts = (string)item.Element("text");
}
if ((string)item.Element("comment") != null)
new1.Comments = (string)item.Element("comment").Element("count").Value;
if ((string)item.Element("likes") != null)
new1.Likes = (string)item.Element("likes").Element("count").Value;
#endregion
if (item.Element("attachment") != null)
{
var vd = from c in item.Descendants("attachment")
select c;
foreach (var content in vd)
{
if (content.Element("audio") != null)
{
AudioAttachment audio = new AudioAttachment();
audio.Title = (string)content.Element("audio").Element("title").Value;
audio.Perfomer = (string)content.Element("audio").Element("performer").Value;
string tmp = audio.Perfomer + " - " + audio.Title;
audio.Title = tmp;
audio.OwnerId = (string)content.Element("audio").Element("owner_id").Value;
audio.Audio_Id = (string)content.Element("audio").Element("aid").Value;
audio.Duration = (string)content.Element("audio").Element("duration").Value;
audio.Url = audio.OwnerId + "_" + audio.Audio_Id;
audio.Count = j++;
if (new1.audioAttachments.Count > 0)
{
if (audio.Audio_Id == new1.audioAttachments[0].Audio_Id)
{
new1.audioAttachments.RemoveAt(0);
}
}
new1.audioAttachments.Add(audio);
}
if (content.Element("photo") != null)
{
PhotoAttachment photo = new PhotoAttachment();
photo.OwnerId = (string)content.Element("photo").Element("owner_id").Value;
photo.PId = (string)content.Element("photo").Element("pid").Value;
photo.Small = (string)content.Element("photo").Element("src_small").Value;
photo.Big = (string)content.Element("photo").Element("src_big").Value;
if (content.Element("photo").Element("src_xxbig") != null)
photo.xBig = (string)content.Element("photo").Element("src_xxbig").Value;
else if (content.Element("photo").Element("src_xbig") != null)
photo.xBig = (string)content.Element("photo").Element("src_xbig").Value;
else
photo.xBig = (string)content.Element("photo").Element("src_big").Value;
if (new1.photoAttachments.Count > 0)
{
if (photo.Big == new1.photoAttachments[0].Big)
{
new1.photoAttachments.RemoveAt(0);
}
}
new1.photoAttachments.Add(photo);
}
if (content.Element("video") != null)
{
VideoAttachment video = new VideoAttachment();
video.Title = (string)content.Element("video").Element("title").Value;
video.Description = (string)content.Element("video").Element("description").Value;
video.OwnerId = (string)content.Element("video").Element("owner_id").Value;
if ((string)content.Element("video").Element("image_small") != null)
video.Small = (string)content.Element("video").Element("image_small").Value;
if ((string)content.Element("video").Element("image_big") != null)
video.Big = (string)content.Element("video").Element("image_big").Value;
video.Video_Id = (string)content.Element("video").Element("vid").Value;
video.Url = video.OwnerId + "_" + video.Video_Id;
if (new1.videoAttachments.Count > 0)
{
if (video.Big == new1.videoAttachments[0].Big)
{
new1.videoAttachments.RemoveAt(0);
}
}
new1.videoAttachments.Add(video);
}
if (content.Element("link") != null)
{
new1.Priority = PostType.Link;
new1.Url.Title = (string)content.Element("link").Element("title").Value;
new1.Url.Description = (string)content.Element("link").Element("description").Value;
new1.Url.Url = (string)content.Element("link").Element("url").Value;
if ((string)content.Element("link").Element("image_src") != null)
new1.Url.Image = (string)content.Element("link").Element("image_src").Value;
else if (new1.photoAttachments.Count > 0 && new1.photoAttachments[0].Big != null)
new1.Url.Image = new1.photoAttachments[0].Big;
else if (new1.photoAttachments.Count > 0 && new1.photoAttachments[1].Big != null)
new1.Url.Image = new1.photoAttachments[1].Big;
}
}
}
if (item.Element("comments") != null)
{
if (item.Element("comments").Element("can_post") != null)
new1.CanPost = Convert.ToInt32(item.Element("comments").Element("can_post").Value);
}
//str += string.Format("{0} {1} {2}\n", NewsList.Count, new1.audioAttachments.Count, new1.photoAttachments.Count);
if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Text;
if (new1.photoAttachments.Count == 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Photo;
if (new1.photoAttachments.Count > 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Photos;
if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 1 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Video;
if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count > 1 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Videos;
if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.Audio;
if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.Audios;
if (new1.photoAttachments.Count == 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.PhotonAudio;
if (new1.photoAttachments.Count == 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.PhotonAudios;
if (new1.photoAttachments.Count > 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.PhotosnAudio;
if (new1.photoAttachments.Count > 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.PhotosnAudios;
if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 1 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.VideonAudio;
if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 1 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.VideonAudios;
if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count > 1 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.VideosnAudio;
if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count > 1 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.VideosnAudios;
NewsList.Add(new1);
new1 = null;
}
}
我能做点像这样的事吗
<local:NewsTemplateSelector.Content.Audios>
<ListBox Name="audiosListbox" ItemsSource="{Binding Audio}" MaxHeight="500" Margin="10" Grid.Column="1" Grid.Row="3" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" Grid.Column="1" FontSize="25" Tap="AudioTitleTap" />
<Button Content="Play" Height="70" Tap="Audios_Button_Click" />
<TextBox Text="{Binding Count}" Tag="Count" Visibility="Collapsed" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<local:NewsTemplateSelector.Content.Audios>
和图像、视频、视频等类似,然后像
<!--Audios-->
<local:NewsTemplateSelector.Audios>
<DataTemplate>
<Grid Name="AudiosGrid" MaxHeight="2000" Tap="ListBoxTap" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="28"/>
</Grid.RowDefinitions>
<Image Source="{Binding SourceImage}" Tap="GetUserInfo" Width="75"/>
<TextBlock Text="{Binding SourceName}" FontSize="25" Grid.Column="1" TextWrapping="Wrap" />
<TextBlock Grid.Column="1" TextWrapping="Wrap" Text="{Binding Copy_owner_name}" FontSize="25" VerticalAlignment="Bottom" Margin="39,0,0,0" MaxHeight="35"/>
<Image Source="{Binding Copy_owner_photo}" Grid.Column="1" HorizontalAlignment="Left" Height="35" Width="39" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding Texts}" FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Height="auto" />
<local:NewsTemplateSelector.Content.Audios>
</local:NewsTemplateSelector.Content.Audios>
<Image Source="\icons\appbar.check.rest.png" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
<TextBlock Text="{Binding Time}" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="3" />
<TextBlock Text="{Binding Likes}" Name="LikeTxtBox" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" TextWrapping="Wrap" Width="30" Margin="0,0.5,0,-0.5"/>
<Line X1="0" Y1="27" X2="420" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" Grid.Column="1" />
<Line X1="0" Y1="27" X2="80" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" />
</Grid>
</DataTemplate>
不是
<!--Audios-->
<local:NewsTemplateSelector.Audios>
<DataTemplate>
<Grid Name="AudiosGrid" MaxHeight="2000" Tap="ListBoxTap" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="28"/>
</Grid.RowDefinitions>
<Image Source="{Binding SourceImage}" Tap="GetUserInfo" Width="75"/>
<TextBlock Text="{Binding SourceName}" FontSize="25" Grid.Column="1" TextWrapping="Wrap" />
<TextBlock Grid.Column="1" TextWrapping="Wrap" Text="{Binding Copy_owner_name}" FontSize="25" VerticalAlignment="Bottom" Margin="39,0,0,0" MaxHeight="35"/>
<Image Source="{Binding Copy_owner_photo}" Grid.Column="1" HorizontalAlignment="Left" Height="35" Width="39" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding Texts}" FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Height="auto" />
<ListBox Name="audiosListbox" ItemsSource="{Binding Audio}" MaxHeight="500" Margin="10" Grid.Column="1" Grid.Row="2" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" Grid.Column="1" FontSize="25" Tap="AudioTitleTap" />
<Button Content="Play" Height="70" Tap="Audios_Button_Click" />
<TextBox Text="{Binding Count}" Tag="Count" Visibility="Collapsed" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Image Source="\icons\appbar.check.rest.png" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
<TextBlock Text="{Binding Time}" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="3" />
<TextBlock Text="{Binding Likes}" Name="LikeTxtBox" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" TextWrapping="Wrap" Width="30" Margin="0,0.5,0,-0.5"/>
<Line X1="0" Y1="27" X2="420" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" Grid.Column="1" />
<Line X1="0" Y1="27" X2="80" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" />
</Grid>
</DataTemplate>
通常,具有完全不同内容的每个XML组件(例如视频与图像,或仅文本等)都需要不同的模板。但您应该将模板定义到不同的XAML文件中,并将其作为ressource字典添加到每个控件中。这样,您就可以在一个位置定义模板并将其导入所有控件,而无需复制/粘贴任何内容。您甚至可以使用
MergedDictionary
以这种方式导入多个XAML文件
我不确定您是如何从该站点读取XML文件的,但也许您应该将它们解析为代码隐藏中更简单类别的自定义对象?这也会给您的应用程序带来更精简的感觉。否则,在代码背后解析XML,确定它是什么类型的内容,并填充您自己的“视频”、“图像”和“文本”对象。如果您可以将许多不同的XML部分分类到同一个对象中,那么从长远来看,您将不得不依赖更少的模板选择器/数据模板
当然,我在猜测很多事情,希望这能对你有所帮助
编辑:
Style=“{StaticResource=TextCommentStyle}”
使用这种方法的优点是,您将拥有更少的容器,并且可以让应用程序使用模板选择器“组合”它们。如果有视频和一些文本,那么datatemplate应该使用各自的用户控件将视频放在一个ListItem中,将文本放在另一个ListItem中。如果需要编辑视频的显示方式,只需编辑一个视频用户控件,它就会反映在整个应用程序中。WPF控件的样式也是这样:在样式中设置它们,您可以在单个文件中全局更改它们。它更易于维护和调试。我添加了解析xml的代码。我阅读了您的评论,如果我正确理解您的意思,我可以创建一个ressource字典,在其中定义控件“视频”、“音频”、“图像”和“文本”,就像我在“UPD”中显示的那样,然后将它们分组到
列表框
的项目中。我更新了说明,并做了一些澄清。我会使用资源字典来保存与视觉样式相关的任何内容,也会保存数据模板,但我会对需要处理的每种类型的附件或内容(包括最简单的文本框或注释)使用一个不同的用户控件。此外,我不会像在解析器中那样对项目的优先级进行分类。相反,您应该让UI逻辑(如模板选择器)检查对象的内容,并根据每个附件逐个确定在屏幕上显示何种视觉效果。
<!--Audios-->
<local:NewsTemplateSelector.Audios>
<DataTemplate>
<Grid Name="AudiosGrid" MaxHeight="2000" Tap="ListBoxTap" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="28"/>
</Grid.RowDefinitions>
<Image Source="{Binding SourceImage}" Tap="GetUserInfo" Width="75"/>
<TextBlock Text="{Binding SourceName}" FontSize="25" Grid.Column="1" TextWrapping="Wrap" />
<TextBlock Grid.Column="1" TextWrapping="Wrap" Text="{Binding Copy_owner_name}" FontSize="25" VerticalAlignment="Bottom" Margin="39,0,0,0" MaxHeight="35"/>
<Image Source="{Binding Copy_owner_photo}" Grid.Column="1" HorizontalAlignment="Left" Height="35" Width="39" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding Texts}" FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Height="auto" />
<local:NewsTemplateSelector.Content.Audios>
</local:NewsTemplateSelector.Content.Audios>
<Image Source="\icons\appbar.check.rest.png" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
<TextBlock Text="{Binding Time}" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="3" />
<TextBlock Text="{Binding Likes}" Name="LikeTxtBox" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" TextWrapping="Wrap" Width="30" Margin="0,0.5,0,-0.5"/>
<Line X1="0" Y1="27" X2="420" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" Grid.Column="1" />
<Line X1="0" Y1="27" X2="80" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" />
</Grid>
</DataTemplate>
<!--Audios-->
<local:NewsTemplateSelector.Audios>
<DataTemplate>
<Grid Name="AudiosGrid" MaxHeight="2000" Tap="ListBoxTap" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="28"/>
</Grid.RowDefinitions>
<Image Source="{Binding SourceImage}" Tap="GetUserInfo" Width="75"/>
<TextBlock Text="{Binding SourceName}" FontSize="25" Grid.Column="1" TextWrapping="Wrap" />
<TextBlock Grid.Column="1" TextWrapping="Wrap" Text="{Binding Copy_owner_name}" FontSize="25" VerticalAlignment="Bottom" Margin="39,0,0,0" MaxHeight="35"/>
<Image Source="{Binding Copy_owner_photo}" Grid.Column="1" HorizontalAlignment="Left" Height="35" Width="39" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding Texts}" FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Height="auto" />
<ListBox Name="audiosListbox" ItemsSource="{Binding Audio}" MaxHeight="500" Margin="10" Grid.Column="1" Grid.Row="2" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" Grid.Column="1" FontSize="25" Tap="AudioTitleTap" />
<Button Content="Play" Height="70" Tap="Audios_Button_Click" />
<TextBox Text="{Binding Count}" Tag="Count" Visibility="Collapsed" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Image Source="\icons\appbar.check.rest.png" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
<TextBlock Text="{Binding Time}" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="3" />
<TextBlock Text="{Binding Likes}" Name="LikeTxtBox" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" TextWrapping="Wrap" Width="30" Margin="0,0.5,0,-0.5"/>
<Line X1="0" Y1="27" X2="420" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" Grid.Column="1" />
<Line X1="0" Y1="27" X2="80" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" />
</Grid>
</DataTemplate>