C# 数据模板取决于页面方向,不改变WP7
我需要一个列表框来根据页面方向显示不同的信息,所以开始查找并找到了这个列表框,实现如下:C# 数据模板取决于页面方向,不改变WP7,c#,xaml,windows-phone-7,C#,Xaml,Windows Phone 7,我需要一个列表框来根据页面方向显示不同的信息,所以开始查找并找到了这个列表框,实现如下: <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="OrientationStates"> <VisualState x:Name="Landscape"> <Storyboard> <Object
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="OrientationStates">
<VisualState x:Name="Landscape">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="lista"
Storyboard.TargetProperty="ItemTemplate">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource LandscapeTemplate}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
在参考资料部分中定义了数据模板:
<DataTemplate x:Key="LandscapeTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Name="txtNombre" Text="{Binding Path=nombre}" TextWrapping="Wrap" Padding="0,10,0,0" FontSize="28" VerticalAlignment="Bottom"></TextBlock>
<TextBlock Name="txtSep" Text=" - " TextWrapping="Wrap" Padding="0,10,0,0" FontSize="22" VerticalAlignment="Bottom"></TextBlock>
<TextBlock Name="txtFecha" Text="{Binding Path=fecha_hora}" TextWrapping="Wrap" Padding="0,10,0,0" FontSize="22" VerticalAlignment="Bottom"></TextBlock>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="PortraitTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Name="txtNombre" Text="{Binding Path=nombre}" TextWrapping="Wrap" Padding="0,10,0,0" FontSize="28" VerticalAlignment="Bottom"></TextBlock>
</StackPanel>
</DataTemplate>
然后按如下方式使用VisualStateManager:
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="OrientationStates">
<VisualState x:Name="Landscape">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="lista"
Storyboard.TargetProperty="ItemTemplate">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource LandscapeTemplate}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
我的应用程序的默认方向是纵向,所以我首先将纵向模板指定给我的列表框
<ListBox x:Name="lista"
SelectionChanged="ListBox_SelectionChanged"
ItemTemplate="{StaticResource PortraitTemplate}">
</ListBox>
然后在测试结果时,模板不会改变。我想知道我错过了什么?。或者我还需要添加什么来更改模板
提前谢谢
附加信息:还尝试使用此功能实现我所需的功能最终有效的功能:
在应用程序页面构造函数(在本例中为主页)中:
this.OrientationChanged+=neweventhandler(MainPage\u OrientationChanged);
并添加处理程序方法:
void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if ((e.Orientation & PageOrientation.Landscape) > 0)
{
VisualStateManager.GoToState(this, "Landscape", true);
lista.ItemTemplate = LandscapeTemplate();
lista.ItemsSource = null;
List<StrTutoria> eventos = generarLista(asistiendo);
this.lista.ItemsSource = eventos;
}
else
{
VisualStateManager.GoToState(this, "Portrait", true);
// Here goes code for portrait state
}
}
void主页\u方向已更改(对象发送者,方向更改开发者)
{
如果((如方向和页面方向)大于0)
{
VisualStateManager.GoToState(这是“风景”,真的);
lista.ItemTemplate=LandscapeTemplate();
lista.ItemsSource=null;
列表事件=通用列表(asistiendo);
this.lista.ItemsSource=eventos;
}
其他的
{
VisualStateManager.GoToState(这是“肖像”,true);
//下面是肖像状态的代码
}
}
和LandscapeTemplate()实现:
private DataTemplate LandscapeTemplate()
{
string xaml =
@"<DataTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<StackPanel Orientation=""Horizontal"">
<TextBlock Name=""txtNombre"" Text=""{Binding Path=nombre}"" TextWrapping=""Wrap"" Padding=""0,10,0,0"" FontSize=""28"" VerticalAlignment=""Bottom""></TextBlock>
<TextBlock Name=""txtSep"" Text="" - "" TextWrapping=""Wrap"" Padding=""0,10,0,0"" FontSize=""22"" VerticalAlignment=""Bottom""></TextBlock>
<TextBlock Name=""txtFecha"" Text=""{Binding Path=fecha_hora}"" TextWrapping=""Wrap"" Padding=""0,10,0,0"" FontSize=""22"" VerticalAlignment=""Bottom""></TextBlock>
</StackPanel>
</DataTemplate>";
DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);
return dt;
}
私有数据模板LandscapeTemplate()
{
字符串xaml=
@"
";
DataTemplate dt=(DataTemplate)XamlReader.Load(xaml);
返回dt;
}
解决方案的最后一部分可用页面的基类是LayoutWarePage吗?正如你在回答的最后一行中所指出的,我试图检查,但不确定如何检查。