win8 VisualStateGroup

WinRT控件与屏幕方向支持
本文探讨了Windows 8应用中WinRT控件的使用,并详细介绍了如何通过XAML实现不同屏幕方向的支持,包括全屏横纵比、填充及侧边栏等多种状态。

This post is part of my Windows 8 / WInRT exploration and notes.  This time, I’m digesting the WinRT Controls sample.

This sample has quite a few more features to digest. There are six different control scenarios. This application supports landscape, portrait, and snapped orientations. As a result, this post will get longer than my average post.

Screen Orientation Support

Most of the orientation support is provided by the underlying framework. Your application must  respond to the ViewStateChanged event:

Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().ViewStateChanged += 

 

    new TypedEventHandler

 

    <Windows.UI.ViewManagement.ApplicationView, Windows.UI.ViewManagement.ApplicationViewStateChangedEventArgs>

 

    (MainPage_ViewStateChanged);

 

When you receive this event, you instruct the Visual State Manager to transition to a particular state:

 

VisualStateManager.GoToState(this, 

 

    ApplicationView.Value.ToString() + DisplayProperties.ResolutionScale.ToString(), false);

 

 

 

The States must be defined in your main window’s XAML description.  Your XAML must include a <VisualStateManager.VisualStateGroups> node.  This element contains a sub-element called VisualStateGroup.  The VisualStateGroup element contains a number of VisualState elements. These elements are named and those names are the states used in the GoToState call above.  Here’s en edited version from the Controls sample:

<VisualStateManager.VisualStateGroups>

 

     <VisualStateGroup x:Name="OrientationStates">

 

          <VisualState x:Name="FullScreenLandscapeScale100Percent"/>

 

          <VisualState x:Name="FilledScale100Percent">

 

         <!-- elided -->

 

          </VisualState>

 

          <VisualState x:Name="FullScreenPortraitScale100Percent">

 

         <!-- elided -->

 

          </VisualState>

 

          <VisualState x:Name="SnappedScale100Percent">

 

         <!-- elided -->

 

          </VisualState>

 

          <VisualState x:Name="FullScreenLandscapeScale140Percent">

 

         <!-- elided -->

 

          </VisualState>

 

          <VisualState x:Name="FilledScale140Percent"/>

 

          <VisualState x:Name="FullScreenPortraitScale140Percent">

 

         <!-- elided -->

 

          </VisualState>

 

          <VisualState x:Name="SnappedScale140Percent"/>

 

     </VisualStateGroup>

 

</VisualStateManager.VisualStateGroups>

 

This application supports eight different visual states.  See the documentation for the ViewStateChanged event for more details.

Once the app is loaded, it runs mostly like a familiar WPF or Silverlight application. There is a control for the list of scenarios.  When you select a different scenario, the main window loads the input and output panes for that scenario.

With only one exception, the scenarios are entirely coded in XAML. Rather than put a lot of declarative XAML in the enntry, I’ll point out the big picture concept:

If you have been writing WPF or Silverlight applications at all, the XAML used for the WinRT controls will be completetly familiar to you. Run the application, and you can likely guess exactly what the XAML looks like.

So far, Windows 8 apps look a lot like WPF or Silverlight apps (if you are a C# or VB.NET developer).

转载于:https://www.cnblogs.com/win-and-first/archive/2012/07/06/2579642.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值