2013-04-14 166 views
3

我正在構建Windows Phone 8應用程序,該應用程序使用Pivot控件爲不同用戶顯示給定日期的數據。每個PivotItem擁有不同的用戶。Windows Phone - 更改佈局方向更改

目前我的應用程序只支持縱向方向,但我也希望支持橫向方向。這樣做,我希望每個PivotItem不再只顯示一個日期的數據,而是整整一週,因此顯着改變了佈局。

我的第一種方法是使用新佈局導航到新頁面,但是在爲此進行的一些研究中,我瞭解到可能正確/最佳的方法是更改​​DataTemplate。我假設應該在Pivot控件ItemTemplate上。

但是,這個我還沒有能夠讓我的頭腦和工作。因此,我的問題是什麼是在方向更改時更改佈局的最佳方法 - 導航到新頁面或更改DataTemplate - 並且如果要更改Pivot控件的模板,應如何完成此操作?

編輯 - 規範當前樞軸控件

<phone:Pivot x:Name="PivotPlatform" Title="DEMO" ItemsSource="{Binding PivotItems}" FontSize="13.333" > 
    <phone:Pivot.HeaderTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Title}"/> 
     </DataTemplate> 
    </phone:Pivot.HeaderTemplate> 
    <phone:Pivot.ItemTemplate> 
     <DataTemplate> 
      <!-- Controls omitted --> 
     </DataTemplate> 
    </phone:Pivot.ItemTemplate> 
</phone:Pivot><?xml version="1.0" encoding="utf-8"?> 

,我想,我需要做的就是用省略控制提取的DataTemplate,然後在「公正」指定取決於方向所需的DataTemplate 。但是,我似乎可以找到該

+0

到目前爲止你有什麼代碼?絕對**不要**導航到新頁面 - 只需檢測方向更改並應用不同的模板(假設您的數據保持不變)。 –

+0

明白了 - 我會找到一種方法讓這項工作無需導航到新的頁面:)我已經添加了代碼,但是刪除了佈局本身以提高可讀性。 –

回答

6

在頁面資源,同時定義的模板

<phone:PhoneApplicationPage.Resources> 
    <DataTemplate x:Key="DataTemplate1"> 
     <!--DEFINE TEMPLATE HERE--> 
    </DataTemplate> 
    <DataTemplate x:Key="DataTemplate2"> 
     <!--DEFINE TEMPLATE HERE--> 
    </DataTemplate> 
</phone:PhoneApplicationPage.Resources> 

然後定義透視像這樣:

<phone:Pivot x:Name="PivotPlatform" 
       Title="DEMO" 
       FontSize="13.333" 
       ItemsSource="{Binding PivotItems}"> 
     <phone:Pivot.HeaderTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Title}" /> 
      </DataTemplate> 
     </phone:Pivot.HeaderTemplate> 
    </phone:Pivot> 

手樂方向改變事件

<phone:PhoneApplicationPage .... 
         OrientationChanged="PhoneApplicationPage_OrientationChanged" 
         ....> 

通過編程設定的DataTemplate

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e) 
{ 
    if (e.Orientation == PageOrientation.PortraitDown || e.Orientation == PageOrientation.PortraitUp) 
    { 
     PivotPlatform.ItemTemplate = this.Resources["DataTemplate1"] as DataTemplate; 
    } 
    else 
    { 
     PivotPlatform.ItemTemplate = this.Resources["DataTemplate2"] as DataTemplate; 
    } 
} 

這應該工作!

+1

謝謝igrali - 我只需在

1

正確的語法,您可以創建在頁面資源樞軸項新模板,然後處理layoutchange事件:

void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e){if (e.Orientation == PageOrientation.LandscapeLeft || e.Orientation == PageOrientation.LandscapeRight) 
{ 
} 
else 
{ 
}} 
+0

我和你在一起了解事件處理程序並檢測方向。但{{}中設置了什麼指定模板? –