日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Windows Phone 7 不溫不火學習之《頁面導航》

系統 2203 0

用于Windows Phone 的SilverLight 提供了 PhoneApplicationFrame 和 PhoneApplicationPage類,這兩個類是微軟針對Silverlight for Windows Phone 另外封裝的,它為導航提供了使得。

  PhoneApplicationPage 控件代表了內容相互分離的區段,一個應用程序可以有多個PhoneApplicationPage 。

  PhoneApplicationFrame 扮演了頁面控件容器的角色,對頁面之間的導航提供了便利,一個應用程序有一個獨立的PhoneApplicationFrame。

  Windows Phone 7是通過 使用URI【通用資源標志符(Uniform Resouce Identifier )】映射進行頁面導航的。下面通過 DEMO 來了解一下Windows Phone 7的各種導航的寫法吧。

第一種通過使用NavigationService 進行導航控制,如下代碼:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> NavigationService.Navigate( new Uri( " /Layout/SecondPage.xaml " ,UriKind.Relative));

UriKind 是一個枚舉類型,自帶有三種類型,這里我們使用相對路徑。

第二種通過直接使用控件自帶功能,即使用微軟的HyperLink控件,直接導航,代碼如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < HyperlinkButtonContent = " 點轉 " NavigateUri = " /Layout/SecondPage.xaml " Height = " 30 " HorizontalAlignment = " Left " Margin = " 101,68,0,0 " Name = " hyperlinkButton1 " VerticalAlignment = " Top " Width = " 200 " />

第三種我們可以為我們的URI路徑起一個別名,然后在調用時調用別名即可,可以方便我們管理,使用步驟如下:

a).在App.xaml的文件上注冊Windows.Navigation 命名空間,該空間位于Reference目錄下的Microsoft.Phone組件,代碼如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> xmlns:nav = " clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone "

b).為應用程序添加資源,代碼如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < Application.Resources >
< nav:UriMapperx:Key = " UriMapper " >
< nav:UriMappingUri = " SecondPage " MappedUri = " /Layout/SecondPage.xaml " />

</ nav:UriMapper >
</ Application.Resources >

c).添加完成后,進入 App.xaml.cs 在它的構造函數里面為應用程序的RootFrame下所在的UriMapper 賦值,這用的話我們應用程序就可以通過它去映射我們傳給它的URI 別名。代碼如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> this .RootFrame.UriMapper = Resources[ " UriMapper " ] as UriMapper;

Tip:這里的Resources 是一個字典型數據,鍵值必須跟上面App.xaml 添加資源的UriMapper Key要相對應。

d).注冊好別名之后,我們就可以在程序中如下使用:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> NavigationService.Navigate( new Uri( " SecondPage " ,UriKind.Relative));

或者在HyperLinkButton 中如下使用:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < HyperlinkButtonContent = " 點轉 " NavigateUri = " SecondPage " Height = " 30 " HorizontalAlignment = " Left " Margin = " 101,68,0,0 " Name = " hyperlinkButton1 " VerticalAlignment = " Top " Width = " 200 " />

好了,說完如何通過URI導航到指定Page 頁面,下面來嘗試一下如何通過URI導航傳遞數據吧。

做過Asp.Net 的童鞋相信對地址欄傳值不陌生,比如我現在在博客園寫文章的路徑如下:

后面的“?”跟著的就是相關的參數或者要表達的某些請求,這種寫法很好,又直接。做過WEB的又好理解,不過慶幸的是微軟把這個非常棒的功能加入到Windows phone 7的頁面傳遞中來,這真是一大另人興奮的事,怎么樣呢?就這樣用:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> NavigationService.Navigate( new Uri( " /Layout/SecondPage.xaml?opt=1 " ,UriKind.Relative));

或者

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < HyperlinkButtonContent = " 點轉 " NavigateUri = " /Layout/SecondPage.xaml?opt=1 " Height = " 30 " HorizontalAlignment = " Left " Margin = " 101,68,0,0 " Name = " hyperlinkButton1 " VerticalAlignment = " Top " Width = " 200 " />

簡單明了,不帶一絲拖拉看來WEB開發人員進入Windows Phone 7開發也可以喲。。。呵呵~!!

我們剛才的縮寫也是同樣直接這種URI帶參數的寫法的,同樣還是來到App.xaml頁面,寫法如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < Application.Resources >
< nav:UriMapperx:Key = " UriMapper " >
< nav:UriMappingUri = " SecondPage " MappedUri = " /Layout/SecondPage.xaml " />
< nav:UriMappingUri = " SecondPage/{content} " MappedUri = " /Layout/SecondPage.xaml?name={content} " />
</ nav:UriMapper >
</ Application.Resources >

這里要注意三點:

  • SecondPage/{content}與后面name={content}這兩個content一定要相同
  • content 不可以寫數字0,寫數字0會報運行時錯誤。可以為數字1,但不可以為其他數字。為“1”時編譯器解析正常,但為其他數字時雖不會報運行時錯誤,但編譯器解析出錯,不能夠正確解析傳進來的參數,會直接打印比如SecondPage/{111}在另外的頁面就直接打印{111}。
  • MappedUri 后面“?”的查詢字段區分大小寫。

添加資源完成后,在頁面使用如下方法即可調用縮寫并傳參數:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> NavigationService.Navigate( new Uri( " SecondPage/terry " ,UriKind.Relative));

試試效果吧,在頁面中的按鈕點擊然后跳轉到第二頁,看會不會顯示“terry”,如圖:

Windows Phone 7 不溫不火學習之《頁面導航》

獲取參數跟Asp.net 差不多,見下方代碼:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> if (NavigationContext.QueryString.Count > 0 )
{
UriTextBox.Text
= NavigationContext.QueryString[ " name " ].ToString();
}

  Windows Phone 7 的NavigationContext.QueryString 是不支持傳遞對象的,那么如果我們現在有個對象需要傳遞該如何實現呢?看了Jake Lin 老師的視頻后才知道可以在App 里面定義一個對象屬性,然后這個屬性就可以提供全局訪問。是不是我可以理解成這個屬性其實也可以放在一個公用的靜態類上呢?呵呵,下面看如何實現吧。

步驟一:

聲明一個對象名為Model寫下如何函數:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public class Model
{
public string name{ get ; set ;}
public string file{ get ; set ;}
}

然后在App.xaml.cs 里面將Model 聲明為一個公用的靜態屬性:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public static Class.ModelMyAppModel{ get ; set ;}

余下的功夫就是為其賦值和取值的操作了,點擊跳往下一頁的按鈕時,為App.Model 賦值。在第二頁時取出App.Model的值,代碼編寫見下方:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> private void button1_Click( object sender,RoutedEventArgse)
{
App.MyAppModel
= new Class.Model{name = " terry " ,file = " my " };
NavigationService.Navigate(
new Uri( " SecondPage/terry " ,UriKind.Relative));
}

第二頁Loaded完畢后:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> private void PhoneApplicationPage_Loaded( object sender,RoutedEventArgse)
{
if (NavigationContext.QueryString.Count > 0 )
{
UriTextBox.Text
= NavigationContext.QueryString[ " name " ].ToString();
}
Class.Modelmodel
= App.MyAppModel;

Debug.WriteLine(model.name
+ " ===>>> " + model.file);
}

見輸出日志信息:

我們還可以通過別名傳參的方法,動態的指定頁面跳轉,先看看界面:

Windows Phone 7 不溫不火學習之《頁面導航》

頁面上有三個單選按鈕,然后工程中對應有三個頁面:

選擇后,點擊按鈕將會通過下標動態跳轉到不同的頁面,代碼編寫看下方。

首先,App.xaml的資源添加:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < Application.Resources >
< nav:UriMapperx:Key = " UriMapper " >
< nav:UriMappingUri = " SecondPage " MappedUri = " /Layout/SecondPage.xaml " />
< nav:UriMappingUri = " SecondPage/{content} " MappedUri = " /Layout/SecondPage.xaml?name={content} " />
< nav:UriMappingUri = " page/{number} " MappedUri = " /Layout/Page{number}.xaml " />
</ nav:UriMapper >
</ Application.Resources >

然后,在主頁上聲明一個全局索引,接著編寫如下代碼,即可完成:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> private void firstRb_Checked( object sender,RoutedEventArgse)
{
index
= 1 ;
}

private void secondRb_Checked( object sender,RoutedEventArgse)
{
index
= 2 ;
}

private void threeRb_Checked( object sender,RoutedEventArgse)
{
index
= 3 ;
}

private void chooseButton_Click( object sender,RoutedEventArgse)
{
NavigationService.Navigate(
new Uri( " page/ " + index,UriKind.Relative));
}

頁面導航和數據傳遞的課程學習完畢到這里先,如果你覺得對你有幫助,別忘了推薦一下,謝謝。

------點擊發布,才記起忘了發源碼: 導航DEMO

Windows Phone 7 不溫不火學習之《頁面導航》


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 达拉特旗| 九江县| 阿瓦提县| 天柱县| 阿克陶县| 辽阳市| 黄骅市| 鸡西市| 神农架林区| 枞阳县| 伊吾县| 新兴县| 苗栗县| 阳山县| 吉隆县| 慈溪市| 驻马店市| 长武县| 满城县| 茶陵县| 原阳县| 郴州市| 凤凰县| 邓州市| 通州市| 内乡县| 额济纳旗| 赤城县| 顺义区| 丰原市| 平南县| 永新县| 铁岭市| 贡嘎县| 湖北省| 张掖市| 沈丘县| 视频| 龙川县| 新竹县| 南平市|