计算类的封装

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. mysql参数,蛮全的

    网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步设置参考,我们需要根据自己的情况进行配置优化,好的做法是MySQL服务器稳定运行了一 ...

  2. 在express中使用Mongoose连接MongoDB

    为何要学Mongoose? Mongoose是MongoDB的一个对象模型工具,封装了MongoDB对文档的的一些增删改查等常用方法,让NodeJS操作Mongodb数据库变得更加灵活简单. 0.安装 ...

  3. windows下安装和配置mongoDB

    上次在mac下安装和配置了mongodb,这次在windows下也尝试安装和配置mongodb. 1.首先下载mongodb压缩包,下载后解压到D盘或E盘.如下: 2.配置环境变量:桌面—计算机右键— ...

  4. 使用git Rebase让历史变得清晰

    当多人协作开发一个分支时,历史记录通常如下方左图所示,比较凌乱.如果希望能像右图那样呈线性提交,就需要学习git rebase的用法. “Merge branch”提交的产生 我们的工作流程是:修改代 ...

  5. ecshop

    if($cat_id == '205'){ $smarty->display('cat1.dwt', $cache_id); }elseif($cat_id == '2'){ $smarty-& ...

  6. toggle笔记

    <!DOCTYPE html> <!-- saved from url=(0040)http://v3.bootcss.com/examples/carousel/ --> & ...

  7. 借助fastjson 实体对象转map

    private Map<String, Object> object2Map(Object object){ JSONObject jsonObject = (JSONObject) JS ...

  8. Spring 3.x jar 包详解 与 依赖关系(转)

    以下的内容我会持续更新(当然是我有新发现的时候); 以下内容是我在网上搜索.整理.修改的而成的内容.由于很多内容都是转载了,无法追溯到源头,因此无法一一对原作者进行道谢. 这几天,我查阅大量的官方的文 ...

  9. AWK只打印某个域后的所有域

    如转载请指明(博客http: yangzhigang cublog cn).前言:有时我们需要将某个域之后的所有域打印出来,而且每个记录(行)的域的个数也不一定,所以用$4,$5,… $n,… $(N ...

  10. MVC部署 - 错误集锦

    一.MVC部署后直接显示文件列表,路由未生效,效果如下: 处理方法为:Web.Config修改一下配置: <system.webServer> <validation validat ...