3

在 Windows 8 中,当视图状态从捕捉到填充到全屏时;

如何捕获这些事件并使用 WinJs 响应它们?

4

1 回答 1

5

请参阅博客文章,了解如何捕获视图状态更改。基本上,建议归结为捕获调整大小事件,然后检查视图状态,如下所示:

 window.addEventListener("resize", onResize);


function onResize() { 
    // Update view for the new window size 
    updateView(); 
} 

function updateView() { 
    // Query for the current view state 
    var myViewState = Windows.UI.ViewManagement.ApplicationView.value; 

    var viewStates = Windows.UI.ViewManagement.ApplicationViewState; 
    var statusText; 

    // Assign text according to view state 
    switch (myViewState) { 
        case viewStates.snapped: 
            statusText = "This app is snapped!"; 
            break; 
        case viewStates.filled: 
            statusText = "This app is in filled state!"; 
            break; 
        case viewStates.fullScreenLandscape: 
            statusText = "This app is full screen landscape!"; 
            break; 
        case viewStates.fullScreenPortrait: 
            statusText = "This app is full screen portrait!"; 
            break; 
        default: 
            statusText = "Error: Invalid view state returned."; 
            break; 
    }

如果您查看适用于 Javascript 的 Microsoft Visual Studio Express 2012 RC for Windows 8 模板,您将在 navigator.js 中看到如下内容:

window.onresize = this._resized.bind(this);

_resized: function(args) {
    if (this.pageControl && this.pageControl.updateLayout) {
        this.pageControl.updateLayout.call(this.pageControl, this.pageElement, appView.value, this.lastViewstate);
    }
    this.lastViewstate = appView.value;
},

//*page*.js

updateLayout: function (element, viewState, lastViewState) {
     /// <param name="element" domElement="true" />
     var listView = element.querySelector(".itemslist").winControl;
     if (lastViewState !== viewState) {
          if (lastViewState === appViewState.snapped || viewState === appViewState.snapped) {
               var handler = function (e) {
               listView.removeEventListener("contentanimating", handler, false);
               e.preventDefault();
          }
          listView.addEventListener("contentanimating", handler, false);
          var firstVisible = listView.indexOfFirstVisible;
          this._initializeLayout(listView, viewState);
          if (firstVisible >= 0 && listView.itemDataSource.list.length > 0) {
                listView.indexOfFirstVisible = firstVisible;
           }
       }
   }
},

_initializeLayout: function (listView, viewState) {

    if (viewState === appViewState.snapped) {
        listView.layout = new ui.ListLayout();
    } else {
        listView.layout = new ui.GridLayout();
    }
},

根据这个msdn 页面,您还可以在视图状态更改影响 CSS 中指定的 CSS 属性或布局时使用媒体查询。对于所有其他更改,应使用 onResize。

于 2012-09-05T04:36:24.177 回答