计算类的封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace wpfone1
{ interface operater1
{ int calculate(int a, int b); } class Add : operater1
{
public int calculate(int a, int b)
{
return a + b; }
} class Sub : operater1
{
public int calculate(int a, int b)
{
return a - b;
}
} class Mul : operater1
{
public int calculate(int a, int b)
{
return a * b;
}
} class Div : operater1
{ public int calculate(int a, int b)
{ if (b == 0)
{ throw new Exception("除数不能为零!"); } else
{ return a / b; } } }
}

用策咯模式实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace wpfone1
{
public class Clacuter
{
private operater1 oper1; public Clacuter(string aSS)
{ switch (aSS)
{ case "+": oper1 = new Add(); break; case "-": oper1 = new Sub(); break; case "*": oper1 = new Mul(); break; case "/": oper1 = new Div(); break; } } public int Calculation(int a, int b)
{ return oper1.calculate(a, b); }
}
}

写入类以及清除题库的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace wpfone1
{
class Writes
{
public void inscribe(string a, string b)
{ StreamWriter aaa = new StreamWriter(a, true);
aaa.WriteLine(b);
aaa.Close(); }
public void cleanup(string c, string d, string e)
{
StreamWriter ddd = new StreamWriter(c);
ddd.WriteLine(" ");
ddd.Close();
StreamWriter aaa = new StreamWriter(d);
aaa.WriteLine("");
aaa.Close();
StreamWriter fff = new StreamWriter(e);
fff.WriteLine("");
fff.Close();
}
}
}

window1的设计以及代码

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; namespace wpfone1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void button1_Click(object sender, RoutedEventArgs e)//写入题
{
Writes writ = new Writes();
string fnm = @"one";
string text1 = this.textBox1.Text;
writ.inscribe(fnm, text1);
string fnmm = @"tow";
string text2 = this.textBox2.Text;
writ.inscribe(fnmm, text2);
string fnm1 = @"fuhao";
string text3 = this.comboBox1.Text;
writ.inscribe(fnm1, text3);
textBox1.Clear();
textBox2.Clear();
} private void button2_Click(object sender, RoutedEventArgs e)
{
string a = @"tow";
string b = @"one";
string c = @"fuhao";
Writes clean = new Writes();
clean.cleanup(a, b, c);
} private void button3_Click(object sender, RoutedEventArgs e)//开始做题
{
Window2 wind = new Window2();
wind.ShowDialog(); }
}
}

Window2的设计及代码

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;
using System.IO; namespace wpfone1
{
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private int i = 1;
public static int count;
public static int right;
private void button1_Click(object sender, RoutedEventArgs e)
{
duti();
} private void textBox4_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Clacuter clacuter = new Clacuter(textBox2.Text);
int B = clacuter.Calculation(int.Parse(textBox1.Text), int.Parse(textBox3.Text));
if (textBox4.Text == B.ToString())
{ MessageBox.Show("回答正确!");
right++; } else
{ MessageBox.Show("回答错误!"); }
count++;
textBox4.Clear();
duti(); } }
public void duti()
{
string[] line = File.ReadAllLines("one");
if (i < line.Length)
{
textBox1.Text = line[i];
string[] lines = File.ReadAllLines("tow");
textBox3.Text = lines[i];
string[] lin = File.ReadAllLines("fuhao");
textBox2.Text = lin[i]; }
i++;
if (i == line.Length + 1)
{
MessageBox.Show("你的题做完了,可以去休息一下了!");
Window3 win3 = new Window3();
win3.ShowDialog();
}
} private void button2_Click(object sender, RoutedEventArgs e)
{
this.Close();
Window3 win3 = new Window3();
win3.ShowDialog(); }
}
}

window3的设计及代码

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 wpfone1
{
/// <summary>
/// Window3.xaml 的交互逻辑
/// </summary>
public partial class Window3 : Window
{
public Window3()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
textBox1.Text = Window2.right.ToString();
textBox2.Text = (Window2.count - Window2.right).ToString();
textBox3.Text = ((Window2.right / (double)(Window2.count)) * 100).ToString() + "%";
} private void button1_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}

测试

总结;

在这里实现感觉跟在windowform中差不太多只有很少的一部分代码有点不一样,用法也有些少许不同!

总的感觉就是在窗体的设计中比较容易操作,感觉比在windowform中更容易些!

WPF中实现的更多相关文章

  1. 在WPF中使用依赖注入的方式创建视图

    在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...

  2. MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息

    MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...

  3. MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信

    MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...

  4. MVVM设计模式和WPF中的实现(四)事件绑定

    MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  5. MVVM模式解析和在WPF中的实现(三)命令绑定

    MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  6. MVVM模式和在WPF中的实现(二)数据绑定

    MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  7. MVVM模式和在WPF中的实现(一)MVVM模式简介

    MVVM模式解析和在WPF中的实现(一) MVVM模式简介 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在 ...

  8. 【WPF】 Timer与 dispatcherTimer 在wpf中你应该用哪个?

    源:Roboby 1.timer或重复生成timer事件,dispatchertimer是集成到队列中的一个时钟.2.dispatchertimer更适合在wpf中访问UI线程上的元素 3.Dispa ...

  9. 在WPF中使用WinForm控件方法

    1.      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2.      在要使用WinForm控 ...

  10. 【WPF】WPF中的List<T>和ObservableCollection<T>

    在WPF中 控件绑定数据源时,数据源建议采用 ObservableCollection<T>集合 ObservableCollection<T> 类:表示一个动态数据集合,在添 ...

随机推荐

  1. 《linux内核设计与实现》读书笔记第一、二章

    第一章 Linux内核简介 1.1 Unix的历史 1971年,Unix被移植到PDP-11型机中. 1973年,Unix操作系统用C语言改写——为Unix系统的广泛移植铺平了道路. 1977年,伯克 ...

  2. 移动web开发--meta 之 viewport

    常用的 viewport meta 如下: 1 <meta name="viewport" content="width=device-width,initial- ...

  3. 使用NSTimer过程中最大的两个坑

    坑1. retain cycle问题. 在一个对象中使用循环执行的nstimer时,若希望在对象的dealloc方法中释放这个nstimer,结局会让你很失望. 这个timer会导致你的对象根本不会被 ...

  4. Java高级之类结构的认识

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! Java体系包括,各种版本的虚拟机,可执行文件,各种第三方类库,Java API类库,Java语言 ...

  5. key_value 类型配置文件的解析

    #include <iostream> #include <string> #include <stdint.h> #include <map> #in ...

  6. Ajax如何解决跨域问题

    如果需要从不同的服务器(不同域名)上获取数据就需要使用跨域 HTTP 请求. 跨域请求在网页上非常常见.很多网页从不同服务器上载入 CSS, 图片,Js脚本等. 在现代浏览器中,为了数据的安全,所有请 ...

  7. [LeetCode] Scramble String(树的问题最易用递归)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  8. [LeetCode] Decode Ways(DP)

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  9. LINQ之select方法选择多个字段

    单个字段: var list1 = list.Select(field1 => field1.CouponID).ToList(); 多个字段: var list1 = list.Select( ...

  10. ASP.NET关于Login控件使用 (转)

    分类: C# 2011-02-21 10:38 4599人阅读 评论(0) 收藏 举报 loginasp.netstringurlserverbutton 今天上网找了一些关于Login控件的使用资料 ...