zoukankan      html  css  js  c++  java
  • 手把手教你做“迷你浏览器”

    关注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 右键选中属性

     

    把下面的属性设置为

     

    属性值

     

    Texthttp://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("http://"))

     

    s_site = "http://"+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("http://")) 
    21. site = "http://"+ site; 
    22. webBrowser1.Navigate(newUri(site, UriKind.Absolute)); 
    23. Xaml: 
    24. <phone:PhoneApplicationPage 
    25. x:Class="MiniBrowser.MainPage" 
    26. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    27. xmlns:x="http://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="http://schemas.microsoft.com/expression/blend/2008" 
    31. xmlns:mc="http://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="http://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的教程来的,后来试着自己做了做,发现一个问题,在编辑框直接输入域名的时候程序会出错,但是加上了前缀“http://”就不会了,于是我把代码加上了一个判断

     

     

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

     

    当然这里没有加上大写判断。第一个程序就写这么多了。总的来说感觉还不错。

  • 相关阅读:
    领域驱动设计实践,精通业务,面向对象编程,面条编程,过程编程
    日志聚合与全链路监控
    Spring Security OAuth2 之token 和 refresh token
    Web开发技术发展历程(笔记)
    JDBC 数据处理 总结
    Idea创建Maven多模块项目
    中国 / 省市区县 / 四级联动 / 地址选择器(京东商城地址选择)
    左膀mongostat,右臂mongotop——MongoDB的监控之道
    制定项目章程
    挣值分析
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3137543.html
Copyright © 2011-2022 走看看