编程语言
首页 > 编程语言> > javascript – WinJs中的视图状态更改事件在哪里?

javascript – WinJs中的视图状态更改事件在哪里?

作者:互联网

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

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

解决方法:

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

 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; 
    }

如果您查看Microsoft Visual Studio Express 2012 RC for Windows 8 template for Javascript,您将在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();
    }
},

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

标签:winjs,javascript,windows-8
来源: https://codeday.me/bug/20190901/1786008.html