实验5:数据流练习
通过本实验,熟悉和掌握Encoding类、FileStream类以及MemoryStream类的使用。
1、创建一个WPF应用程序项目
2、将App.xaml中的Application.Resources节内容改为
3、修改MainWindow.xaml及代码隐藏类
MainWindow.xaml
MainWindow.xaml.cs主要内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Button oldButton = new Button();
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Button btn = e.Source as Button;
btn.Foreground = Brushes.Black;
oldButton.Foreground = Brushes.Black;
oldButton = btn;
frame1.Source = new Uri(btn.Tag.ToString(), UriKind.Relative);
}
}
}
Paeg1.xaml
Page1.xaml.cs
Paeg2.xaml
Page2.xaml.cs
Paeg3.xaml
Page3.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1.Examples
{
/// <summary>
/// Page3.xaml 的交互逻辑
/// </summary>
public partial class Page3 : Page
{
public Page3()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
string str = "abcd、中国";
textBlock1.Text = string.Format("写入的数据:{0}\n",str);
Byte[] data = Encoding.UTF8.GetBytes(str);
using (MemoryStream ms=new MemoryStream() )
{
ms.Write(data,0,data.Length);
byte[] bytes = new byte[data.Length];
ms.Position = 0;
int n = ms.Read(bytes, 0, bytes.Length);
string s = Encoding.UTF8.GetString(bytes, 0, n);
textBlock1.Text += string.Format("读出的数据:{0}\n",s);
}
}
}
}
运行结果
通过本实验,熟悉和掌握了Encoding类、FileStream类以及MemoryStream
的使用