本文翻译:http://jamescroft.co.uk/blog/uwp/playing-with-scrolling-parallax-effects-on-ui-elements-in-windows-apps/

我们觉得使用原来的微软ScrollView效果实在不好,我们需要一个好看的效果。如果你不知道我上面说的是什么,那么我应该来说我马上要做的,其实我们可以看到我们有很多层图片,在我们向下拉,他们下降不一样,给我们看来好像我们移动的是在一个有Z。这里说的Z是三维,因为三维XYZ。我们通过这个让我们的滚动看起来不是那么差。

其实我们可以看:http://www.cnblogs.com/JoannaQ/archive/2013/02/08/2909111.html 大神的解释很好

我们可以使用大神做的https://github.com/clarkezone/UWPCompositionDemos他用的很简单,我们需要滚动条,图片。他这个需要我们改多,所以我们做个可以添加到滚动条的控件,有滚动条的GridView,ListView,他们移动会让后面图片比前面下降少,如果我们滚动向下。


public class UIElementParallaxEffectBehavior : DependencyObject, IBehavior
{
        public static readonly DependencyProperty ParallaxElementProperty =
            DependencyProperty.Register(
                "ParallaxElement",
                typeof(UIElement),
                typeof(UIElementParallaxEffectBehavior),
                new PropertyMetadata(null, OnParallaxElementChanged));

        public static readonly DependencyProperty ParallaxMultiplierProperty =
            DependencyProperty.Register(
                "ParallaxMultiplier",
                typeof(double),
                typeof(UIElementParallaxEffectBehavior),
                new PropertyMetadata(0.3));

        public double ParallaxMultiplier
        {
            get
            {
                return (double)GetValue(ParallaxMultiplierProperty);
            }
            set
            {
                SetValue(ParallaxMultiplierProperty, value);
            }
        }

        private static void OnParallaxElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var behavior = d as UIElementParallaxEffectBehavior;
            behavior?.AttachParallaxEffect();
        }

        private void AttachParallaxEffect()
        {
            if (this.ParallaxElement != null && this.AssociatedObject != null)
            {
                var scrollViewer = this.AssociatedObject as ScrollViewer;
                if (scrollViewer == null)
                {
                    // Attempt to see if this is attached to a scroll-based control like a ListView or GridView.
                    scrollViewer = this.AssociatedObject.FindChildElementOfType<ScrollViewer>();

                    if (scrollViewer == null)
                    {
                        throw new InvalidOperationException("The associated object or one of it's child elements must be of type ScrollViewer.");
                    }
                }

                var compositionPropertySet =
                    ElementCompositionPreview.GetScrollViewerManipulationPropertySet(scrollViewer);

                var compositor = compositionPropertySet.Compositor;

                var animationExpression =
                    compositor.CreateExpressionAnimation("ScrollViewer.Translation.Y * Multiplier");

                animationExpression.SetScalarParameter("Multiplier", (float)this.ParallaxMultiplier);
                animationExpression.SetReferenceParameter("ScrollViewer", compositionPropertySet);

                var visual = ElementCompositionPreview.GetElementVisual(this.ParallaxElement);
                visual.StartAnimation("Offset.Y", animationExpression);
            }
        }

        public UIElement ParallaxElement
        {
            get
            {
                return (UIElement)this.GetValue(ParallaxElementProperty);
            }
            set
            {
                this.SetValue(ParallaxElementProperty, value);
            }
        }

        public void Attach(DependencyObject associatedObject)
        {
            if (this.AssociatedObject != null)
            {
                throw new InvalidOperationException("Cannot assign to the same behavior twice.");
            }

            this.AssociatedObject = associatedObject;

            this.AttachParallaxEffect();
        }

        public void Detach()
        {
            this.AssociatedObject = null;
        }

        public DependencyObject AssociatedObject { get; private set; }
}

如果觉得我做的好复杂,也没有注释,那么我就想骂下垃圾微软,因为我做到这我的神器没法使用,我们来弄个简单,因为刚才我翻译的那个他做SDK,我们不是需要在背景弄一个图片,我们可以使用一个可以显示的。

当然我们需要在Nuget下载:https://www.nuget.org/packages/Microsoft.Xaml.Behaviors.Uwp.Managed/

源码:https://github.com/Microsoft/XamlBehaviors

我们将会修改微软的

public class ParallaxBehavior : Behavior<FrameworkElement>
{

    public UIElement ParallaxContent
    {
        get { return (UIElement)GetValue(ParallaxContentProperty); }
        set { SetValue(ParallaxContentProperty, value); }
    }

    public static readonly DependencyProperty ParallaxContentProperty = DependencyProperty.Register(
        "ParallaxContent",
        typeof(UIElement),
        typeof(ParallaxBehavior),
        new PropertyMetadata(null, OnParallaxContentChanged));

    public double ParallaxMultiplier
    {
        get { return (double)GetValue(ParallaxMultiplierProperty); }
        set { SetValue(ParallaxMultiplierProperty, value); }
    }

    public static readonly DependencyProperty ParallaxMultiplierProperty = DependencyProperty.Register(
        "ParallaxMultiplier",
        typeof(double),
        typeof(ParallaxBehavior),
        new PropertyMetadata(0.3d));

    protected override void OnAttached()
    {
        base.OnAttached();
        AssignParallax();
    }

    private void AssignParallax()
    {
        if (ParallaxContent == null) return;
        if (AssociatedObject == null) return;

        var scroller = AssociatedObject as ScrollViewer;
        if (scroller == null)
        {
            scroller = AssociatedObject.GetChildOfType<ScrollViewer>();
        }
        if (scroller == null) return;

        CompositionPropertySet scrollerViewerManipulation = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(scroller);

        Compositor compositor = scrollerViewerManipulation.Compositor;

        ExpressionAnimation expression = compositor.CreateExpressionAnimation("ScrollManipululation.Translation.Y * ParallaxMultiplier");

        expression.SetScalarParameter("ParallaxMultiplier", (float)ParallaxMultiplier);
        expression.SetReferenceParameter("ScrollManipululation", scrollerViewerManipulation);

        Visual textVisual = ElementCompositionPreview.GetElementVisual(ParallaxContent);
        textVisual.StartAnimation("Offset.Y", expression);
    }

    private static void OnParallaxContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = d as ParallaxBehavior;
        b.AssignParallax();
    }
 }

我们可以在Xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Image x:Name="ParallaxImage" Source="ms-appx:///Assets/Guadeloupe.jpg" Stretch="Fill"/>
    <ScrollViewer>
        <TextBlock HorizontalAlignment="Stretch" TextWrapping="WrapWholeWords" FontSize="30" Foreground="Black">
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                Nunc fringilla ultrices est eu ornare.
                Suspendisse purus massa, iaculis in finibus dictum, gravida vel purus.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
        </TextBlock>
        <interactivity:Interaction.Behaviors>
            <behaviors:ParallaxBehavior ParallaxContent="{Binding ElementName=ParallaxImage}" />
        </interactivity:Interaction.Behaviors>
    </ScrollViewer>
</Grid>

上面代码是http://visuallylocated.com/post/2015/12/10/Easy-Parallax-effect-in-Windows-10.aspx修改,因为我现在没法使用神器,不知道他说的是不是对的。

好久没写,因为最近忙,要考试,所以现在也没有多少去查,如果看到不对的,希望大神能在我博客说。

我们刚才使用就是在有ScrollView的ListView可以使用,现在我们使用改的代码

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView>
        <ListView.Header>
            <Image x:Name="ParallaxImage" Source="ms-appx:///Assets/Guadeloupe.jpg" Stretch="UniformToFill"/>
        </ListView.Header>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Background" Value="White"/>
            </Style>
        </ListView.ItemContainerStyle>
        <x:String>Lorem ipsum dolor sit amet, consectetur adipiscing.</x:String>
        <x:String>Nunc fringilla ultrices est eu ornare.</x:String>
        <x:String>Suspendisse purus massa, iaculis in finibus dictum, gravida vel purus.</x:String>
        <interactivity:Interaction.Behaviors>
            <behaviors:ParallaxBehavior ParallaxContent="{Binding ElementName=ParallaxImage}" ParallaxMultiplier="-0.2"/>
        </interactivity:Interaction.Behaviors>
    </ListView>
</Grid>

我很少文章是自己原创,但是翻译也写不好

晚上1429081529在群里:地图是否可以离线使用,于是我就开了地图,在这时我们在说,pivot控件,是滑动到哪个页面,哪个页面才会开始加载的吗?经过LI大神的细心断点,发现只要已进入这个page,不管哪个pivotItem都会加载,但是里面的子控件只有滑到哪个页面时才会加载。

https://blogs.windows.com/buildingapps/2015/12/08/awaken-your-creativity-with-the-new-windows-ui-composition/

win10 uwp 视差效果的更多相关文章

  1. win10 uwp 萤火虫效果

    原文:win10 uwp 萤火虫效果 本文在Nukepayload2指导下,使用他的思想用C#写出来. 本文告诉大家,如何使用 win2d 做出萤火虫效果. 安装 win2d 安装win2d的方法请使 ...

  2. win10 UWP 蜘蛛网效果

    我看见了知乎首页登录背景和普通的地球人写的博客,发现了个好看的效果. 那么我来告诉大家如何做这个效果. 第一步是在 Canvas 画点,第二步是让点移动,第三步是画线 在 Canvas 画一个点 我们 ...

  3. win10 uwp win2d CanvasVirtualControl 与 CanvasAnimatedControl

    本文来告诉大家 CanvasVirtualControl ,在什么时候使用这个控件. 在之前的入门教程win10 uwp win2d 入门 看这一篇就够了我直接用的是CanvasControl,实际上 ...

  4. Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App

    安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...

  5. Win10 UWP开发系列:实现Master/Detail布局

    在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...

  6. 【Win10 UWP】后台任务与动态磁贴

    动态磁贴(Live Tile)是WP系统的大亮点之一,一直以来受到广大用户的喜爱.这一讲主要研究如何在UWP应用里通过后台任务添加和使用动态磁贴功能. 从WP7到Win8,再到Win10 UWP,磁贴 ...

  7. win10 uwp 列表模板选择器

    本文主要讲ListView等列表可以根据内容不同,使用不同模板的列表模板选择器,DataTemplateSelector. 如果在 UWP 需要定义某些列的显示和其他列不同,或者某些行的显示和其他行不 ...

  8. win10 uwp 毛玻璃

    毛玻璃在UWP很简单,不会和WPF那样伤性能. 本文告诉大家,如何在 UWP 使用 win2d 做毛玻璃. 毛玻璃可以使用 win2D 方法,也可以使用 Compositor . 使用 win2d 得 ...

  9. win10 uwp 渲染原理 DirectComposition 渲染

    本文来告诉大家一个新的技术DirectComposition,在 win7 之后(实际上是 vista),微软正在考虑一个新的渲染机制 在 Windows Vista 就引入了一个服务,桌面窗口管理器 ...

随机推荐

  1. 201521123087 《Java程序设计》第5周学习总结

    1. 本周学习总结 2. 书面作业 作业参考文件下载 代码阅读:Child压缩包内源代码1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误.并分析输出结 ...

  2. 201521044091 《Java程序设计》第3周学习总结

    1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 本周学习总结 ...

  3. 201521123004 《Java程序设计》第3周学习总结

    1. 本周学习总结 (1)①使用构造函数(constructor) eg:Date now = new Date(); new Date(); //创建了一个Date对象 now是Date类型变量,存 ...

  4. 陈敏-第一周Java课程总结

    一.本周学习总结 1.感受到JAVA的神奇魅力,以及其跨平台的优势 2.第一次接触感觉还是有很多不懂. 3.了解了JDK 二.书面作业 (一)为什么java程序可以跨平台运行?执行java程序的步骤是 ...

  5. java课设

    1.代码截图: 2.设计思路 建立GUI界面,系统产生一个随机数(对用户不可见),然后用户输入猜测数,系统根据用户每次输入的数据给出评语(偏大,偏小,猜测成功).当用户最终猜测成功后,就把当次的随机数 ...

  6. 201521123039 《java程序设计》第一周学习总结

    #1.本章学习总结 Java是面向对象的程序语言,它一切定义都是对象.我们所编写的Java程序经过编译后生成了*.class的文件,再经过JVM对*.class解释运行就可以得到Java程序,所以Ja ...

  7. TCP/IP协议:OSI七层模型、TCP/IP四层模型的对比

    1. OSI七层和TCP/IP四层的关系 1.1 OSI引入了服务.接口.协议.分层的概念,TCP/IP借鉴了OSI的这些概念建立TCP/IP模型. 1.2 OSI先有模型,后有协议,先有标准,后进行 ...

  8. 浅谈JAVA中“增强”类的某个方法的几个中方法!

    一.继承 使用场景:能够控制这个类的构造的时候,才可以使用继承. 优点:简单容易使用, 缺点:耦合性大大的增强,不利于后期的维护,所以对于继承这种方法,谨慎使用.  代码实现:二.装饰者模式 使用场景 ...

  9. 《Head First 设计模式》读书笔记(1) - 策略模式

    <Head First 设计模式>(点击查看详情) 1.写在前面的话 之前在列书单的时候,看网友对于设计模式的推荐里说,设计模式的书类别都大同小异,于是自己就选择了Head First系列 ...

  10. python实例编写(4)--js,滚动条,cookie,验证码,获取特定属性的元素,实现原理

    一.调用js 执行方法:execute_script(script,*args) 场景一:在页面上直接执行调用js 场景二:在定位的某个元素上执行调用js 如:掩藏文字(提示插件 tooltip设置淡 ...