Windows phone 7 根据页面方向更改数据模板

Windows phone 7 根据页面方向更改数据模板,windows-phone-7,orientation,datatemplate,Windows Phone 7,Orientation,Datatemplate,我在XAML中有两个listBox ItemTemplate。但是我无法使用页面的orientationChanged事件来更改它以更改DataTemplate。 代码如下: protected override void OnOrientationChanged(OrientationChangedEventArgs e) { if (e.Orientation == PageOrientation.Landscape || e.Orientat

我在XAML中有两个listBox ItemTemplate。但是我无法使用页面的orientationChanged事件来更改它以更改DataTemplate。 代码如下:

protected override void OnOrientationChanged(OrientationChangedEventArgs e)
    {
        if (e.Orientation == PageOrientation.Landscape ||
            e.Orientation == PageOrientation.LandscapeLeft ||
            e.Orientation == PageOrientation.LandscapeRight)
        {
            this.HeadLineListBox.ItemTemplate = (DataTemplate)this.Resources["L_headerTemplate"];
        }

        else if (e.Orientation == PageOrientation.Portrait ||
                 e.Orientation == PageOrientation.PortraitDown ||
                 e.Orientation == PageOrientation.PortraitUp)
        {
            this.HeadLineListBox.ItemTemplate = (DataTemplate)this.Resources["P_headerTemplate"];
        }

        base.OnOrientationChanged(e);
    }
当我第一次进入页面时,如果方向是纵向的,即使我改变了方向,它也会一直显示纵向数据模板。所以当我第一次进入页面时,它是横向的。有人可以帮助我吗


附言:我使用了这里的post方式:但它仍然不起作用。

不幸的是,我认为这是行不通的。将项目添加到列表框时,将应用ItemTemplate。您可以使用以下代码段和您自己的单独模板来测试这一点:

// Constructor
public MainPage()
{
    InitializeComponent();
    Loaded += (sender, e) =>
    {
        DispatcherTimer t = new DispatcherTimer();
        t.Interval = TimeSpan.FromSeconds(5);
        t.Tick += (sender2, e2) =>
        {
            MyListBox.Items.Add(this.Orientation.ToString());
        };
        t.Start();
    };
}
效果很有趣。在我的示例中,当我旋转仿真器时,以下输出被写入列表框:

THIS IS THE PORTRAIT TEMPLATE: PortraitUp
THIS IS THE LANDSCAPE TEMPLATE: LandscapeLeft 
THIS IS THE LANDSCAPE TEMPLATE: LandscapeRight 
THIS IS THE LANDSCAPE TEMPLATE: LandscapeLeft 
THIS IS THE PORTRAIT TEMPLATE: PortraitUp
过去对我有效的另一种方法是使用两个完全不同的布局管理器——一个为纵向布局优化,另一个为横向布局优化——并根据方向的更改切换每个布局的可见性/不透明度。我以前使用过这种技术,它提供了良好的可混合性和良好的性能(如果您的页面不太复杂的话)。我仍在努力寻找一种“最佳”的方法,但至少我知道这种方法是有效的


/chris

在分配新的itemtemplate以应用更新的itemtemplate之后,需要再次绑定itemsource-

 if (e.Orientation.ToString().Contains("Portrait"))

   lstData.ItemTemplate =

     (DataTemplate)this.Resources["listPortrait"];

 else

   lstData.ItemTemplate =

     (DataTemplate)this.Resources["listLandscape"];

 lstData.ItemsSource = null;

 lstData.ItemsSource = ((Products)this.Resources["productCollection"]).DataCollection;

我已经尝试了这里提到的方法,我也没有工作。我已使用上述技术根据页面的方向更改布局。是OrientationChanged事件未触发还是datatemplate未被更新?我在OrientationChanged事件中设置了一个断点。它在方向更改时确实触发。但页面的工作方式与预期不同。加载页面时是否触发此事件?您是从另一个页面进入此页面,还是第一个加载?如果不是第一个加载前一页所支持的方向?您使用的是哪个版本的工具?加载页面时没有触发。这是第一次加载,不是来自其他页面。我正在使用7.1 beta2 toolsThanks进行共享。这是一种可行的方法。但我的页面有点复杂,所以性能不太好。我想您可能会遇到问题,要么编写一个模板,对这两个都适用横向和纵向,或在方向更改时重新加载列表框。前者是最佳解决方案,我几乎不会推荐第二种。祝你好运-一定要在这里发帖,让我们知道你是如何解决这个问题的。我在代码之后找到了另一种方法:this.headlineistbox.ItemTemplate=(DataTemplate)this.Resources[“P_headerTemplate”];我重新加载绑定到列表的数据。这真的很有效。嗯,你知道这不是最好的方法。有人可能会有更好的方法。如果你知道的话,请告诉我。是的-那会有效的,尽管(正如我在之前的评论中指出的)我不推荐这种方法。但是,如果它对你有用,并且你对应用程序的性能还满意,那就继续吧!这是一种方法。但如果你有更好的方法,请发回。