首先在解决方案里新建一个类库,然后在解决方案里新建三个项目,WPF,WEB,WinForm,但是这三个项目都需要一个计算类进行计算,那么就在新建的类库Calculator里面放一个Calculat.cs类,以便于三个项目共用,调用的时候需要引用一下,下面会有图解。

在类库引用Calculat.cs:

WPF+WEB+WinForm->>表现层共用类-LMLPHP

引用后:

WPF+WEB+WinForm->>表现层共用类-LMLPHP

共用类Calculat.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace Calculator
 {
     public class Calculat
     {
         public abstract class Calculator    //抽象类 Calculator
         {
             public abstract double Cal(double a, double b); //抽象方法Cal
         }
         public class Add : Calculator      //派生类Add继承抽象类Calculator
         {
             public override double Cal(double a, double b)//并重写了抽象方法Cal
             {
                 ;
                 result = a + b;
                 return result;
             }
         }
         public class Sub : Calculator
         {
             public override double Cal(double a, double b)
             {
                 ;
                 result = a - b;
                 return result;
             }
         }
         public class Mul : Calculator
         {
             public override double Cal(double a, double b)
             {
                 ;
                 result = a * b;
                 return result;
             }
         }
         public class Div : Calculator
         {
             public override double Cal(double a, double b)
             {
                 ;
                 result = a / b;
                 return result;
             }
         }
         public class Context           //上下文
         {
             private Calculator calculate = null;//实例化一个基类的引用对象
             public Context(Calculator _cal)//_cal为派生类的一个对象
             {
                 this.calculate = _cal;    //把派生类的对象赋给基类的引用对象
             }
             public double Cal(double a, double b, String symbol)
             {
                 return this.calculate.Cal(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;

 namespace WPF
 {
     /// <summary>
     /// MainWindow.xaml 的交互逻辑
     /// </summary>
     public partial class MainWindow : Window
     {
         public MainWindow()
         {
             InitializeComponent();
         }
         ;
         ;
         public static String AddSymbol = "+";  //加法
         public static String SubSymbol = "-";  //减法
         public static String MulSymbol = "*";  //乘法
         public static String DivSymbol = "/";  //除法
         private void textBox3_KeyDown(object sender, KeyEventArgs e)
         {
             double a = double.Parse(textBox1.Text); //用来存第一个数
             string symbol = comboBox1.Text;          //用来存运算符
             double b = double.Parse(textBox2.Text); //用来存第二个数
             Calculator.Calculat.Context contex = null;  //上下文
             if (symbol.Equals(AddSymbol))           //若为加号
             {
                 contex = new Calculator.Calculat.Context(new Calculator.Calculat.Add());    //加法策略
             }
             else if (symbol.Equals(SubSymbol))      //如果减号
             {
                 contex = new Calculator.Calculat.Context(new Calculator.Calculat.Sub());    //减法策略
             }
             else if (symbol.Equals(MulSymbol))      //若为乘号
             {
                 contex = new Calculator.Calculat.Context(new Calculator.Calculat.Mul());    //乘法策略
             }
             else if (symbol.Equals(DivSymbol))      //若为除号
             {
                 contex = new Calculator.Calculat.Context(new Calculator.Calculat.Div());    //除法策略
             }
             string answer = contex.Cal(a, b, symbol).ToString();  //用answer来存计算出来的答案,此时已经计算出a,b两个数的运算结果。
             if (e.Key == Key.Enter)              //回车操作
             {
                 string result = textBox1.Text + comboBox1.Text + textBox2.Text;//把运算式子存在result里面
                 Count++;  //出题总数加一
                 if (textBox3.Text == answer) //如果输入答案与计算出的answer相等
                 {
                     MessageBox.Show("回答正确!");  //弹出回答正确
                     listBox1.Items.Add(result + "=" + textBox3.Text.Trim() + "√");//并把运算式子存在listbox里
                     Right++; //答对题数加一
                 }
                 else //如果答错
                 {
                     MessageBox.Show("答题错误!"); //弹出答题错误
                     listBox1.Items.Add(result + "=" + textBox3.Text.Trim() + "×");//同样把运算式子放在listbox
                 }
                 label2.Content = ).PadRight(, , ) + "%";//统计正确率
                 textBox1.Clear();//把文本框清空,进行下一次出题
                 textBox2.Clear();
                 textBox3.Clear();
             }
         }
     }
 }

WPF+WEB+WinForm-&gt;&gt;表现层共用类-LMLPHP

出题答题:

WPF+WEB+WinForm-&gt;&gt;表现层共用类-LMLPHP

统计:

WPF+WEB+WinForm-&gt;&gt;表现层共用类-LMLPHP

感觉WPF的界面会比WEB、WinForm要美观一些。

04-15 02:37