在我的应用程序中,我显示了来自社交网络 vkontakte(俄罗斯 facebook:) 的新闻源。我向服务器发送请求并接收一个 xml 文件,其中包含大约 50 个新闻源项目。xml 示例(2 项,共 50 项)
<item>
<type>post</type>
<source_id>-26406986</source_id>
<date>1356207001</date>
<post_id>1058858</post_id>
<text>#Суть на #lm.</text>
<attachment>
<type>photo</type>
<photo>
<pid>293678498</pid>
<aid>-7</aid>
<owner_id>-26406986</owner_id>
<user_id>65647811</user_id>
<src>http://cs406928.userapi.com/v406928811/61c7/Rjee0rUq3vU.jpg</src>
<src_big>http://cs406928.userapi.com/v406928811/61c8/PSYiLcxlv9s.jpg</src_big>
<src_small>http://cs406928.userapi.com/v406928811/61c6/26XXAbejR7U.jpg</src_small>
<width>186</width>
<height>604</height>
<text />
<created>1356203380</created>
<access_key>ca379f61ca5de866ae</access_key>
</photo>
</attachment>
<attachments list="true">
<attachment>
<type>photo</type>
<photo>
<pid>293678498</pid>
<aid>-7</aid>
<owner_id>-26406986</owner_id>
<user_id>65647811</user_id>
<src>http://cs406928.userapi.com/v406928811/61c7/Rjee0rUq3vU.jpg</src>
<src_big>http://cs406928.userapi.com/v406928811/61c8/PSYiLcxlv9s.jpg</src_big>
<src_small>http://cs406928.userapi.com/v406928811/61c6/26XXAbejR7U.jpg</src_small>
<width>186</width>
<height>604</height>
<text />
<created>1356203380</created>
<access_key>ca379f61ca5de866ae</access_key>
</photo>
</attachment>
</attachments>
<comments>
<count>47</count>
<can_post>1</can_post>
</comments>
<likes>
<count>167</count>
<user_likes>0</user_likes>
<can_like>1</can_like>
<can_publish>1</can_publish>
</likes>
<reposts>
<count>35</count>
<user_reposted>0</user_reposted>
</reposts>
<post_source>
<type>vk</type>
</post_source>
</item>
每个项目都可以有不同的内容<attachments>
(文本、照片、视频、音频、文档、文本 n 照片、文本 n 音频以及该五线谱的所有可能组合)。我已经在这里问过这个问题。我有一个答案 - 使用模板选择器。嗯……我用过,现在我有 16 种不同listbox
的物品,而且不是所有可能的组合。它在 xaml 中的代码非常庞大。示例,2 个模板:
视频和音频
<!--VideonAudios--> <local:NewsTemplateSelector.VideonAudios>
<DataTemplate>
<Grid Name="VideoGrid" 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="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 Video[0].Title}" FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Height="auto" />
<Image Source="{Binding Video[0].Big}" Margin="10" Grid.Column="1" Grid.Row="2" Tap="Video_Tap" />
<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>
<Image Source="\icons\appbar.check.rest.png" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="4" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
<TextBlock Text="{Binding Time}" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="4" />
<TextBlock Text="{Binding Likes}" Name="LikeTxtBox" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="4" 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" />
<TextBox Text="{Binding Video[0].Url}" Tag="VURL" Visibility="Collapsed" />
</Grid>
</DataTemplate>
</local:NewsTemplateSelector.VideonAudios>
它只有 16 个模板中的 2 个。嗯......它有效。
但是,有时列表框会以一些奇怪的行为开始工作,我认为这是因为这个模板。现在,我必须在不同的页面上制作类似的列表框,我认为使用模板选择器不是一个好主意。谁能告诉我,有没有不同的方法来构建listbox
具有不同内容的项目?
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>