自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MetroGridHelper:WP7設(shè)計師與開發(fā)人員的得力助手

譯文
移動開發(fā)
如果最終外觀設(shè)計方案能夠與Windows Phone上現(xiàn)有的性能計數(shù)器相似,那么肯定會大受歡迎——在調(diào)試時,我們能夠?qū)⑦@套網(wǎng)格重疊覆蓋在整個應(yīng)用程序框架上,使其成為獨立而完整的全局顯示方案。

作者簡介:Jeff Wilcox,微軟高級軟件開發(fā)工程師,不久前,調(diào)往微軟Windows Azure組,負(fù)責(zé)微軟云技術(shù)的開源項目。Jeff Wilcox曾負(fù)責(zé)開發(fā)了Windows Phone平臺的Foursqaure客戶端,并參與了無數(shù)Silverlight工具包、Windows Phone 7.0、7.1開發(fā)包,Windows Phone用戶界面控制、幫助,以及許多其他重要項目。

[[79339]] 
Jeff Wilcox

【51CTO譯文】作為沉浸于開發(fā)行業(yè)多年的老手,大家一定對這些話題并不陌生:整理頁面邊距、對齊圖像內(nèi)容以及讓自己的應(yīng)用程序更美觀。隨著軟件消費者中“外貌協(xié)會”成員的比重不斷上升,用戶界面不夠搶眼的應(yīng)用幾乎已經(jīng)無法在市場上占得一席之地。

不久前,我有幸參加了Windows Phone設(shè)計團(tuán)隊舉辦的“美觀開發(fā)空間”活動。我要由衷地贊嘆,這是一次真正的創(chuàng)意空間交流活動,到處是有趣的參與者、舒緩的音樂以及為市場創(chuàng)造出更好、更漂亮的應(yīng)用程序的熱烈渴求。

在活動中我一直坐在兩位用戶體驗設(shè)計師Corrina與Arturo身邊,與他們討論設(shè)計工作中的原則性取向;很偶然地,我們的話題轉(zhuǎn)移到一副簡潔而相當(dāng)美觀的網(wǎng)格圖上,這也正是二位在Windows Phone平臺上開發(fā)的主要焦點。圖像由多個紅色正方形構(gòu)成,每個方形為25x25像素,兩個方形之間相隔12像素,也就是說每個獨立單元都擁有寬高為24像素的反襯背景。(還是那句話,Metro風(fēng)格要求邊框長度始終為12的整數(shù))

設(shè)計將使用典型的Photoshop層來容納這些方形,或者是在應(yīng)用程序頁面之上插入XAML借以完成圖形對齊、網(wǎng)格設(shè)計及位置調(diào)整等等。

我的想法是:如果最終外觀設(shè)計方案能夠與Windows Phone上現(xiàn)有的性能計數(shù)器相似,那么肯定會大受歡迎——在調(diào)試時,我們能夠?qū)⑦@套網(wǎng)格重疊覆蓋在整個應(yīng)用程序框架上,使其成為獨立而完整的全局顯示方案。就在活動過程當(dāng)中,我即興用代碼將自己的想法表達(dá)了出來,下面請大家分享我的成果。

要使用這款計數(shù)器,我們只需打開App.xaml.cs文件(這里囊括了其它多款性能計數(shù)器),并將其添加進(jìn)來。如果大家只是打算簡單設(shè)計一下,那么我建議各位直接將其啟用,這樣一來我們就能夠讓它作用于實機(jī)及模擬器中的應(yīng)用程序。之所以要把它與模擬器關(guān)聯(lián)起來,是因為我們能夠?qū)⒆罱K顯示效果通過截圖與朋友及家人分享,并聆聽他們在圖像位置方面提出的意見。

  1. // Show graphics profiling information while debugging.  
  2. if (System.Diagnostics.Debugger.IsAttached)  
  3. {  
  4.     // Display the current frame rate counters.  
  5.     Application.Current.Host.Settings.EnableFrameRateCounter = true;  
  6.    
  7.     // Display the metro grid helper.  
  8.     MetroGridHelper.IsVisible = true

以下是這段簡單的代碼在與小型應(yīng)用程序協(xié)作時顯示出的效果:

效果

在這個例子中,我遇到了Windows Phone設(shè)計中的經(jīng)典“bug”:文本信息區(qū)塊之一在插入過程中未能正確顯示應(yīng)有的Metro風(fēng)格。也就是說,該區(qū)塊的左側(cè)邊距為“0”,而不是Metro要求的12像素,這使得對應(yīng)文字內(nèi)容比其它字體更靠左。通過上圖中正方形的對比,相信大家能更清晰地理解我遇到的問題,字體錯位現(xiàn)象十分明顯。

如果大家不喜歡默認(rèn)的紅色以及~0.15的不透明度,我還為不透明度及顏色添加了簡單的靜態(tài)屬性設(shè)置選項,希望能讓各位獲得自己理想中的方形單元效果。在運(yùn)行過程中,可見屬性不會顯示出來,但請大家注意,只要網(wǎng)格本身仍然存在于可視化元素列表當(dāng)中,就會占用對應(yīng)的性能資源(因此請務(wù)必在應(yīng)用程序的發(fā)布版本中把網(wǎng)格去掉,否則會造成毫無意義的資源浪費)。

源代碼

我已經(jīng)在NuGet上發(fā)布過源文件——這應(yīng)該是大家在自己的項目中使用這款小成品的最佳方式。如果今后我做出任何修正或添加某些功能,各位也將會在NuGet網(wǎng)站上及時找到最新版本。

◆確保自己已經(jīng)安裝了NuGet(http://www.nuget.org/)

◆使用控制臺或軟件包管理器安裝該軟件包,我把它命名為MetroGridHelper

PM> Install-Package MetroGridHelper

當(dāng)然,大家也可以將這部分源代碼在項目中整理成一個全新的文件,MetroGridHelper.cs:

  1. // (c) Copyright Microsoft Corporation.  
  2. // This source is subject to the Microsoft Public License (Ms-PL).  
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.  
  4. // All other rights reserved.  
  5.  
  6. using System.Collections.Generic;  
  7. using System.Diagnostics;  
  8. using System.Windows.Controls;  
  9. using System.Windows.Media;  
  10. using System.Windows.Shapes;  
  11.  
  12. namespace System.Windows  
  13. {  
  14.     /// <summary>  
  15.     /// A utility class that overlays a designer-friendly grid on top of the  
  16.     /// application frame, for use similar to the performance counters in  
  17.     /// App.xaml.cs. The color and opacity are configurable. The grid contains  
  18.     /// a number of squares that are 24x24, offset with 12px gutters, and all  
  19.     /// 24px away from the edge of the device.  
  20.     /// </summary>  
  21.     public static class MetroGridHelper  
  22.     {  
  23.         private static bool _visible;  
  24.         private static double _opacity = 0.15;  
  25.         private static Color _color = Colors.Red;  
  26.         private static List<Rectangle> _squares;  
  27.         private static Grid _grid;  
  28.    
  29.         /// <summary>  
  30.         /// Gets or sets a value indicating whether the designer grid is  
  31.         /// visible on top of the application's frame.  
  32.         /// </summary>  
  33.         public static bool IsVisible  
  34.         {  
  35.             get 
  36.             {  
  37.                 return _visible;  
  38.             }  
  39.             set 
  40.             {  
  41.                 _visible = value;  
  42.                 UpdateGrid();  
  43.             }  
  44.         }  
  45.    
  46.         /// <summary>  
  47.         /// Gets or sets the color to use for the grid's squares.  
  48.         /// </summary>  
  49.         public static Color Color  
  50.         {  
  51.             get { return _color; }  
  52.             set 
  53.             {  
  54.                 _color = value;  
  55.                 UpdateGrid();  
  56.             }  
  57.         }  
  58.    
  59.         /// <summary>  
  60.         /// Gets or sets a value indicating the opacity for the grid's squares.  
  61.         /// </summary>  
  62.         public static double Opacity  
  63.         {  
  64.             get { return _opacity; }  
  65.             set 
  66.             {  
  67.                 _opacity = value;  
  68.                 UpdateGrid();  
  69.             }  
  70.         }  
  71.    
  72.         /// <summary>  
  73.         /// Updates the grid (if it already has been created) or initializes it  
  74.         /// otherwise.  
  75.         /// </summary>  
  76.         private static void UpdateGrid()  
  77.         {  
  78.             if (_squares != null)  
  79.             {  
  80.                 var brush = new SolidColorBrush(_color);  
  81.                 foreach (var square in _squares)  
  82.                 {  
  83.                     square.Fill = brush;  
  84.                 }  
  85.                 if (_grid != null)  
  86.                 {  
  87.                     _grid.Visibility = _visible ? Visibility.Visible : Visibility.Collapsed;  
  88.                     _grid.Opacity = _opacity;  
  89.                 }  
  90.             }  
  91.             else 
  92.             {  
  93.                 BuildGrid();  
  94.             }  
  95.         }  
  96.    
  97.         /// <summary>  
  98.         /// Builds the grid.  
  99.         /// </summary>  
  100.         private static void BuildGrid()  
  101.         {  
  102.             _squares = new List<Rectangle>();  
  103.    
  104.             var frame = Application.Current.RootVisual as Frame;  
  105.             if (frame == null || VisualTreeHelper.GetChildrenCount(frame) == 0)  
  106.             {  
  107.                 Deployment.Current.Dispatcher.BeginInvoke(BuildGrid);  
  108.                 return;  
  109.             }  
  110.    
  111.             var child = VisualTreeHelper.GetChild(frame, 0);  
  112.             var childAsBorder = child as Border;  
  113.             var childAsGrid = child as Grid;  
  114.             if (childAsBorder != null)  
  115.             {  
  116.                 // Not a pretty way to control the root visual, but I did not  
  117.                 // want to implement using a popup.  
  118.                 var content = childAsBorder.Child;  
  119.                 if (content == null)  
  120.                 {  
  121.                     Deployment.Current.Dispatcher.BeginInvoke(BuildGrid);  
  122.                     return;  
  123.                 }  
  124.                 childAsBorder.Child = null;  
  125.                 Deployment.Current.Dispatcher.BeginInvoke(() =>  
  126.                 {  
  127.                     Grid newGrid = new Grid();  
  128.                     childAsBorder.Child = newGrid;  
  129.                     newGrid.Children.Add(content);  
  130.                     PrepareGrid(frame, newGrid);  
  131.                 });  
  132.             }  
  133.             else if (childAsGrid != null)  
  134.             {  
  135.                 PrepareGrid(frame, childAsGrid);  
  136.             }  
  137.             else 
  138.             {  
  139.                 Debug.WriteLine("Dear developer:");  
  140.                 Debug.WriteLine("Unfortunately the design overlay feature requires that the root frame visual");  
  141.                 Debug.WriteLine("be a Border or a Grid. So the overlay grid just isn't going to happen.");  
  142.                 return;  
  143.             }  
  144.         }  
  145.    
  146.         /// <summary>  
  147.         /// Does the actual work of preparing the grid once the parent frame is  
  148.         /// in the visual tree and we have a Grid instance to work with for  
  149.         /// placing the chilren.  
  150.         /// </summary>  
  151.         /// <param name="frame">The phone application frame.</param>  
  152.         /// <param name="parent">The parent grid to insert the sub-grid into.</param>  
  153.         private static void PrepareGrid(Frame frame, Grid parent)  
  154.         {  
  155.             var brush = new SolidColorBrush(_color);  
  156.    
  157.             _grid = new Grid();  
  158.             _grid.IsHitTestVisible = false;  
  159.    
  160.             // To support both orientations, unfortunately more visuals need to  
  161.             // be used. An alternate implementation would be to react to the  
  162.             // orientation change event and re-draw/remove squares.  
  163.             double width = frame.ActualWidth;  
  164.             double height = frame.ActualHeight;  
  165.             double max = Math.Max(width, height);  
  166.    
  167.             for (int x = 24; x < /*width*/ max; x += 37)  
  168.             {  
  169.                 for (int y = 24; y < /*height*/ max; y += 37)  
  170.                 {  
  171.                     var rect = new Rectangle  
  172.                     {  
  173.                         Width = 25,  
  174.                         Height = 25,  
  175.                         VerticalAlignment = System.Windows.VerticalAlignment.Top,  
  176.                         HorizontalAlignment = System.Windows.HorizontalAlignment.Left,  
  177.                         Margin = new Thickness(x, y, 0, 0),  
  178.                         IsHitTestVisible = false,  
  179.                         Fill = brush,  
  180.                     };  
  181.                     _grid.Children.Add(rect);  
  182.                     _squares.Add(rect);  
  183.                 }  
  184.             }  
  185.    
  186.             _grid.Visibility = _visible ? Visibility.Visible : Visibility.Collapsed;  
  187.             _grid.Opacity = _opacity;  
  188.    
  189.             // For performance reasons a single surface should ideally be used  
  190.             // for the grid.  
  191.             _grid.CacheMode = new BitmapCache();  
  192.    
  193.             // Places the grid into the visual tree. It is never removed once  
  194.             // being added.  
  195.             parent.Children.Add(_grid);  
  196.         }  
  197.     }  

 

原文鏈接http://www.jeff.wilcox.name/2011/10/metrogridhelper/    核子可樂 譯

責(zé)任編輯:王曉東 來源: 51CTO.com
相關(guān)推薦

2019-09-01 23:48:59

開發(fā)代碼編程

2024-12-20 08:10:00

2013-03-26 13:45:34

開發(fā)人員設(shè)計師創(chuàng)業(yè)

2010-08-16 09:21:35

Windows Pho

2011-04-30 16:56:45

Windows PhoiOS

2024-02-06 09:53:45

Pythonget()函數(shù)Dictionary

2024-09-27 17:06:13

2015-07-28 16:38:56

App移動開發(fā)

2023-06-01 08:17:37

UX開發(fā)

2010-04-16 10:49:38

2016-01-25 09:32:37

容器網(wǎng)絡(luò)SDN

2010-08-09 16:09:25

2012-05-30 15:15:42

ibmdw

2023-03-15 07:12:53

企業(yè)開發(fā)人員提供商

2009-12-11 14:50:14

Visual Basi

2009-11-23 20:07:51

ibmdw開發(fā)

2021-02-19 09:33:01

kubernetesJAVA服務(wù)

2021-04-15 09:42:21

程序開發(fā)軟件開發(fā)

2016-03-09 12:11:33

Web開發(fā)人員簡單步驟
點贊
收藏

51CTO技術(shù)棧公眾號