gpt4 book ai didi

c# - 在 ScrollViewer (UWP) 中移动图像

转载 作者:太空狗 更新时间:2023-10-30 00:30:56 25 4
gpt4 key购买 nike

我在 Scrollviewer 中有一个 Image...

<ScrollViewer x:Name="Scrollster" ZoomMode="Enabled" MinZoomFactor="1" MaxZoomFactor="4"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" ManipulationMode="All">
<Image x:Name="Img" Source="{x:Bind ImgSource}" Stretch="UniformToFill" PointerPressed="Img_PointerPressed"/>
</ScrollViewer>

我想在使用鼠标指针拖动图像时移动图像!

我试过:

private void Img_PointerPressed(object sender,PointerRoutedEventArgs e)
{
var p = e.Pointer;
}

但我无法获得指针位置来更改滚动查看器的位置。

我的代码有什么问题?我做得对吗?

最佳答案

应该在 Img 控件上设置 ManipulationMode。此外,您可能希望指定所需的确切模式而不是 All 以防止不必要的手势处理。

<Image x:Name="Img" Source="{x:Bind ImgSource}" Width="150" Height="150" Stretch="UniformToFill" 
ManipulationMode="TranslateX, TranslateY"
ManipulationStarted="Img_ManipulationStarted"
ManipulationDelta="Img_ManipulationDelta"
ManipulationCompleted="Img_ManipulationCompleted">
<Image.RenderTransform>
<CompositeTransform x:Name="Transform" />
</Image.RenderTransform>
</Image>

根据您上面的描述,我认为同时打开 TranslateXTranslateY 应该就足够了。然后,您将需要处理操作事件,例如 ManipulationStartedManipulationDeltaManipulationCompleted

您的大部分逻辑应该在 ManipulationDelta 事件中完成,该事件将在平移过程中多次触发。这是您获取 XY 位置并相应设置它们的地方。

这是一个简单的示例。

void Img_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
// dim the image while panning
this.Img.Opacity = 0.4;
}

void Img_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
this.Transform.TranslateX += e.Delta.Translation.X;
this.Transform.TranslateY += e.Delta.Translation.Y;
}

void Img_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
// reset the Opacity
this.Img.Opacity = 1;
}

关于c# - 在 ScrollViewer (UWP) 中移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31896930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com