locked
Drag-Drop to target dynamically generated images

    Question

  • hi,

    i want to do;

    • dynamically add fruit image to stage and give them tag and different source. - i did this part
    • drag and drop them to target which if it is apple its target is apple basket - this part where i cant do.

    here is my codes;

    RectBGElma = new Rect(300, 300, 100, 100);
    .
    .
    .
                


    private async void setDynamicImages() { for (int i = 0; i < randMeyveCount; i++) { IMG_meyve = new Image();

    randMeyveTur = Rand.Next(6); switch (randMeyveTur) { case 0: IMG_meyve.Height = 36 * ratioHeight; IMG_meyve.Width = 51 * ratioWidth; randMeyveCesit = Rand.Next(5); IMG_meyve.Source = new BitmapImage(new Uri("ms-appx:///Assets/Sayfa10/dusen_meyveler/cilek_0" + (randMeyveCesit + 1) + ".png")); RectSelectedBGMeyve = RectBGCilek; IMG_meyve.Tag = i; break; case 1: IMG_meyve.Height = 68 * ratioHeight; IMG_meyve.Width = 85 * ratioWidth; randMeyveCesit = Rand.Next(5); IMG_meyve.Source = new BitmapImage(new Uri("ms-appx:///Assets/Sayfa10/dusen_meyveler/elma_0" + (randMeyveCesit + 1) + ".png")); RectSelectedBGMeyve = RectBGElma; IMG_meyve.Tag = i; break; . . . } this.canvasMeyveleriSayalim.Children.Add(IMG_meyve); IMG_meyve.PointerPressed += IMG_meyve_PointerPressed; IMG_meyve.PointerReleased += IMG_meyve_PointerReleased; }


     private void IMG_meyve_PointerPressed(object sender, PointerRoutedEventArgs e)
            {
                isHoldMeyve = true;
                pressedIMGTag = Convert.ToInt32(IMG_meyve.Tag);
                positionWithinImage = e.GetCurrentPoint(sender as Image).Position;
                meyveX = e.GetCurrentPoint(IMG_meyve).Position.X;
                meyveY = e.GetCurrentPoint(IMG_meyve).Position.Y;
            }


    private void IMG_meyve_PointerReleased(object sender, PointerRoutedEventArgs e)
            {
                isHoldMeyve = false;
                RectSelectedMeyve = new Rect(Canvas.GetLeft(IMG_meyve), Canvas.GetTop(IMG_meyve), IMG_meyve.ActualWidth, IMG_meyve.ActualHeight);
                if (isHitTest.isHitTest(RectSelectedMeyve, RectSelectedBGMeyve))
                {
                    OptimeAnimationLib.MsgBox("hit");
                }
            }
    private void canvasSayfa10_PointerMoved(object sender, PointerRoutedEventArgs e)
            {
                if (isHoldMeyve)
                {
                    if (Convert.ToInt32(IMG_meyve.Tag) == pressedIMGTag)
                    {
                        PointerPoint pt1 = e.GetCurrentPoint(canvasMeyveleriSayalim);
                        Canvas.SetLeft(IMG_meyve, pt1.Position.X - meyveX);
                        Canvas.SetTop(IMG_meyve, pt1.Position.Y - meyveY);
                    }
                }
            }

    if i drag any image, only last added one dragging. i mean i clicked first added image but last added one dragging.

    and hit test part didnt work(hittest class detect intersect of rects, it works on another part, tried before.)

    sorry for my English.

    thanks.



    • Edited by Murat VAROL Friday, March 21, 2014 10:22 AM
    Friday, March 21, 2014 10:15 AM

All replies

  • You could be using the built-in drag/drop events to capture this information.  I think it would work much better for this scenario than looking for intersecting rectangles.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, March 21, 2014 2:53 PM
    Moderator
  • sorry hittest part working now, it was because another mistake. not according to codes on top.

    but still i cant drag drop correctly, only last added one dragging.

    Saturday, March 22, 2014 6:07 PM