//---fired when the user clicks on the Signature pad---
void SigPad_MouseLeftButtonDown(
object sender, MouseButtonEventArgs e) {
//---record that the mouse left button is pressed---
MouseDown = true;
//---create a new instance of _points and _lines to
// record all the points drawn---
_points = new List();
//---save the current point for later use---
_previouspoint = e.GetPosition(SigPad);
//---add the point---
_points.Add(_previouspoint);
}
The MouseLeftButtonUp
event is fired when the user releases the left mouse button. You interpret that as the end of the signature signing process. Code the MouseLeftButtonUp
event handler of SigPad
as follows:
//---fired when the user let go of the left mouse button---
void SigPad_MouseLeftButtonUp(
object sender, MouseButtonEventArgs e) {
//---user has let go of the left mouse button---
MouseDown = false;
//---add the list of points to the current line---
_lines.Add(_points);
}
The MouseMove
event is fired continuously when the user moves the mouse. Here, you draw a line connecting the previous point with the current point. Code the MouseMove
event handler of SigPad
as follows:
//---fired when the left mouse button is moved---
void SigPad_MouseMove(object sender, MouseEventArgs e) {
//---if left mouse button is pressed...---
if (MouseDown) {
//---add the current point---
var currentPoint = e.GetPosition(SigPad);
_points.Add(currentPoint);
//---draws a line connecting the previous
// point and the current point---
Line line = new Line() {
X1 = _previouspoint.X,
Y1 = _previouspoint.Y,
X2 = currentPoint.X,
Y2 = currentPoint.Y,
StrokeThickness = 2,
Stroke = new SolidColorBrush(Colors.Black)
};
//---add the line to the signature pad---
SigPad.Children.Add(line);
//---saves the current point for later use---
_previouspoint = currentPoint;
}
}
Press F5 to test the application. Use your mouse to draw on the web page (see Figure 19-70).
Figure 19-70
Saving the Signature to Isolated Storage
This section explains how to store the coordinates of the signature using isolated storage. This technique is useful if you need to persist information on the client side, such as backing up the signature that the user has signed.
Using the same project created in the previous section, add the following highlighted code to Page.xaml
:
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
Canvas.Left="8" Canvas.Top="9" Background="#FFF4F60C">
Stroke="#FF000000" StrokeThickness="3"/>
Canvas.Left="315" Canvas.Top="168">
Stroke="#FF000000" Fill="#FFE6EBFF"
RadiusX="3" RadiusY="3" StrokeThickness="3"/>
TextWrapping="Wrap" Canvas.Left="32"
Canvas.Top="1" Text ="Save"/>
Canvas.Left="214" Canvas.Top="168">
Stroke="#FF000000" Fill="#FFE6EBFF"
RadiusX="3" RadiusY="3" StrokeThickness="3"/>
Canvas.Left="30" Canvas.Top="1" Text="Load"/>
Canvas.Left="113" Canvas.Top="168">
Fill="#FFE6EBFF" RadiusX="3" RadiusY="3"
StrokeThickness="3"/>
Canvas.Left="30" Canvas.Top="1" Text="Clear"/>
TextWrapping="Wrap" Canvas.Left="8" Canvas.Top="198"
OpacityMask="#FF000000" x:Name="txtStatus"/>
Page.xaml
should now look like Figure 19-71.
Figure 19-71
In Page.xaml.cs
, import the following namespaces:
using System.IO.IsolatedStorage;
using System.IO;
Add the following lines to the Page() constructor:
public Page() {
InitializeComponent();
//---wire up the event handlers---
SigPad.MouseLeftButtonDown += new
MouseButtonEventHandler(SigPad_MouseLeftButtonDown);
SigPad.MouseLeftButtonUp += new
MouseButtonEventHandler(SigPad_MouseLeftButtonUp);
SigPad.MouseMove += new
MouseEventHandler(SigPad_MouseMove);
//---wire up the event handlers---
btnSave.MouseLeftButtonDown += new
MouseButtonEventHandler(btnSave_MouseLeftButtonDown);
btnLoad.MouseLeftButtonDown += new
MouseButtonEventHandler(btnLoad_MouseLeftButtonDown);
btnClear.MouseLeftButtonDown += new
MouseButtonEventHandler(btnClear_MouseLeftButtonDown);
}
Define the GetSignatureLines()
function so that the coordinates of the signature can be converted from a List object to a string:
//---returns the signature as a series of lines---
private string GetSignatureLines() {
System.Text.StringBuilder sb = new
System.Text.StringBuilder();
//---for each line---
for (int i = 0; i <= _lines.Count - 1; i++) {
//---for each point---
foreach (Point pt in _lines[i]) {
sb.Append(pt.X + "," + pt.Y + "|");
}
sb.Append("\n");
}
return sb.ToString();
}
Code the MouseLeftButtonDown
event handler for the Save button so that the signature can be saved to isolated storage:
//---Save button---
void btnSave_MouseLeftButtonDown(
object sender, MouseButtonEventArgs e) {
//---save into isolated storage---
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream("IsoStoreFile.txt",
FileMode.Create, isoStore);
StreamWriter writer = new StreamWriter(isoStream);
//---writes the lines to file---
writer.Write(GetSignatureLines());
txtStatus.Text = "Signature saved!";
writer.Close();
isoStream.Close();
}
Define the DrawSignature()
subroutine so that the signature can be reproduced from a string representing a collection of lines:
Читать дальше