原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础

[源码下载]

重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 ScrollViewer

  • 演示 ScrollViewer 的基本应用
  • 演示 ScrollBar 的基本应用
  • 演示 ScrollContentPresenter 的基本应用

示例
1、ScrollViewer 的基本应用
ScrollViewer/Demo.xaml

<Page
x:Class="XamlDemo.Controls.ScrollViewer.Demo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.ScrollViewer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" /> <!--
ScrollViewer - 滚动视图控件
Content - 滚动视图内的内容
IsDeferredScrollingEnabled - 是否启用延迟滚动,在滚动内容过多时,启用延迟混动可以改善性能,默认值为 false
HorizontalScrollMode - 水平滚动条的行为方式,Windows.UI.Xaml.Controls.ScrollMode枚举(Disabled, Enabled, Auto)
VerticalScrollMode - 垂直滚动条的行为方式
HorizontalScrollBarVisibility - 水平滚动条的可见性,Windows.UI.Xaml.Controls.ScrollBarVisibility枚举(Disabled, Auto, Hidden, Visible)
VerticalScrollBarVisibility - 垂直滚动条的可见性
ViewChanged - 发生滚动时所触发的事件
-->
<ScrollViewer Name="scrollViewer" Width="400" Height="400" Margin="0 10 0 0" HorizontalAlignment="Left"
IsDeferredScrollingEnabled="False"
ViewChanged="scrollViewer_ViewChanged_1"
HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled"
HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
<ScrollViewer.Content>
<Image Source="/Assets/Logo.png" Width="1000" />
</ScrollViewer.Content>
</ScrollViewer> <StackPanel Orientation="Horizontal">
<!--使 ScrollViewer 里的内容滚动到相对于 ScrollViewer 居中-->
<Button Content="居中" Click="Button_Click_1" />
</StackPanel> </StackPanel>
</Grid>
</Page>

ScrollViewer/Demo.xaml.cs

/*
* ScrollViewer - 滚动视图控件
*
* 本例用于演示 ScrollViewer 的基本用法
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Controls.ScrollViewer
{
public sealed partial class Demo : Page
{
public Demo()
{
this.InitializeComponent();
} private void scrollViewer_ViewChanged_1(object sender, ScrollViewerViewChangedEventArgs e)
{
lblMsg.Text = ""; /*
* ScrollViewer - 滚动视图控件
* ComputedHorizontalScrollBarVisibility - 当前水平滚动条的可见性,比如当 HorizontalScrollBarVisibility 设置为 Auton 时,可以通过 ComputedHorizontalScrollBarVisibility 来判断当前水平滚动条是否可见
* ComputedVerticalScrollBarVisibility - 当前垂直滚动条的可见性
* ExtentWidth - ScrollViewer 内的内容的宽
* ExtentHeight - ScrollViewer 内的内容的高
* ViewportWidth - 可视区的宽
* ViewportHeight - 可视区的高
* HorizontalOffset - 滚动内容的水平方向的偏移量
* VerticalOffset - 滚动内容的垂直方向的偏移量
* ScrollableWidth - 可滚动区域的水平方向的大小
* ScrollableHeight - 可滚动区域的垂直方向的大小
*
* ScrollToHorizontalOffset() - 滚动到指定的水平偏移位置
* ScrollToVerticalOffset() - 滚动到指定的垂直偏移位置
*/ lblMsg.Text += "ComputedHorizontalScrollBarVisibility: " + scrollViewer.ComputedHorizontalScrollBarVisibility;
lblMsg.Text += "\r\n";
lblMsg.Text += "ComputedVerticalScrollBarVisibility: " + scrollViewer.ComputedVerticalScrollBarVisibility;
lblMsg.Text += "\r\n";
lblMsg.Text += "ExtentWidth: " + scrollViewer.ExtentWidth;
lblMsg.Text += "\r\n";
lblMsg.Text += "ExtentHeight: " + scrollViewer.ExtentHeight;
lblMsg.Text += "\r\n";
lblMsg.Text += "ViewportWidth: " + scrollViewer.ViewportWidth;
lblMsg.Text += "\r\n";
lblMsg.Text += "ViewportHeight: " + scrollViewer.ViewportHeight;
lblMsg.Text += "\r\n";
lblMsg.Text += "HorizontalOffset: " + scrollViewer.HorizontalOffset;
lblMsg.Text += "\r\n";
lblMsg.Text += "VerticalOffset: " + scrollViewer.VerticalOffset;
lblMsg.Text += "\r\n";
lblMsg.Text += "ScrollableWidth: " + scrollViewer.ScrollableWidth;
lblMsg.Text += "\r\n";
lblMsg.Text += "ScrollableHeight: " + scrollViewer.ScrollableHeight;
lblMsg.Text += "\r\n";
} private void Button_Click_1(object sender, RoutedEventArgs e)
{
scrollViewer.ScrollToHorizontalOffset(scrollViewer.ScrollableWidth / );
scrollViewer.ScrollToVerticalOffset(scrollViewer.ScrollableHeight / );
}
}
}

2、ScrollBar 的基本应用
ScrollViewer/ScrollBarDemo.xaml

<Page
x:Class="XamlDemo.Controls.ScrollViewer.ScrollBarDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.ScrollViewer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120,0,0,0"> <TextBlock Name="lblMsg" FontSize="14.667" HorizontalAlignment="Left" />
<ScrollViewer Name="scrollViewer" Width="400" Height="200" HorizontalAlignment="Left">
<Image Source="/Assets/Logo.png" Width="1000" />
</ScrollViewer> </StackPanel>
</Grid>
</Page>

ScrollViewer/ScrollBarDemo.xaml.cs

/*
* ScrollBar - 滚动条控件
*
* 本例通过访问 ScrollViewer 内的名为 VerticalScrollBar 的 ScrollBar 控件,来简要说明 ScrollBar 控件
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using XamlDemo.Common; namespace XamlDemo.Controls.ScrollViewer
{
public sealed partial class ScrollBarDemo : Page
{
public ScrollBarDemo()
{
this.InitializeComponent(); this.Loaded += ScrollBarDemo_Loaded;
} void ScrollBarDemo_Loaded(object sender, RoutedEventArgs e)
{
// 找到 ScrollViewer 内的名为 VerticalScrollBar 的 ScrollBar 控件,即 ScrollViewer 内的垂直滚动条
var scrollBar = Helper.GetVisualChild<ScrollBar>(scrollViewer, "VerticalScrollBar");
// ValueChanged - 当滚动条的值发生改变是所触发的事件
scrollBar.ValueChanged += scrollBar_ValueChanged;
} void scrollBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
// 显示垂直滚动条的当前值
lblMsg.Text = e.NewValue.ToString();
}
}
}

3、ScrollContentPresenter 的基本应用
ScrollViewer/ScrollContentPresenterDemo.xaml

<Page
x:Class="XamlDemo.Controls.ScrollViewer.ScrollContentPresenterDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.ScrollViewer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <ScrollViewer Name="scrollViewer" Width="400" Height="400" Background="Blue" HorizontalAlignment="Left"
HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled"
HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
<ScrollViewer.Content>
<Image Source="/Assets/Logo.png" Width="1000" />
</ScrollViewer.Content>
</ScrollViewer> </StackPanel>
</Grid>
</Page>

ScrollViewer/ScrollContentPresenterDemo.xaml.cs

/*
* ScrollContentPresenter - ScrollViewer 的内容呈现器(类似的有 ContentPresenter, ItemsPresenter)
*
* ScrollContentPresenter 用来呈现 ScrollViewer 的 Content
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using XamlDemo.Common; namespace XamlDemo.Controls.ScrollViewer
{
public sealed partial class ScrollContentPresenterDemo : Page
{
public ScrollContentPresenterDemo()
{
this.InitializeComponent(); this.Loaded += ScrollContentPresenterDemo_Loaded;
} void ScrollContentPresenterDemo_Loaded(object sender, RoutedEventArgs e)
{
// 找到 ScrollViewer 内的名为 ScrollContentPresenter 的 ScrollContentPresenter 控件
var scrollContentPresenter = Helper.GetVisualChild<ScrollContentPresenter>(scrollViewer, "ScrollContentPresenter"); scrollContentPresenter.Margin = new Thickness();
}
}
}

OK
[源码下载]

重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础的更多相关文章

  1. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom

    原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...

  2. 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree

    原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...

  3. 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager

    原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...

  4. 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试

    原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...

  5. 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

    原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...

  6. 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom

    原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...

  7. 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示

    原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...

  8. 重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView

    原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...

  9. 重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid

    原文:重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGr ...

随机推荐

  1. 系统变量file.encoding对Java的运行影响有多大?(转)good

    这个话题来自: Nutz的issue 361 在考虑这个issue时, 我一直倾向于使用系统变量file.encoding来改变JVM的默认编码. 今天,我想到, 这个系统变量,对JVM的影响到底有多 ...

  2. 基于DSP的疲劳驾驶检测系统的研究

    原地址:http://www.chinaaet.com/article/index.aspx?id=114534 关键词:疲劳检测DSP亮瞳效应PERCLOS 摘  要: 针对汽车驾驶员疲劳驾驶检测的 ...

  3. 使用Swing实现简易而不简单的文档编辑器

    本文通过Swing来实现文档简易而不简单的文档编辑器,该文档编辑器的功能包括: 设置字体样式:粗体,斜体,下划线,可扩展 设置字体:宋体,黑体,可扩展 设置字号:12,14,18,20,30,40, ...

  4. Mod in math

    An Introduction to Modular Math When we divide two integers we will have an equation that looks like ...

  5. asp.net EF6.0中出现未找到具有固定名称“System.Data.SqlClient”的 ADO.NET提供程序的实体框架提供程序解决办法

    出现的错误信息如下所示: 指定的架构无效.错误:  DataModel.ssdl(2,2) : 错误 0152: 未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提 ...

  6. python语言学习1——初识python

    Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. 龟叔给Python的定位是“优雅”.“明确”.“简单”,所以Python ...

  7. 服务器编程入门(2)IP协议详解

    问题聚焦:     IP协议是TCP/IP协议族的核心协议,也是socket网络编程的基础之一.这里从两个方面较为深入地探讨IP协议:     1,IP头部信息(指定IP通信的源端IP地址,目的端IP ...

  8. 低版本的 opencv库的 vs2010 打开 高版本opencv

    打开track.vcxproj文件, 注释掉跟版本有关的行就可. 本例子中,当用双击.sln用vs2010打开高版本的opencv项目时,会出现错误, 并且会有错误信息提示,双击该错误信息,就会打开该 ...

  9. openstack学习笔记一 虚拟机启动过程代码跟踪

    openstack学习笔记一 虚拟机启动过程代码跟踪 本文主要通过对虚拟机创建过程的代码跟踪.观察虚拟机启动任务状态的变化,来透彻理解openstack各组件之间的作用过程. 当从horizon界面发 ...

  10. shiro权限架作战

    shiro框架作为一种特权的开源框架,通过身份验证和授权从具体的业务逻辑分离极大地提高了我们的发展速度,它的易用性使得它越来越受到人们的青睐.上一页ACL架相比,shiro能更easy的实现权限控制, ...