在學習Prism框架之前,我預先寫了一個非常簡單的計算器解決方案。代碼如下:
1
static
void Main(
string[] args)
2 {
3 while ( true)
4 {
5 string input = Console.ReadLine();
6
7 if (CommandTypes.Contains(input))
8 {
9 int index = Array.IndexOf(CommandTypes, input);
10
11 int x = int.Parse(Console.ReadLine());
12 int y = int.Parse(Console.ReadLine());
13
14 int result = funs[index](x, y);
15
16 Console.WriteLine(result);
17 }
18 else
19 {
20 Console.WriteLine( " Mistake! ");
21 }
22 }
23 }
24 static int Add( int x, int y)
25 {
26 return x + y;
27 }
28 static int Sub( int x, int y)
29 {
30 return x - y;
31 }
32 static int Mul( int x, int y)
33 {
34 return x * y;
35 }
36 static int Div( int x, int y)
37 {
38 return x / y;
39 }
40
41 static string[] CommandTypes = { " add ", " sub ", " mul ", " div " };
42 static Func< int, int, int>[] funs = { Add, Sub, Mul, Div };
43 }
2 {
3 while ( true)
4 {
5 string input = Console.ReadLine();
6
7 if (CommandTypes.Contains(input))
8 {
9 int index = Array.IndexOf(CommandTypes, input);
10
11 int x = int.Parse(Console.ReadLine());
12 int y = int.Parse(Console.ReadLine());
13
14 int result = funs[index](x, y);
15
16 Console.WriteLine(result);
17 }
18 else
19 {
20 Console.WriteLine( " Mistake! ");
21 }
22 }
23 }
24 static int Add( int x, int y)
25 {
26 return x + y;
27 }
28 static int Sub( int x, int y)
29 {
30 return x - y;
31 }
32 static int Mul( int x, int y)
33 {
34 return x * y;
35 }
36 static int Div( int x, int y)
37 {
38 return x / y;
39 }
40
41 static string[] CommandTypes = { " add ", " sub ", " mul ", " div " };
42 static Func< int, int, int>[] funs = { Add, Sub, Mul, Div };
43 }
在這里,主要是以學習Prism框架為目的。以上的功能,使用如上的,面向過程的方法來實現,很清晰易懂。不過,既然是面向對象的編程。而且在之后的章節中將要應用到Prism框架及其設計思想和模式。所以在本節中,我們還需要先對上面的代碼重構一下。 感興趣的朋友們,可以點擊下載。
我先說明一下,各位下載下去的代碼,並沒有使用到Prism框架中的任何東西。它只是我為了學習Prism框架而寫的一解決方案,算是前期的准備工作。我將在下一章中開始詳細記錄我是如何學習Prism框架的。希望各路朋友們多多指教。