本文翻译: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. 201521123092《Java程序设计》第七周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下 publ ...

  2. 201521123115《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...

  3. python2/python3 内存中打包/压缩文件

    python2:(包含压缩选项,如果只打包,可以调整zipfile.ZIP_DEFLATED) import zipfile import StringIO class InMemoryZip(obj ...

  4. MongoDB 分布式架构 复制 分片 适用性范围

    转载自 http://www.mongoing.com/archives/3573

  5. 懒人小工具:T4自动生成Model,Insert,Select,Delete以及导出Excel的方法

    之前写了篇文章,懒人小工具:[自动生成Model,Insert,Select,Delete以及导出Excel的方法](http://www.jianshu.com/p/d5b11589174a),但是 ...

  6. Flask-WTF 配置、验证及日志P4

    参数配置 参数 说明 WTF_CSRF_ENABLED 设置为False以禁用所有CSRF保护 WTF_CSRF_CHECK_DEFAULT 使用CSRF保护扩展时,这可以控制每个视图是否受到默认保护 ...

  7. oracle中number类型最简单明了解释

    NUMBER (p,s) p和s范围: p 1-38 s -84-127 number(p,s),s大于0,表示有效位最大为p,小数位最多为s,小数点右边s位置开始四舍五入,若s>p,小数点右侧 ...

  8. escape()、encodeURI()、encodeURIComponent()区别详解(转)

      JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,dec ...

  9. 如何用Python脚本从文件读取数据?

    最近自学Python的进度比较慢,工作之余断断续续的看着效率比较低,看来还是要狠下心来每天进步一点点. 还记得前段时间陈大猫提了一口"先实现用python读取本地文件",碰巧今天看 ...

  10. springmvc返回枚举属性值

    使用fastJSON ,在枚举中写toString 方法 如下@Overridepublic String toString() {return "{" + this.name() ...