-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.cs
114 lines (97 loc) · 2.65 KB
/
AppDelegate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using System.IO;
using ServiceStack.Text;
using System.Text;
namespace TestServiceStackJSON1
{
[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
UIWindow App_Window;
UIWebView Browser;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
App_Window = new UIWindow(UIScreen.MainScreen.Bounds);
var vc = new UIViewController();
var view = new UIView();
var button = UIButton.FromType(UIButtonType.RoundedRect);
button.Frame = new RectangleF(0, 0, 100, 40);
button.SetTitle("Do It", UIControlState.Normal);
button.TouchDown += delegate {
Test_JSON();
};
view.AddSubview(button);
Browser = new UIWebView();
Browser.Frame = new RectangleF(0, 50, UIScreen.MainScreen.Bounds.Width,
UIScreen.MainScreen.Bounds.Height-50);
view.AddSubview(Browser);
vc.View = view;
App_Window.RootViewController = vc;
App_Window.MakeKeyAndVisible();
return true;
}
private void Test_JSON()
{
string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),
"test.json");
var obj = new TestClass();
obj.Prop1 = "Hello World";
try
{
using (var writer = File.CreateText(file))
{
JsonSerializer.SerializeToWriter<TestClass>(obj, writer);
}
}
catch (Exception ex)
{
Error(ex);
return;
}
try
{
using (var reader = File.OpenText(file))
{
obj = JsonSerializer.DeserializeFromReader<TestClass>(reader);
}
HTML(obj.Prop1);
}
catch (Exception ex)
{
Error(ex);
}
}
private void Error(Exception ex)
{
HTML("Error:<pre>" + Concat_Stack_Traces(ex) + "</pre>");
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
private void HTML(string html)
{
Browser.LoadHtmlString(html, new NSUrl("/"));
}
public static string Concat_Stack_Traces(Exception ex)
{
StringBuilder sb = new StringBuilder();
do
{
sb.AppendLine(ex.Message);
string trace = null;
if (string.IsNullOrEmpty(ex.StackTrace))
{
trace = "<No Stack Trace>";
}
else
{
trace = ex.StackTrace;
}
sb.AppendLine(trace);
ex = ex.InnerException;
} while (ex != null);
return sb.ToString();
}
}
}