一、具體代碼
類代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace WpfApplication1
{
class lei
{
public interface Strategy
{
int calculate(int a, int b);
}
public class Add : Strategy
{
public int calculate(int a, int b)
{
return a + b;
}
}
public class Sub : Strategy
{
public int calculate(int a, int b)
{
return a -b;
}
}
public class Mul : Strategy
{
public int calculate(int a, int b)
{
return a * b;
}
}
public class Div : Strategy
{
public int calculate(int a, int b)
{
return a / b;
}
}
public class Content
{
private Strategy strategy;
public Content(Strategy strategy)
{
this.strategy = strategy;
}
public int calculate(int a, int b, String m)
{
return this.strategy.calculate(a, b);
}
}
}
}
MainWindow.xaml.cs代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.IO;
using System.Windows.Threading;
using Microsoft.Win32;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string path = ".\text1.txt";
public static int right = 0;
public static int Count = 0;
private int t;
int a;
private void button1_Click(object sender, RoutedEventArgs e)
{
StreamWriter baocun1 = File.AppendText("baocun1.txt");
baocun1.WriteLine(textBox1.Text);
baocun1.Close();
StreamWriter baocun2 = File.AppendText("baocun2.txt");
baocun2.WriteLine(textBox2.Text);
baocun2.Close();
StreamWriter baocun3 = File.AppendText("baocun3.txt");
baocun3.WriteLine(textBox3.Text);
baocun3.Close();
textBox6.Text += textBox1.Text + textBox2.Text + textBox3.Text + label1.Content + textBox4.Text + "\n";
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
textBox5.Text = t.ToString();
DispatcherTimer myTimer = new DispatcherTimer();
myTimer.Interval = new TimeSpan(0, 0, 0,1);
myTimer.Tick += new EventHandler(Timer_Tick);
myTimer.Start();
string[] m = new string[100];
m = File.ReadAllLines("baocun1.txt");
textBox1.Text = m[a];
string[] n = new string[100];
n = File.ReadAllLines("baocun2.txt");
textBox2.Text = n[a];
string[] v = new string[100];
v = File.ReadAllLines("baocun3.txt");
textBox3.Text = v[a];
a++;
}
void Timer_Tick(object send, EventArgs e)
{
t = t + 1;
textBox5.Text = t.ToString();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
//調用;
WpfApplication1.lei.Content content = null;
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox3.Text);
string m = textBox2.Text;
switch (m)
{
case "+":
content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Add());
break;
case "-":
content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Sub());
break;
case "*":
content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Mul());
break;
case "/":
content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Div());
break;
default:
break;
}
string answer = content.calculate(a, b, m).ToString();
if (textBox4.Text == answer.ToString())
{
MessageBox.Show("恭喜你,小朋友!回答正确!");
right++;
Count++;
}
else
{
MessageBox.Show("回答錯誤,繼續加油!小朋友");
Count++;
}
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}
private void button4_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("一共用時" + textBox5.Text + "秒");
Window1 win = new Window1();
win.ShowDialog();
}
}
}
Window.xaml.cs代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Window1.xaml 的互動邏輯
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
textBox1.Text = MainWindow.Count.ToString();
textBox2.Text = MainWindow.right.ToString();
textBox3.Text = ((MainWindow.right / (double)(MainWindow.Count)) * 100).ToString() + "%";
}
}
}
二、測試
三、總結
在WPF裡實作電腦程式跟在WinForm裡差不多,就是有些控件的用法有點差別,還有就是沒有時間控件,得自己寫代碼實作,然後就上網搜了搜,很慶幸寫出來了。