我正在开发一个Windows Phone应用程序。
我有以下C#代码:
private void Default_Loaded(object sender, RoutedEventArgs e)
{
long gameId;
short gameType;
loadedData = XDocument.Load("SampleData/GamesDesc.xml");
if (NavigationContext.QueryString.Count == 2)
{
if (long.TryParse(NavigationContext.QueryString.Values.First(), out gameId))
{
if (short.TryParse(NavigationContext.QueryString.Values.Last(), out gameType))
{
var filteredData = from c in loadedData.Descendants("gameDescription")
where c.Attribute("game_id").Value == gameId.ToString() &&
c.Attribute("gameType").Value == gameType.ToString() &&
c.Attribute("language").Value.Equals(CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
select new SampleData.GameDesc()
{
Id = uint.Parse(c.Attribute("game_id").Value),
Name = c.Attribute("name").Value,
Language = c.Attribute("language").Value,
GameType = uint.Parse(c.Attribute("gameType").Value),
ShortDescription = c.Attribute("shortDescription").Value,
LongDescription = c.Attribute("longDescription").Value
};
LayoutRoot.DataContext = filteredData;
}
}
}
}
以下XAML代码:
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid x:Name="LongDescPanel" Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="0.45*"/>
<RowDefinition Height="0.55*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="LongDescription" Margin="8,8,8,13" TextWrapping="Wrap" Text="{Binding LongDescription}"/>
<Button x:Name="PlayButton" Content="{Binding Path=AppResources.Play, Source={StaticResource LocalizedStrings}}" VerticalAlignment="Top" Margin="165,36,165,0" d:LayoutOverrides="Width" Grid.Row="1" Click="PlayButton_Click" />
</Grid>
</Grid>
</Grid>
为什么LongDescription
TextBlock什么都不显示?
答案 0 :(得分:1)
您正在将IEnumberable<SampleData.GameDesc>
设置为 LayoutRoot 的 DataContext 。这就是它无法读取名为 LongDescription 的属性的原因。
尝试将单个元素设置为 DataContext :
LayoutRoot.DataContext = filteredData.First();
如果您想显示所有项目而不仅仅是第一项,请使用 ItemsControl 。