在我的应用程序中,我显示来自社交网络vkontakte(俄语facebook :)的新闻源。我向服务器发送请求并收到一个xml文件,其中包括大约50个新闻源项目。 xml的示例(50项中的2项)
<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>
中包含不同的内容(文字,照片,视频,音频,文档,文字和照片,文字和音频,以及此工作人员的所有可能组合)。我已经在这里问了这个问题。我有一个答案 - 使用模板选择器。嗯...我用它,现在我有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个来自16个模板。嗯......它有效。
但是,有时列表框开始使用一些奇怪的行为,我认为它是因为这个模板。现在,我必须在不同的页面上制作类似的列表框,我认为使用模板选择器并不是一个好主意。任何人都可以告诉我,有没有不同的方法来构建具有不同内容的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>
答案 0 :(得分:0)
通常,每个具有完全不同内容的XML组件(例如视频与图像或文本等)都需要不同的模板。但是您应该将模板定义到不同的XAML文件中,并将其作为资源字典添加到每个控件中。这样,您可以在一个位置定义模板并将其导入所有控件,而无需复制/粘贴任何内容。您甚至可以使用MergedDictionary
以这种方式导入多个XAML文件。
我不确定你是如何从该网站读取你的XML文件的,但也许你应该将它们解析为代码隐藏中的自定义对象,在更简单的类别中?这样可以为您的应用程序提供更加流线型的感觉。否则说,在代码隐藏中解析XML,确定它是什么类型的内容,并填充自己的“视频”,“图像”和“文本”对象。如果您可以将许多不同的XML部分分类到同一个对象中,那么从长远来看,您将不得不依赖更少的模板选择器/数据模板。
但当然我推测了很多东西,希望这对你有所帮助。
编辑:
Style="{StaticResource=TextCommentStyle}"
。使用这种方法的好处是,您将拥有更少的容器,并且您将使用模板选择器让应用程序“组合”它们。如果有视频和一些文本,那么datatemplate应该使用各自的UserControl将视频放在ListItem中,将文本放在另一个ListItem中。如果您需要编辑视频的显示方式,您只需编辑1个视频UserControl,它就会反映在整个应用程序中。与WPF控件的样式相同:将它们设置为样式,您可以在单个文件中全局更改它们。它的维护和调试要容易得多。