閱讀579 返回首頁    go 阿裏雲 go 技術社區[雲棲]


手把手教你做“迷你瀏覽器”

關注wp很久了,一直都想加入到wp開發的陣營中來,今天終於有了時間開始自己的wp開發之旅。下麵是我的第一個wp7應用迷你瀏覽器

首先打開Microsoft Visual Studio 2010 Express for Windows Phone

開始新建項目

 

選擇Silverlight for Windows Phone 然後選擇 Windows PhoneApplication

我們把項目為起名為:MiniBrowser 點擊確定會出現下麵的窗體

 

我們選擇 Windows Phone OS 7.1 

按下確定這樣子工程就創建好了。下麵開始我們的設計了。

1.設置標題的屬性,選擇我的應用程序

右鍵選中屬性

 

Text屬性的值修改為“我的第一個Windows Phone 程序

選中頁麵名稱右鍵選中屬性

 

Text值修改為“迷你瀏覽器“

可以看到設計圖變為

 

在迷你瀏覽器的下方添加一個TextBox控件

 

選中TextBox 右鍵選中屬性

把下麵的屬性設置為

屬性值

Texthttps://www.wpdever.com

HeightAuto

WidthAuto

HorizontalAlignmentStretch

VerticalAlignmentTop

TextBox右邊添加一個Button控件並把屬性設為

屬性值

ContentGo

HeightAuto

WidthAuto

HorizontalAlignmentRight

VerticalAlignmentTop

TextBox控件的下方區域添加一個WebBrowser 控件,並填充滿下方區域

添加完控件之後就完成了設計

下麵是設計的圖

 

雙擊按鈕為按鈕添加事件處理

在處理事件中添加如下代碼

string s_site = textBox1.Text;

if (!s_site.Contains("https://"))

s_site = "https://"+s_site;

webBrowser1.Navigate(newUri(s_site, UriKind.Absolute));

這樣整個工作就完成了。運行來看看

 

通過調整,我們看看橫版的效果

附上代碼

C#


  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using Microsoft.Phone.Controls; 
  13. namespace MiniBrowser 
  14. publicpartialclassMainPage : PhoneApplicationPage 
  15. // 構造函數 
  16. publicMainPage() 
  17. InitializeComponent(); 
  18. privatevoid button1_Click(objectsender, RoutedEventArgs e) 
  19. stringsite = textBox1.Text; 
  20. if(!site.Contains("https://")) 
  21. site = "https://"+ site; 
  22. webBrowser1.Navigate(newUri(site, UriKind.Absolute)); 
  23. Xaml: 
  24. <phone:PhoneApplicationPage 
  25. x:Class="MiniBrowser.MainPage" 
  26. xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  27. xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
  28. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  29. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  30. xmlns:d="https://schemas.microsoft.com/expression/blend/2008" 
  31. xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" 
  32. mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  33. FontFamily="{StaticResourcePhoneFontFamilyNormal}" 
  34. FontSize="{StaticResourcePhoneFontSizeNormal}" 
  35. Foreground="{StaticResourcePhoneForegroundBrush}" 
  36. SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" 
  37. shell:SystemTray.IsVisible="True"
  38. <!--LayoutRoot 是包含所有頁麵內容的根網格--> 
  39. <Grid x:Name="LayoutRoot" Background="Transparent"
  40. <Grid.RowDefinitions> 
  41. <RowDefinition Height="173"/> 
  42. <RowDefinition Height="595*"/> 
  43. </Grid.RowDefinitions> 
  44. <!--TitlePanel 包含應用程序的名稱和頁標題--> 
  45. <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"
  46. <TextBlock x:Name="ApplicationTitle" Text="我的第一個Windows Phone 程序" Style="{StaticResource PhoneTextNormalStyle}"/> 
  47. <TextBlock x:Name="PageTitle" Text="迷你瀏覽器" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  48. </StackPanel> 
  49. <!--ContentPanel - 在此處放置其他內容--> 
  50. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"
  51. <TextBox Height="Auto" HorizontalAlignment="Stretch" Margin="0,0,120,0" Name="textBox1" Text="https://www.wpdever.com/" VerticalAlignment="Top" /> 
  52. <Button Content="Go" Height="Auto" HorizontalAlignment="Right" Name="button1" VerticalAlignment="Top" 
  53. Width="Auto" Click="button1_Click"/> 
  54. <phone:WebBrowser HorizontalAlignment="Stretch" Margin="0,84,0,0" Name="webBrowser1" VerticalAlignment="Stretch" 
  55. Height="Auto" Width="Auto" /> 
  56. </Grid> 
  57. </Grid> 
  58. </phone:PhoneApplicationPage> 

總結:

這是我的第一個wp程序,剛開始寫的時候是參考msdn的教程來的,後來試著自己做了做,發現一個問題,在編輯框直接輸入域名的時候程序會出錯,但是加上了前綴“https://”就不會了,於是我把代碼加上了一個判斷

 


  1. string s_site = textBox1.Text; 
  2. if (!s_site.Contains("https://")) 
  3. s_site = "https://"+s_site; 
  4. webBrowser1.Navigate(newUri(s_site, UriKind.Absolute)); 

 

當然這裏沒有加上大寫判斷。第一個程序就寫這麼多了。總的來說感覺還不錯。

最後更新:2017-04-03 18:52:12

  上一篇:go 大話數據結構之二:算法
  下一篇:go 最棒的10款MySQL GUI工具