1

I seem to be unable to figure out how to Draw graphics in a Windows phone app in C#. I want to Draw e.g. a line. In old school Windows forms i add an event handler to the Windows paint event. And then use a GDI+ Graphics object. But there is no paint event in any controls?

So how do i draw a line on a canvas in a Windows phone app?


I think I need to clarify. I want to create dynamic graphics and I want to use C#. I want an update frequency arround 30 fps and I only need a few graphics elements approximately 100.

Generic Name
  • 1,083
  • 1
  • 12
  • 19
  • forget "old school", XAML based technologies have a completely different paradigm than archaic winforms. if you need a line, simply do `` or something. – Federico Berasategui Jun 19 '14 at 20:26

3 Answers3

0

You start by adding Canvas to the form and then .Add() graphical objects to the canvas children - it makes the object scaled for you by the engine, which is kind of neat. Usually looks like this:

line = new **Line**();
line.Stroke = Brushes.Yellow;

line.X1 = 0;
line.Y1 = 0;
line.X2 = 100;
line.Y2 = 100;

line.StrokeThickness = 2;
yourCanvas.Children.**Add(line)**;

Just drop the **s from the code - they are for attention grabbing.

Actually, as I drew dynamic hypercubes, I have never used the XAML version, but if you need a static structure or even substructure XAML is the way to go. As I understand, Children.Add() dynamically creates node in the parsed XAML tree, that .NET keeps in memory. If you can not take slight performance hit for dynamicly positioned graphics that WPF imposes, you will have to stick with DirectX or OpenGL for better performance.

  • -1 you don't `.Add()` stuff in XAML based technologies. That's what XAML is for. – Federico Berasategui Jun 19 '14 at 20:26
  • `static structure` - sorry, you have no idea what you're talking about. -1. See [my example](http://stackoverflow.com/a/15469477/643085) of a properly architected "dynamic lines" UI in XAML. No procedural code. It is actually a `ListBox`. BTW, since I never went to any schools, I have no idea what "dynamic hypercubes" are, however I do know XAML and the proper way to use it ;) – Federico Berasategui Jun 19 '14 at 20:33
  • So, you implicitly generate lines by Template, which is affectively a factory declared in XAML in your example. How do you think after that the template is added by .NET into the scene - magic? – Wanted by FBI.... Sexually LOL Jun 19 '14 at 21:08
  • it doesn't matter. Under the hood it also uses `System.Windows.Media.DrawingContext.DrawLine()`, and it doesn't matter. Under the hood it's all 0's and 1's in the CPU, and it doesn't matter. A procedural approach is discouraged in XAML based technologies. – Federico Berasategui Jun 19 '14 at 21:14
0

if you need a line, use the Line class:

<Page xmlns="whatever">
   <Grid>
      <Line X1="0" Y1="0" X2="10" Y2="10" Stroke="Blue" StrokeThickness="2"/>
   </Grid>
</Page>

Other than that, refer to MSDN.

Forget whatever procedural paradigms you might have learned in archaic technologies. Modern technologies are declarative.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
0

If you want low level access to draw 2D or 3D like when using DirectX you can take a look to SharpDX

There are some samples for Windows Phone in Github like:

  • MiniCube: Display a rotating cube in a DrawingSurfaceBackgroundGrid
berXpert
  • 149
  • 5