参考书:《 visual C# 从入门到精通》
第一部分 Visual C# 和Visual Studio 2015概述
第5章 使用复合赋值和循环语句
5.1 使用复合赋值操作符
*=
/=
%=
+=
-=
如 answer+=42
,等效于answer=answer+42
。
5.2 使用while 语句
如:
int i=0;
while(i<10){
Console.WriteLine(i);
}
下面我们尝试创建一个空白应用,实现一个简单的文本文件查看器的功能。这个项目还是有点难度的。书上都没有说明FileOpenPicker窗口的用法,所以我废了不少的劲去探索FileOpenPicker窗口的用法。需要注意一些细节,但由于我还处于入门阶段,现在是说不明白一些地方的所以然来。
- 首先是拖控件,一个Button,两个TextBox。布局如图就行了。
- 添加Button控件的Click时间的处理方法
openFileClick
。同时注意在MainPage.xaml.cs
源代码的using
指令下添加:using Windows.Storage;//手动添加 using Windows.Storage.Pickers;//手动添加
。 - 我直接把代码放上来好了。注意
openFileClick
方法的返回类型的前面添加一个关键字async
。 - 开始调试,点击
Open File
,会出现一个文件选取对话框,找到我们本项目的C#源代码MainPage.xaml.cs点击“打开”即可。会出现下图的效果。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;//手动添加
using Windows.Storage.Pickers;//手动添加
// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板
namespace C_5_2
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void openFileClick(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.List;
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".cs");
StorageFile file = await picker.PickSingleFileAsync();
pathShow.Text = file.Path.ToString();
var stringopenFile = await file.OpenAsync(FileAccessMode.Read);
var inputStream = stringopenFile.GetInputStreamAt(0);
TextReader reader = new StreamReader(inputStream.AsStreamForRead());
displayData(reader);
}
private void displayData(TextReader reader)
{
sourceShow.Text = "";
string line = reader.ReadLine();
while (line != null)
{
sourceShow.Text += line + "\r\n";
line = reader.ReadLine();
}
reader.Dispose();//释放与文件关联的资源并关闭文件
}
}
}
5.3 编写for语句
如下代码:
for(int i=0;i<10;++i){
Console.WriteLine(i);
}
5.4 编写do语句
如下:
int i=0;
do
{
Console.WriteLine(i);
++i;
}
while(i<10);
break
和continue
:break
跳出循环,continue
结束当前循环立即开始下一次循环。
下面我又新建一个空白应用,展示将一个数字转换成八进制的步骤,效果如图:
源代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板
namespace c_5_4
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void stepsClick(object sender, RoutedEventArgs e)
{
int num = int.Parse(numberText.Text);
stepsText.Text = "";
string current = "";
do
{
int nextDigit = num % 8;
num /= 8;
int digitCode = '0' + nextDigit;
char digit = Convert.ToChar(digitCode);
current = digit + current;
stepsText.Text += current + "\r\n";
} while (num != 0);
}
}
}