第一个wp应用程序:迷你浏览器

移动开发
这是我的第一个wp程序,刚开始写的时候是参考msdn的教程来的,后来试着自己做了做,发现一个问题,在编辑框直接输入域名的时候程序会出错,但是加上了前缀“http://”就不会了,希望与大家共同学习交流。

关注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)); 

 

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

 

责任编辑:闫佳明 来源: wpdever
相关推荐

2023-05-19 08:49:58

SQLAlchemy数据库

2010-01-08 12:14:44

ibmdwAndroid

2011-06-08 10:24:38

Windows Pho 应用程序

2013-01-11 14:45:43

iOS开发移动应用iPhone

2011-06-08 10:01:36

Windows Pho 应用程序

2011-06-14 15:32:26

Android视频教程

2011-04-02 10:08:21

webOS开发应用程序

2021-04-03 12:31:48

Python开发数据科学

2011-11-17 10:14:52

浏览器应用程序Web App

2011-01-24 07:24:48

Visual Stud

2021-04-07 13:38:27

Django项目视图

2012-02-08 11:15:38

HibernateJava

2010-07-30 14:58:06

Flex应用

2011-05-11 10:58:39

iOS

2013-10-30 22:10:28

Clouda程序

2018-08-22 17:32:45

2012-05-25 15:20:38

XNA

2009-06-26 16:07:43

MyEclipse开发Hibernate程序

2009-05-27 08:54:15

浏览器平台Chrome

2012-03-15 21:34:06

Windows8
点赞
收藏

51CTO技术栈公众号