[UWP]涨姿式UWP源码——适配电脑和手机

上一篇咱们介绍了绘制主界面的MainPage.xaml,本篇则会结合MainPage.xaml.cs来说一讲如何适配电脑和手机这些不一样尺寸的设备。html

同时适配电脑和手机存在几个麻烦的地方:git

  1. 屏幕尺寸差距过大,不太适合以手机为基准,而后在电脑上等比放大。
  2. 手机屏幕小,可是分辨率高。好比Lumia 9502K屏就默认采用400%的比例来显示。
  3. 手机通常默认竖屏。电脑会有16932各类比例,且默认横屏。致使总体布局须要调整。

其余细节讨论能够看我以前写的一些心得:github

http://www.cnblogs.com/manupstairs/p/5143414.html布局

在涨姿式UWP中,经过Page对象的SizeChanged事件来控制界面尺寸变化。有童鞋可能要问,既然都是以屏幕Width为依据变化,为何不在XAML中使用AdaptiveTrigger MinWindowWidth属性。this

        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState >
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="769" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="GridRootLayout.HorizontalAlignment" Value="Left"></Setter>
                        <Setter Target="GridRootLayout.VerticalAlignment" Value="Top"></Setter>
                        <Setter Target="GridRootLayout.Width" Value="320"></Setter>
                        <Setter Target="GridRootLayout.Height" Value="640"></Setter>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

上面代码经过AdaptiveTriggerWidth等于769时,将GridRootLayoutHorizontalAlignmentVerticalAlignmentWidthHeight四个属性从新赋值,确实是官方Sample给出的用法。我以前也介绍过这种用法:spa

http://www.cnblogs.com/manupstairs/p/5267418.html3d

相较而言,SizeChanged的实现显得更为灵活:code

  1. 能够将界面变化赋值的代码封装成一个方法,在多处调用。
  2. 能够有须要计算的复杂条件判断,而不单单是MinWindowWidth这种的值判断。

代码中我提取了一个UpdateLayout方法,在SizeChanged时传递e.NewSize.Width做为参数。以Width为依据,同时判断SelectedItem是否为null,进一步计算页面的布局。另外UpdateLayout方法还会在ViewModel的自定义事件UpdateLayoutEvent被触发时调用。orm

        private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            UpdateLayout(e.NewSize.Width);
        }

        private void UpdateLayout(double newWidth)
        {
            if (newWidth <= 800)
            {
                this.splitView.DisplayMode = SplitViewDisplayMode.Overlay;
                this.borderMiddle.Width = 0;
                if (listViewItems.SelectedItem == null)
                {
                    columnRight.Width = zeroGridLength;
                    columnLeft.Width = oneStarGridLength;
                    columnRightBar.Width = zeroGridLength;
                    columnLeftBar.Width = oneStarGridLength;
                }
                else
                {
                    columnLeft.Width = zeroGridLength;
                    columnRight.Width = oneStarGridLength;
                    columnLeftBar.Width = zeroGridLength;
                    columnRightBar.Width = oneStarGridLength;
                }
            }
            else
            {
                columnLeft.Width = fourStarGridLength;
                columnRight.Width = sixStarGridLength;
                columnLeftBar.Width = fourStarGridLength;
                columnRightBar.Width = sixStarGridLength;
                this.splitView.DisplayMode = SplitViewDisplayMode.CompactOverlay;
                this.borderMiddle.Width = 48;
            }
        }

MainPage.xaml.cs中,咱们还处理了系统Back按钮的事件,这在手机和平板上会起到做用。htm

            SystemNavigationManager.GetForCurrentView().BackRequested += (sender, e) =>
            {
                if (vm.SelectedItem != null)
                {
                    vm.SelectedItem = null;
                    e.Handled = true;
                }
            };

另外须要注意的是,若是要处理手机的状态栏,须要额外的添加引用Windows Mobile Extensions for the UWP

添加以后的引用列表以下图:

特别要注意的是,即便添加了Windows Mobile Extensions for the UWP,在访问Mobile特有的API以前,仍须要经过if判断来避免程序崩溃。这里若是不进行if判断,在PCTablet上运行时就会闪退。

            if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
            {
                StatusBar.GetForCurrentView().BackgroundColor = Colors.Transparent;
                StatusBar.GetForCurrentView().ForegroundColor = Colors.Black;
            }

本篇主要介绍如何经过SizeChanged来实现自适应布局,谢谢能看到这里的各位!

Windows 10 Create Update 411日就要正式推出了,Windows Phone听说又要崛起了……

GitHub源代码地址:

https://github.com/manupstairs/ZhangZiShiRSSRead

Windows Store

https://www.microsoft.com/zh-cn/store/p/%e6%b6%a8%e5%a7%bf%e5%8a%bfuwp/9nblggh3zqd1

相关文章
相关标签/搜索