PS:这是在公司技术论坛介绍WP7开发时,收集的资料和一个提纲。
public void Request(string url, string postString)
{
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);//创建req
req.Accept = "*/*";//接受任意文件
req.UserAgent = " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)"; // 模拟使用IE在浏览
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16";
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
if ((postString != null & postString.Length > 0) || url.Contains("?"))
{
byte[] postdata = Encoding.UTF8.GetBytes(postString);
req.BeginGetRequestStream(async1 =>
{
using (Stream stream = req.EndGetRequestStream(async1))
stream.Write(postdata, 0, postdata.Length);
req.BeginGetResponse(async2 =>
{
WebResponse rep = req.EndGetResponse(async2);
using (Stream stream = rep.GetResponseStream())
using (StreamReader sr = new StreamReader(stream))
{
string content = sr.ReadToEnd();
}
}, null);
}, null);
}
}
经验证,该方法能够成功使用。
挺喜欢知乎这个网站的,闲暇之余便做了个知乎精选,技术上没什么难度,仅练练手而已。
感兴趣的朋友可以到这里下载 http://115.com/file/be8ujjiq
今天下午的时候,部门经理告诉我,在后面的2-3个月我将去辅助别的部门进行开发工作。主要是因为那个部门的人一直做Delphi的开发,现在需要接触.net和Silverlight,需要一个比较熟悉的人去“指导”一下。
虽然部门经理说是这样说,但我感觉自己还没有足够的信心和能力去指导他们。毕竟我可能面对的是一些老程序员,在他们面前我可能会放不开。同时自己资质尚浅,虽然一直搞.net,但是对于一些较深入的东西,理解的依然不够透彻。我需要通过这2-3个月,来锻炼自己的“领导能力”,同时也更清晰的提高自己的技术水平,在让他们明白和理解的同时,我应该更明白,更理解。
我想成为架构师,但不是每一名程序员都能成为架构师。这是我梦开始的地方!
因为在自己最近半年来一直在做Silverlight相关的项目,同时感觉移动领域的开发是很有前途的,对于微软Windows Phone的前景,个人是比较看好的。于是决定尝试WP7的开发。在仔细考虑之后,选择了数独作为自己的第一个WP7项目。
关于WP7的初学文章,如果有一定的Silverlight(WPF)基础,那么我推荐张崟翻译的WP7开发31日谈http://blog.csdn.net/porscheyin/article/details/5997208
如果是没有做过Silverlight开发的朋友,再看该文章之前,最好先阅读一点Silverlight的相关资料。
现在我们进入正题,关于数独游戏呢,相关的资料请参考http://zh.wikipedia.org/wiki/%E6%95%B0%E7%8B%AC 关于数独的算法,网上也有很多,这不是本文讨论的重点。
首先是程序的主页面,进行游戏难度的选择。该页面名称为GameLevel.xaml,页面主要的xaml代码如下:
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel VerticalAlignment="Top">
<TextBlock Text="请选择一个等级开始游戏:" />
<Button Content="Simple" Margin="0,10,0,10" Click="OnChooseLevel" />
<Button Content="Normal" Margin="0,10,0,10" Click="OnChooseLevel"/>
<Button Content="Hard" Margin="0,10,0,10" Click="OnChooseLevel"/>
<Button Content="God" Margin="0,10,0,10" Click="OnChooseLevel" />
</StackPanel>
</Grid>
在进行UI设计的时候最好参考《UI_Design_and_Interaction_Guide_for_Windows_Phone_7_v2.0》
GameLevel.cs代码如下:
private void OnChooseLevel(object sender, RoutedEventArgs e)
{
string level = string.Empty;
switch (((Button)sender).Content.ToString())
{
case "Simple":
GameInfo.CurLevel = GameLevels.Simple;
break;
case "Normal":
GameInfo.CurLevel = GameLevels.Normal;
break;
case "Hard":
GameInfo.CurLevel = GameLevels.Hard;
break;
case "God":
GameInfo.CurLevel = GameLevels.God;
break;
default:
break;
}
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
在需要进行跨页面传值的时候,我直接将需要传到下一个页面的Level参数存入了GameInfo这个静态类中。
选择难度后,则是进行游戏的主画面:
从左至右分别是Simple,Normal,Hard,God难度模式下的画面。
游戏页面的主要XAML代码MainPage.xaml
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="SudokuSeven" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="TIME:0S" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="5,0,5,0">
<Grid.RowDefinitions>
<RowDefinition Height="491"/>
<RowDefinition Height="116"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderBrush="Blue" BorderThickness="1" Margin="3">
<Grid Name="grid" Grid.Row="0" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
</Grid.ColumnDefinitions>
</Grid>
</Border>
<StackPanel HorizontalAlignment="Center" Grid.Row="1" Orientation="Horizontal" Height="44" VerticalAlignment="Top">
<TextBlock Name="Num1" Text="1" Margin="10,0,10,0" FontSize="32" MouseLeftButtonDown="doSelectedNum" />
<TextBlock Name="Num2" Text="2" Margin="10,0,10,0" FontSize="32" MouseLeftButtonDown="doSelectedNum" />
<TextBlock Name="Num3" Text="3" Margin="10,0,10,0" FontSize="32" MouseLeftButtonDown="doSelectedNum" />
<TextBlock Name="Num4" Text="4" Margin="10,0,10,0" FontSize="32" MouseLeftButtonDown="doSelectedNum" />
<TextBlock Name="Num5" Text="5" Margin="10,0,10,0" FontSize="32" MouseLeftButtonDown="doSelectedNum" />
<TextBlock Name="Num6" Text="6" Margin="10,0,10,0" FontSize="32" MouseLeftButtonDown="doSelectedNum" />
<TextBlock Name="Num7" Text="7" Margin="10,0,10,0" FontSize="32" MouseLeftButtonDown="doSelectedNum" />
<TextBlock Name="Num8" Text="8" Margin="10,0,10,0" FontSize="32" MouseLeftButtonDown="doSelectedNum" />
<TextBlock Name="Num9" Text="9" Margin="10,0,10,0" FontSize="32" MouseLeftButtonDown="doSelectedNum" />
</StackPanel>
</Grid>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/icons/appbar.check.rest.png" Text="Valid" Click="OnValidate"/>
<shell:ApplicationBarIconButton IconUri="/icons/appbar.refresh.rest.png" Text="Restart" Click="OnRestart"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
在生成游戏的时候,本人采用的方法是将正确的数独结果,隐藏掉部分数字的方式。MainPage.cs中的主要代码如下:
初始化游戏的方法
private void InitGame()
{
this.grid.Children.Clear();
grids = new List<TextBlock>();
SudokuMath sm = new SudokuMath();
sm.Intial();
int[,] map = sm.Creat();//得到一个包含正确结果的二维数组
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
GridInfo gridinfo = new GridInfo() { Number = map[i, j], IsSelected = false, X = j - 1, Y = i - 1, IsHidden = false };
TextBlock numberGrid = new TextBlock() { Tag = gridinfo, FontSize = 30, TextAlignment = TextAlignment.Center, Text = map[i, j].ToString() };
numberGrid.MouseLeftButtonDown += new MouseButtonEventHandler(nubmberGrid_MouseLeftButtonDown);
numberGrid.SetValue(Grid.RowProperty, i - 1);
numberGrid.SetValue(Grid.ColumnProperty, j - 1);
this.grid.Children.Add(numberGrid);
grids.Add(numberGrid);
}
}
SetGameLevel();
seconds = 0;
timer.Interval = new TimeSpan(TimeSpan.TicksPerSecond);
timer.Start();
}
隐藏特定位置数字的方法
private void HideNumber(int x, int y)
{
TextBlock grid = FindNumberGrid(x, y);
grid.Text = unknow;
((GridInfo)grid.Tag).IsHidden = true;
grid.Foreground = grid.Foreground = new SolidColorBrush(Colors.Red);
}
private TextBlock FindNumberGrid(int x, int y)
{
foreach (TextBlock grid in grids)
{
GridInfo gridinfo = grid.Tag as GridInfo;
if (gridinfo.X == x && gridinfo.Y == y)
{
return grid;
}
}
return new TextBlock();
}
GridInfo.cs类
public class GridInfo
{
public int Number { get; set; }
public bool IsSelected { get; set; }
public int X { get; set; }
public int Y { get; set; }
public bool IsHidden { get; set; }
}
至此一个简单的WP7应用就完成了。