• [UWP] 解决FlipView图片放大的诡异bug


    想要实现图片的放大缩小可以通过在Image外面套一个ScrollViewer,然后设置ScrollViewer的ZoomMode="Enabled"

    <FlipView ItemsSource="{x:Bind Images}">
        <FlipView.ItemTemplate>
            <DataTemplate>
                <ScrollViewer ZoomMode="Enabled" MinZoomFactor="1">
                    <Image Source="{Binding }"/>
                </ScrollViewer>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>

    这样就可以粗糙地实现图片放缩了,为什么说是粗糙的,因为这样简单地弄会出现一个bug 。。。

    把图片放大到超过屏幕宽度后,停止放大,会看到图片诡异地动了一下,

     解决方案

    控制图片的大小和ScrollViewer的Viewport一样大

    <FlipView ItemsSource="{x:Bind Images}" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch">
                        <FlipView.ItemTemplate>
                            <DataTemplate>
                                <ScrollViewer x:Name="ScrollViewerMain" ZoomMode="Enabled"
                                              MinZoomFactor="0.5"
                                              HorizontalScrollBarVisibility="Hidden"
                                              VerticalScrollBarVisibility="Hidden">
                                    <Image Source="{Binding }"
                                           MaxWidth="{Binding Path=ViewportWidth,ElementName=ScrollViewerMain}"
                                           MaxHeight="{Binding Path=ViewportHeight,ElementName=ScrollViewerMain}"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"/>
                                </ScrollViewer>
                            </DataTemplate>
                        </FlipView.ItemTemplate>
                    </FlipView>

     再加个双击放大

    给ScrollViewer加个DoubleTapped的事件处理

     private async void ScrollViewerMain_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
            {
                var scrollViewer = sender as ScrollViewer;
                var doubleTapPoint = e.GetPosition(scrollViewer);
    
                if (Math.Abs(scrollViewer.ZoomFactor - 1.0) > 1E-06)
                {
                    scrollViewer.ChangeView(doubleTapPoint.X, doubleTapPoint.Y, 1);
                }
                else 
                {
                    var dispatcher = Window.Current.CoreWindow.Dispatcher;
                    await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
                    {
                        scrollViewer.ChangeView(doubleTapPoint.X, doubleTapPoint.Y, 2);
                    });
                }
            }

    附上Demo

    FlipViewDemo.rar

    参考链接

    https://www.goedware.com/index.php/blog/3-pinch-zoom-image-in-uwp-app

    http://igrali.com/2015/07/17/why-is-my-zoomable-scrollviewer-snapping-the-image-to-the-left/

    https://stackoverflow.com/questions/37380661/uwp-zooming-image-with-pinch-zoom-and-double-tap-with-flip-view

  • 相关阅读:
    ASP.NET提供文件下载函数
    web.config access数据库相对路径设置方法 for asp.net2.0
    MSDE安装后身份验证自动是windows验证,osql命令中修改为sqlserve身份验证或混合验证呢及注冊表修改方法
    关于ASP.NET IIS的注册:
    指定datagrid编辑框的宽度及相映属性(转)备用
    TransparentListBox
    在dll中使用FastReport会遇到以下内个问题
    值得学习一
    注册表恢复默认共享
    有关dnn的安装不成功问题
  • 原文地址:https://www.cnblogs.com/arcsinw/p/8638526.html
Copyright © 2020-2023  润新知