将Windows通用应用程序中集线器控制中的数据与列表项绑定
本文关键字:数据 列表 绑定 集线器 Windows 应用程序 控制 | 更新日期: 2023-09-27 18:27:47
你好,我的列表定义如下
public class Article
{
public string title { get; set; }
public string author { get; set; }
public string content { get; set; }
}
public static List<Article> articles = new List<Article>();
我使用异步方法从服务器获取xml数据并对其进行解析
private async void Button_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
//Progress Bar
prg.Visibility = Visibility.Visible;
string xml = string.Empty;
Uri url = new Uri("http://someurl.com/someting.php");
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml");
var response = await httpClient.GetAsync(url);
using (var responseStream = await response.Content.ReadAsStreamAsync())
using (var streamReader = new StreamReader(responseStream))
{
xml = streamReader.ReadToEnd();
}
try
{
Debug.WriteLine("Parsing to start");
string eve = "article";
XDocument loadedData = XDocument.Parse(xml);
foreach (var item in loadedData.Descendants(eve))
{
try
{
Article c = new Article();
c.title = item.Element("title").Value;
c.author = item.Element("author").Value;
c.content = item.Element("content").Value;
articles.Add(c);
}
catch
{
Debug.WriteLine("Failed");
}
}
Debug.WriteLine("About to add items");
articlelist.DataContext = articles;
Debug.WriteLine("Items added");
}
catch
{
Debug.WriteLine("Parsing Failed");
}
prg.Visibility = Visibility.Collapsed;
}
以下是xaml UI elments
<Page
x:Class="Airtrixz.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Airtrixz"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<ProgressBar Grid.Row="0" IsIndeterminate="True" Foreground="{StaticResource PhoneAccentBrush}" VerticalAlignment="Top" Height="16" x:Name="prg" Visibility="Collapsed"/>
<TextBlock Text="Airtrixz" Margin="10,0,0,0" FontSize="30"/>
<Hub Margin="0,50" Foreground="White">
<HubSection Header="posts" x:Name="articlelist" >
<DataTemplate>
<StackPanel Background="Transparent">
<TextBlock Foreground="White" Text="{Binding title}" Height="25" />
<TextBlock Foreground="White" Text="{Binding author}" Height="25"/>
<TextBlock Foreground="White" Text="{Binding content}" Height="25"/>
</StackPanel>
</DataTemplate>
</HubSection>
</Hub>
<Button Width="100" Margin="10,10,0,0" VerticalAlignment="Bottom" Height="50" Tapped="Button_Tapped" Content="Load"/>
</Grid>
</Page>
做articlelist.DataContext=articles
似乎很好。但没有显示任何列表项,HubSection 中的标题、内容和作者属性出现以下错误
错误:BindingExpression路径错误:在"System.Collections.Generic.List 1[[Airtrixz.MainPage+Article, Airtirxz.WindowsPhone, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. BindingExpression: Path='title' DataItem='System.Collections.Generic.List
1[[Airtrixz.MainPage+Article,Airtirxz.WindowsPhone,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]],mscorlib,Version=4.0.0,Culture=neutrale,PublicKeyToken=7ec85d7bea7798e"上找不到"title"属性;目标元素是'Windows.UI.Xaml.Controls.TextBlock'(名称='ull');目标属性是"文本"(类型为"字符串")
有人能给我一个解决方案吗?
由于HubSection的DataTemplate是一个StackPanel,它将只显示一个对象,而不是整个文章列表。为此,您需要一个ListBox或ListView。这就是您收到Bindings错误的原因;它正试图在您的列表中查找"title"属性,但该属性当然不存在。试试这个:
<Hub Margin="0,50" Foreground="White">
<HubSection Header="posts" x:Name="articlelist" >
<DataTemplate>
<ListView ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Background="Transparent">
<TextBlock Foreground="White" Text="{Binding title}" Height="25" />
<TextBlock Foreground="White" Text="{Binding author}" Height="25"/>
<TextBlock Foreground="White" Text="{Binding content}" Height="25"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</HubSection>
</Hub>
这将把ListView
的ItemsSource
绑定到HubSection
的DataContext
,您在Button_Tapped
回调中将其设置为articles
,因此它应该都能工作。以任何一种方式发布回复,我都可以尝试帮助调试更多内容。
不确定我是否应该把它放在这里作为答案,因为我从未使用过Hub
控件,但我的猜测是HubSections
必须像这样填充:
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<HubSection Header="posts">
<DataTemplate>
<StackPanel Background="Transparent">
<TextBlock Foreground="White" Text="{Binding title}" Height="25" />
<TextBlock Foreground="White" Text="{Binding author}" Height="25"/>
<TextBlock Foreground="White" Text="{Binding content}" Height="25"/>
</StackPanel>
</DataTemplate>
</HubSection>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>