wpf - How must I change my code to make more than one line shape? -


i want draw lines in "touch" application in wpf , wrote following code:

xaml (part):

<canvas x:name="maincanvas"             ismanipulationenabled="true"             touchdown="maincanvas_touchdown"              touchup="maincanvas_touchup"> 

c#:

public partial class mainwindow : window {     line myline = new line();      public mainwindow()     {         initializecomponent();     }      public void maincanvas_touchdown(object sender, toucheventargs e)     {         myline.stroke = system.windows.media.brushes.lightsteelblue;         myline.strokethickness = 2;         // line's starting point         myline.x1 = e.gettouchpoint(this).position.x;         myline.y1 = e.gettouchpoint(this).position.y;     }      public void maincanvas_touchup(object sender, toucheventargs e)     {         // line's ending point         myline.x2 = e.gettouchpoint(this).position.x;         myline.y2 = e.gettouchpoint(this).position.y;          maincanvas.children.add(myline);       } } 

with this, i'm able draw 1 line. if try draw another, application crashes.

for each line want draw, have create new line instance. in current code, have 1 line instance, instance represents 1 displayed line.

unlike other graphics toolkits use various objects draw graphical elements onto drawing surface step step, wpf works vector graphics: each of objects use is graphical element; 2 identical graphical elements, need 2 objects same properties.

therefore, when add same graphical object second time, in line:

maincanvas.children.add(myline); 

an exception thrown, because can add each graphical element once.

as want add arbitrary number of lines, store lines in list , add new line instance (i.e. instantiate line class , add new line canvas) in touch-down event. in touch-up event, set second point of new instance (the last element in list).

please aware not c#, wpf. (in other words, c# pro has never worked wpf not able tell you, wpf developer has never used c# (always vb.net, example) might help.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -