win10 uwp 视差效果
我们觉得使用原来的微软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都会加载,但是里面的子控件只有滑到哪个页面时才会加载。
win10 uwp 视差效果的更多相关文章
- win10 uwp 萤火虫效果
原文:win10 uwp 萤火虫效果 本文在Nukepayload2指导下,使用他的思想用C#写出来. 本文告诉大家,如何使用 win2d 做出萤火虫效果. 安装 win2d 安装win2d的方法请使 ...
- win10 UWP 蜘蛛网效果
我看见了知乎首页登录背景和普通的地球人写的博客,发现了个好看的效果. 那么我来告诉大家如何做这个效果. 第一步是在 Canvas 画点,第二步是让点移动,第三步是画线 在 Canvas 画一个点 我们 ...
- win10 uwp win2d CanvasVirtualControl 与 CanvasAnimatedControl
本文来告诉大家 CanvasVirtualControl ,在什么时候使用这个控件. 在之前的入门教程win10 uwp win2d 入门 看这一篇就够了我直接用的是CanvasControl,实际上 ...
- Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App
安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...
- Win10 UWP开发系列:实现Master/Detail布局
在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...
- 【Win10 UWP】后台任务与动态磁贴
动态磁贴(Live Tile)是WP系统的大亮点之一,一直以来受到广大用户的喜爱.这一讲主要研究如何在UWP应用里通过后台任务添加和使用动态磁贴功能. 从WP7到Win8,再到Win10 UWP,磁贴 ...
- win10 uwp 列表模板选择器
本文主要讲ListView等列表可以根据内容不同,使用不同模板的列表模板选择器,DataTemplateSelector. 如果在 UWP 需要定义某些列的显示和其他列不同,或者某些行的显示和其他行不 ...
- win10 uwp 毛玻璃
毛玻璃在UWP很简单,不会和WPF那样伤性能. 本文告诉大家,如何在 UWP 使用 win2d 做毛玻璃. 毛玻璃可以使用 win2D 方法,也可以使用 Compositor . 使用 win2d 得 ...
- win10 uwp 渲染原理 DirectComposition 渲染
本文来告诉大家一个新的技术DirectComposition,在 win7 之后(实际上是 vista),微软正在考虑一个新的渲染机制 在 Windows Vista 就引入了一个服务,桌面窗口管理器 ...
随机推荐
- 201521123063 《java程序设计》第六周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...
- 201521123039《Java程序设计》 第二周学习总结
1.本周学习总结 答:上课老师介绍了Java基本的数据类型,需要注意的地方有:**java的整型数都为带符号数**,**byte类型范围(-127,128)太小,所以我们一般不使用byte型,byte ...
- 201521123040《Java程序设计》第2周学习总结
1. 本周学习总结 本周学习目录: <1>回顾(Java程序) <2>掌握各种数据类型的使用 <3>掌握运算符和表达式的使用 <4>枚举类.String ...
- 201521123100 《java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...
- 201521123103 《java学习笔记》 第十二周学习总结
一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 二.书面作业 将Student对象(属性:int id, String name,int age,double ...
- 201521123059 《Java程序设计》第十二周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...
- java课设
1.代码截图: 2.设计思路 建立GUI界面,系统产生一个随机数(对用户不可见),然后用户输入猜测数,系统根据用户每次输入的数据给出评语(偏大,偏小,猜测成功).当用户最终猜测成功后,就把当次的随机数 ...
- SpringMVC第五篇【方法返回值、数据回显、idea下配置虚拟目录、文件上传】
Controller方法返回值 Controller方法的返回值其实就几种类型,我们来总结一下-. void String ModelAndView redirect重定向 forward转发 数据回 ...
- Java开发中遇到的问题
head丢失 html的dtd不对 Integer数据类型 使用==比较 这个肯定错(事后才知道) sql语句处理分组的时候,在本地服务使用没问题,在服务器上出现sql异常 group by语句规范, ...
- python基础之条件循环语句
前两篇说的是数据类型和数据运算,本篇来讲讲条件语句和循环语句. 0x00. 条件语句 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语 ...