wpf 在 Windows 8 Metro 应用程序中拖放图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13000587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:52:50  来源:igfitidea点击:

Drag and drop image in Windows 8 Metro App

c#wpfxamlwindows-8

提问by Ruwan Dissanayaka

Is there a way to drag and drop an image in a Windows 8 metro app. I'm using C# and XAML. Following is what I need...

有没有办法在 Windows 8 Metro 应用程序中拖放图像。我正在使用 C# 和 XAML。以下是我需要的...

enter image description here

在此处输入图片说明

回答by John Koerner

Sure there is. You'll have to control it yourself, but it is pretty easy. You need to use a few pointer events like this:

当然有。你必须自己控制它,但这很容易。您需要使用一些像这样的指针事件:

XAML:

XAML:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"   PointerMoved="GridPointerMoved">
    <Image x:Name="image1" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Source="Assets/imageFile.png"  PointerPressed="ImagePointerPressed" PointerReleased="ImagePointerReleased"/>
</Grid>

Then in your CS file:

然后在您的 CS 文件中:

Point positionWithinImage;
private void ImagePointerPressed(object sender, PointerRoutedEventArgs e)
{
    Debug.WriteLine("Pressed");
    holding = true;

    positionWithinImage = e.GetCurrentPoint(sender as Image).Position;
}

private void ImagePointerReleased(object sender, PointerRoutedEventArgs e)
{
    Debug.WriteLine("Released");
    holding = false;
}

bool holding = false;

private void GridPointerMoved(object sender, PointerRoutedEventArgs e)
{
    if (holding)
    {
        var pos = e.GetCurrentPoint(image1.Parent as Grid).Position;
        image1.Margin = new Thickness(pos.X - this.positionWithinImage.X, pos.Y - this.positionWithinImage.Y, 0, 0);
    }
}

回答by joaquim.adraz

I've found a good solution here: http://xatazch.blogspot.pt/2012/08/drag-and-drop-item-using.html

我在这里找到了一个很好的解决方案:http: //xatazch.blogspot.pt/2012/08/drag-and-drop-item-using.html

Using TranslateTransform we can get a smooth and "real time" movement for the draggable item.

使用 TranslateTransform,我们可以获得可拖动项的平滑和“实时”移动。

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
     image1.ManipulationDelta += DragableItem_ManipulationDelta;
     image1.RenderTransform = new TranslateTransform();
}

private void DragableItem_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
     Image dragableItem = sender as Image;
     TranslateTransform translateTransform = dragableItem.RenderTransform as TranslateTransform;

     translateTransform.X += e.Delta.Translation.X;
     translateTransform.Y += e.Delta.Translation.Y;
}

Hope it helps!

希望能帮助到你!

回答by Starceaker

I tried your solution but it didn't really result in "perfect" moving of the image.

我尝试了你的解决方案,但它并没有真正导致图像的“完美”移动。

Alternatively you can try to use the Manipulation events:

或者,您可以尝试使用操作事件:

XAML:

XAML:

<Image x:Name="imgSanta" Width="250" Source="Assets/santa.png" ManipulationMode="All" ManipulationStarted="imgSanta_ManipulationStarted_1" ManipulationDelta="imgSanta_ManipulationDelta_1"></Image>

C#

C#

private void imgSanta_ManipulationStarted_1(object sender, ManipulationStartedRoutedEventArgs e)
        {
            txtFeedback.Text = "Manipulation started";
        }

        private void imgSanta_ManipulationDelta_1(object sender, ManipulationDeltaRoutedEventArgs e)
        {                
            var newTop = imgSanta.Margin.Top + e.Delta.Translation.Y;
            var newLeft = imgSanta.Margin.Left + e.Delta.Translation.X;
            imgSanta.Margin = new Thickness(newLeft, newTop, 0, 0);
        } 

Even with this I'm not 100% satisfied but I do think it has a better result. Let me know what you think about this. (Even though this is an older post, it's worth mentioning)

即使这样,我也不是 100% 满意,但我确实认为它有更好的结果。让我知道您对此有何看法。(尽管这是一个较旧的帖子,但值得一提)

EDIT: I noticed the 1:1 movement only worked when my image was located within a StackPanel.

编辑:我注意到 1:1 运动仅在我的图像位于 StackPanel 内时才有效。

回答by Nils

joaquims approach is the faster one... have look at this demo app: http://code.msdn.microsoft.com/windowsapps/Drag-and-Drop-a-picture-in-26580dc0

joaquims 方法是更快的方法......看看这个演示应用程序:http://code.msdn.microsoft.com/windowsapps/Drag-and-Drop-a-picture-in-26580dc0