首先,新建一个xml或者txt文件,比如:Example.txt;
在里面输入内容 “hello,my name is white,who are you?”
在XAML写一个控件TextBlock,命名为text1
在MainPage.xaml.cs 文件中,编写PhoneApplicationPage_Loaded 事件
代码为:
需要首先引入命名空间:
using System.Windows.Resources;
using System.IO;
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
StreamResourceInfo ssi = Application.GetResourceStream(new Uri("CityCode.txt",UriKind.Relative));
using (StreamReader sr = new StreamReader(ssi.Stream))
{
text1.Text = sr.ReadToEnd();
}
}
这样就能成功读取txt文件中的内容了。
效果是: