在uwp初始屏幕设计中,最后在手机上调试,发现ProgressRing及文字都不显示的问题。最后,经过调试发现是它们的位置超出屏幕的显示范围了,所以才会看不到。下面我将已实际的例子,说明我自己的手机上计算位置方法。
以我的手机Lumia640xl为例,手机的分辨率是1280×720。所以使用// 检索启动屏幕图像的窗口坐标。
splashImageRect = splash.ImageLocation;
获取的值:X=0,Y=0,Width = 720,Height = 1280。经过测试,坐标系的原点在屏幕左上角,以下面的方法计算,
void PositionRing() { splashProgressRing.SetValue(Canvas.LeftProperty, splashImageRect.X + (splashImageRect.Width * 0.5) - (splashProgressRing.Width * 0.5)); splashProgressRing.SetValue(Canvas.TopProperty, (splashImageRect.Y + splashImageRect.Height + splashImageRect.Height * 0.1)); }
得出的结果是splashProgressRing的x= 336(splashProgressRing.Width = 48),splashProgressRing的Y= 1480;而实际手机屏幕的有效像素是多少呢?
double density = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
使用这个方法可以获取,手机屏幕有效像素和分辨率的一个转换值(我个人的理解),调试得到的这个值为1.75(不同手机这个值可能会不一样,没试过,好像是和显示缩放有关系)。计算一下,得到有效像素约等于411×731,所以在屏幕上肯定看不到(336,1428)。知道这个换算方法以后,就很好解决了,只需要把控件按照现有的坐标系计算坐标就可以了(x最大值411,y最大值731)。但是为了适配不同分辨率的手机,因此,还是建议尽量不要直接给出具体的坐标,而是有一定的参照。我使用下面的算式,使得ProgressRing的位置正常了。(但是由于手里只有这一种型号的手机,所以,不肯定是否完美解决了问题)
void PositionRing() { //desktop if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily.Equals("windows.desktop", StringComparison.CurrentCultureIgnoreCase)) { splashProgressRing.SetValue(Canvas.LeftProperty, splashImageRect.X + (splashImageRect.Width * 0.5) - (splashProgressRing.Width * 0.5)); splashProgressRing.SetValue(Canvas.TopProperty, (splashImageRect.Y + splashImageRect.Height + splashImageRect.Height * 0.1)); } //mobile else if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily.Equals("windows.mobile", StringComparison.CurrentCultureIgnoreCase)) { double density = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; splashProgressRing.SetValue(Canvas.LeftProperty, (splashImageRect.Width / density) * 0.5 - splashProgressRing.Width * 0.5); splashProgressRing.SetValue(Canvas.TopProperty, (splashImageRect.Height/density)*0.5 +(splashImageRect.Height/density)*0.2); } }
目前为止,也就这么多了,以后发现问题了再更新~
本文由 Kevin Yang 发布在 Kevin Yang,转载此文请保持文章完整性,并请附上文章来源(Kevin Yang)及本页链接。
原文链接:https://www.yzj0308.com/uwp%e6%89%8b%e6%9c%ba%e5%90%af%e5%8a%a8%e5%b1%8f%e5%b9%95%e6%8e%a7%e4%bb%b6%e4%bd%8d%e7%bd%ae%e8%ae%a1%e7%ae%97/
原文链接:https://www.yzj0308.com/uwp%e6%89%8b%e6%9c%ba%e5%90%af%e5%8a%a8%e5%b1%8f%e5%b9%95%e6%8e%a7%e4%bb%b6%e4%bd%8d%e7%bd%ae%e8%ae%a1%e7%ae%97/