//---draws the signature---
private void DrawSignature(string value) {
_lines = new List>();
//---split into individual lines---
string[] lines = value.Split('\n');
//---for each individual line---
for (int i = 0; i <= lines.Length - 2; i++) {
//---split into individual points---
string[] ps = lines[i].Split('|');
_points = new List();
//---for each point---
for (int j = 0; j <= ps.Length - 2; j++) {
string[] xy = ps[j].Split(',');
_points.Add(new Point(
(Convert.ToDouble(xy[0])),
Convert.ToDouble(xy[1])));
}
_lines.Add(_points);
}
//---draws the signature---
for (int line = 0; line <= _lines.Count - 1; line++) {
_points = (List)_lines[line];
for (int i = 1; i <= _points.Count - 1; i++) {
Line sline = new Line() {
X1 = _points[i - 1].X,
Y1 = _points[i - 1].Y,
X2 = _points[i].X,
Y2 = _points[i].Y,
StrokeThickness = 2,
Stroke = new SolidColorBrush(Colors.Black)
};
SigPad.Children.Add(sline);
}
}
}
Code the MouseLeftButtonDown
event handler for the Load
button so that the series of signature lines can be loaded from isolated storage:
//---Load button---
void btnLoad_MouseLeftButtonDown(
object sender, MouseButtonEventArgs e) {
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream("IsoStoreFile.txt",
FileMode.Open, isoStore);
StreamReader reader = new StreamReader(isoStream);
//---read all lines from the file---
string lines = reader.ReadToEnd();
//---draws the signature---
DrawSignature(lines);
txtStatus.Text = "Signature loaded!";
reader.Close();
isoStream.Close();
}
Code the MouseLeftButtonDown
event handler for the Clear
button so that the signature can be cleared from the drawing pad:
//---Clear button---
void btnClear_MouseLeftButtonDown(
object sender, MouseButtonEventArgs e) {
_lines = new List>();
_points = new List();
//---iteratively clear all the signature lines---
int totalChild = SigPad.Children.Count - 2;
for (int i = 0; i <= totalChild; i++) {
SigPad.Children.RemoveAt(1);
}
txtStatus.Text = "Signature cleared!";
}
Press F5 to test the application. You can now sign and then save the signature. You can also load the saved signature (see Figure 19-72).
Figure 19-72
Saving the Signature to Web Services
One of these signatures isn't a lot of good unless you can send it to a Web Service. This section shows you how to do that.
Using the same project created in the previous section, add a new Web Site project to the current solution (see Figure 19-73).
Figure 19-73
Select ASP.NET Web Site, and name the project SignatureWebSite
.
Add a new Web Service item to the Web Site project, and use its default name of WebService.asmx
(see Figure 19-74).
Figure 19-74
In the WebService.cs
file, add the following lines:
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Web.Script.Services;
///
/// Summary description for WebService ///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
...
...
}
Define the following two web methods:
[WebMethod]
public bool SaveSignature(string value) {
try {
File.WriteAllText(Server.MapPath(".") +
@"\Signature.txt", value);
return true;
} catch (Exception ex) {
return false;
}
}
[WebMethod]
public string GetSignature() {
string fileContents;
fileContents = File.ReadAllText(
Server.MapPath(".") + @"\Signature.txt");
return fileContents;
}
The SaveSignature()
function saves the values of the signature into a text file. The GetSignature()
function reads the content of the text file and returns the content to the caller.
In the Signature
project, add a service reference (see Figure 19-75).
Figure 19-75
Click the Discover button and then OK (see Figure 19-76).
Figure 19-76
In Page.xaml.cs
, modify the Save button as follows:
//---Save button---
void btnSave_MouseLeftButtonDown(
object sender, MouseButtonEventArgs e) {
try {
ServiceReference1.WebServiceSoapClient ws = new
Signature.ServiceReference1.WebServiceSoapClient();
//---wire up the event handler when the web service returns---
ws.SaveSignatureCompleted += new
EventHandler(ws_SaveSignatureCompleted);
//---calls the web service method---
ws.SaveSignatureAsync(GetSignatureLines());
} catch (Exception ex) {
txtStatus.Text = ex.ToString();
}
}
Here, you send the signature to the Web service asynchronously. When the Web Service call returns, the ws_SaveSignatureCompleted
event handler will be called.
Читать дальше