简体   繁体   中英

Moving objects on canvas - windows phone

I'm developing a software for Windows Phone 8.1, and I have the following situation.

On one of my pages (not the MainPage) I have a canvas, and I have a few objects (polygons and lines) generated dynamically. The lines are on the polygon edges, because I would like to use the edges isolated, different colors, events, etc.

using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

Polygon polygon = new Polygon();
polygon.Points.Add(new Point(x1, y1));
polygon.Points.Add(new Point(x2, y2));
//other awesome things
Line line = new Line();
line.X1 = x1;
line.Y1 = y1;
line.X2 = x2;
line.Y2 = y2;
//other amazing things

I was able to add these things to the canvas, until this point everything is okay. But I have to move these objects on touch (drag & drop, ManipulationDelta event maybe?), and of course if the polygon itself is moving, the lines should move too... So how could I do that? What should my event look like?

I thought I could create a different class for these objects like this:

public class MyPolygon
{
    public Polygon _polygon;
    public List<Line> _lines;
}

And this could be a nice idea if I have coordinate-setting (and other) functions in it, but then how can I connect them to the canvas? And the event handling is still a question for this idea too.

So my question is half theoretical and half practical. I'm open to every solution and idea about this topic.

If you are using a canvas you should make it proportional by using the following lines of code and manipulating them.

width = Convert.ToInt32(Window.Current.Bounds.Width);
height = Convert.ToInt32(Window.Current.Bounds.Height)

You can add an event that every time you move something the whole screen adapts to the change and moves with it. The way to actually move objects on the canvas from the c# code is simple:

Obj1.SetValue(Canvas.LeftProperty, Width/2);
Obj1.SetValue(Canvas.TopProperty, height * 0.1);

this will move the object's top left corner to 0.9 the height of the screen and half the width of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM