背水一戰 Windows 10 (47) - 控制項(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom

来源:http://www.cnblogs.com/webabcd/archive/2017/05/08/6823782.html
-Advertisement-
Play Games

背水一戰 Windows 10 之 控制項(ScrollViewer 特性): Chaining - 鎖鏈, Rail - 軌道, Inertia - 慣性, Snap - 對齊, Zoom - 縮放 ...


[源碼下載]


背水一戰 Windows 10 (47) - 控制項(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom



作者:webabcd


介紹
背水一戰 Windows 10 之 控制項(ScrollViewer 特性)

  • Chaining - 鎖鏈
  • Rail - 軌道
  • Inertia - 慣性
  • Snap - 對齊
  • Zoom - 縮放



示例
1、演示 ScrollViewer 的 Chaining 特性
Controls/ScrollViewerDemo/Chaining.xaml

<Page
    x:Class="Windows10.Controls.ScrollViewerDemo.Chaining"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.ScrollViewerDemo"
    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="10 0 10 10">

            <ScrollViewer HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled">
                <StackPanel>

                    <CheckBox Name="chkIsHorizontalScrollChainingEnabled" Content="IsHorizontalScrollChainingEnabled" IsChecked="True" Margin="5" />
                    <CheckBox Name="chkIsVerticalScrollChainingEnabled" Content="IsVerticalScrollChainingEnabled" IsChecked="True" Margin="5" />

                    <TextBlock Text="我是參照物" Margin="5" />
                    
                    <!--
                        Chaining: 鎖鏈,在觸摸模式下,滾動本 ScrollViewer 如果超出了邊界,則滾動其父 ScrollViewer
                            本例的測試方法:在觸摸模式下,滾動 ScrollViewer 內的內容直至超出邊界,超出邊界後不要停下來繼續滾動,通過“我是參照物”觀察父 ScrollViewer 是否也被滾動
                    
                        IsHorizontalScrollChainingEnabled - 是否啟用水平方向上的 Chaining,預設值為 true
                        IsVerticalScrollChainingEnabled - 是否啟用垂直方向上的 Chaining,預設值為 true
                    -->

                    <ScrollViewer Name="scrollViewer" Width="400" Height="400" Margin="5" HorizontalAlignment="Left" 
                          HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled"
                          HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible"
                          IsHorizontalScrollChainingEnabled="{Binding IsChecked, ElementName=chkIsHorizontalScrollChainingEnabled}"
                          IsVerticalScrollChainingEnabled="{Binding IsChecked, ElementName=chkIsVerticalScrollChainingEnabled}">
                        <Image Source="/Assets/StoreLogo.png" Width="1000" />
                    </ScrollViewer>

                </StackPanel>
            </ScrollViewer>

        </StackPanel>
    </Grid>
</Page>


2、演示 ScrollViewer 的 Rail 特性
Controls/ScrollViewerDemo/Rail.xaml

<Page
    x:Class="Windows10.Controls.ScrollViewerDemo.Rail"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.ScrollViewerDemo"
    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="10 0 10 10">

            <CheckBox Name="chkIsHorizontalRailEnabled" Content="IsHorizontalRailEnabled" IsChecked="True" Margin="5" />
            <CheckBox Name="chkIsVerticalRailEnabled" Content="IsVerticalRailEnabled" IsChecked="True" Margin="5" />
            
            <!--
                Rail: 軌道,在觸摸模式下,假設沿水平方向滾動,則由於軌道的作用,在鬆手前只能延水平方向滾動(即使手指有垂直方向的滾動也無用)
                
                IsHorizontalRailEnabled - 是否啟用水平方向上的軌道,預設值為 true
                IsVerticalRailEnabled - 是否啟用垂直方向上的軌道,預設值為 true
            -->

            <ScrollViewer Name="scrollViewer" Width="400" Height="400" Margin="5" HorizontalAlignment="Left"
                          HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled"
                          HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible"
                          IsHorizontalRailEnabled="{Binding IsChecked, ElementName=chkIsHorizontalRailEnabled}" 
                          IsVerticalRailEnabled="{Binding IsChecked, ElementName=chkIsVerticalRailEnabled}">
                <Image Source="/Assets/StoreLogo.png" Width="1000" />
            </ScrollViewer>

        </StackPanel>
    </Grid>
</Page>



3、演示 ScrollViewer 的 Inertia 特性
Controls/ScrollViewerDemo/Inertia.xaml

<Page
    x:Class="Windows10.Controls.ScrollViewerDemo.Inertia"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.ScrollViewerDemo"
    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="10 0 10 10">

            <CheckBox Name="chkIsScrollInertiaEnabled" Content="IsScrollInertiaEnabled" IsChecked="True" Margin="5" />

            <!--
                Inertia: 慣性,在觸摸模式下,用一個加速度滾動內容,鬆手後內容會具有慣性滾動效果
                
                IsScrollInertiaEnabled - 是否啟用慣性效果,預設值為 true
            -->

            <ScrollViewer Name="scrollViewer" Width="400" Height="400" Margin="5" HorizontalAlignment="Left"
                          HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled"
                          HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible"
                          IsScrollInertiaEnabled="{Binding IsChecked, ElementName=chkIsScrollInertiaEnabled}">
                <Image Source="/Assets/StoreLogo.png" Width="1000" />
            </ScrollViewer>

        </StackPanel>
    </Grid>
</Page>


4、演示 ScrollViewer 的 Snap 特性
Controls/ScrollViewerDemo/Snap.xaml

<Page
    x:Class="Windows10.Controls.ScrollViewerDemo.Snap"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.ScrollViewerDemo"
    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="10 0 10 10">

            <!--用於設置 ScrollViewer 的 HorizontalSnapPointsType-->
            <ComboBox Name="comboBox" SelectedIndex="0" SelectionChanged="comboBox_SelectionChanged" HorizontalAlignment="Left" Margin="5">
                <ComboBoxItem>HorizontalSnapPointsType = SnapPointsType.None</ComboBoxItem>
                <ComboBoxItem>HorizontalSnapPointsType = SnapPointsType.Optional</ComboBoxItem>
                <ComboBoxItem>HorizontalSnapPointsType = SnapPointsType.Mandatory</ComboBoxItem>
            </ComboBox>

            <ScrollViewer x:Name="scrollViewer" Width="400" Height="200" HorizontalAlignment="Left" Margin="5"
                          HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled"
                          HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
                <StackPanel Orientation="Horizontal">
                    <Image Width="400" Height="200" Stretch="Fill" Source="/Assets/StoreLogo.png" />
                    <Image Width="400" Height="200" Stretch="Fill" Source="/Assets/StoreLogo.png" />
                    <Image Width="350" Height="200" Stretch="Fill" Source="/Assets/StoreLogo.png" />
                    <Image Width="450" Height="200" Stretch="Fill" Source="/Assets/StoreLogo.png" />
                    <Image Width="400" Height="200" Stretch="Fill" Source="/Assets/StoreLogo.png" />

                    <TextBox Width="400" Height="200" FontSize="24" Text="message1" Name="txtMsg1"  />
                    <TextBox Width="400" Height="200" FontSize="24" Text="message2" Name="txtMsg2"  />
                    <TextBox Width="400" Height="200" FontSize="24" Text="message3" Name="txtMsg3"  />
                </StackPanel>
            </ScrollViewer>

            <!--用於演示通過程式定位到 ScrollViewer 內的指定元素-->
            <Button Name="btnScroll" Content="滾動到 txtMsg2" Click="btnScroll_Click" Margin="5" />

        </StackPanel>
    </Grid>
</Page>

Controls/ScrollViewerDemo/Snap.xaml.cs

/*
 * Snap: 對齊,在觸摸模式下,如果 ScrollViewer 有多個元素,在滾動結束後會定位到其中某一個具體的元素,這就叫對齊
 * 
 * HorizontalSnapPointsType - 水平方向上的對齊行為,Windows.UI.Xaml.Controls.SnapPointsType枚舉
 *     SnapPointsType.None - 不需要對齊,預設值
 *     SnapPointsType.Optional - 看情況,如果離某個元素很近則對齊此元素
 *     SnapPointsType.Mandatory - 強制對齊,必須對齊到某一元素
 *     SnapPointsType.OptionalSingle - 僅對 Zoom 對齊有用(參看 /Controls/ScrollViewerDemo/Zoom.xaml)
 *     SnapPointsType.MandatorySingle - 僅對 Zoom 對齊有用(參看 /Controls/ScrollViewerDemo/Zoom.xaml)
 * VerticalSnapPointsType - 垂直方向上的對齊行為
 * 
 * 
 * HorizontalSnapPointsAlignment - 水平方向上的對齊方式,Windows.UI.Xaml.Controls.Primitives.SnapPointsAlignment枚舉
 *     SnapPointsAlignment.Near - 元素的左側對齊 ScrollViewer 的左側邊界,預設值
 *     SnapPointsAlignment.Center - 元素的中心點對齊 ScrollViewer 的中心點
 *     SnapPointsAlignment.Far - 元素的右側對齊 ScrollViewer 的右側邊界
 * VerticalSnapPointsAlignment - 垂直方向上的對齊方式
 * 
 * 
 * BringIntoViewOnFocusChange - ScrollViewer 內的某元素獲得焦點後,是否需要定位到此元素,預設值為 true
 */

using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.ScrollViewerDemo
{
    public sealed partial class Snap : Page
    {
        public Snap()
        {
            this.InitializeComponent();
            this.Loaded += Snap_Loaded;
        }

        void Snap_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            scrollViewer.HorizontalSnapPointsAlignment = Windows.UI.Xaml.Controls.Primitives.SnapPointsAlignment.Near;
            scrollViewer.BringIntoViewOnFocusChange = true;
        }

        private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (scrollViewer != null && comboBox != null)
            {
                switch (comboBox.SelectedIndex)
                {
                    case 0:
                        scrollViewer.HorizontalSnapPointsType = SnapPointsType.None;
                        break;
                    case 1:
                        scrollViewer.HorizontalSnapPointsType = SnapPointsType.Optional;
                        break;
                    case 2:
                        scrollViewer.HorizontalSnapPointsType = SnapPointsType.Mandatory;
                        break;
                    default:
                        scrollViewer.HorizontalSnapPointsType = SnapPointsType.None;
                        break;
                }
            }
        }

        private void btnScroll_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // 當 BringIntoViewOnFocusChange 為 true 時,如果 txtMsg2 獲得焦點,則 ScrollViewer 會自動滾動到 txtMsg2
            txtMsg2.Focus(Windows.UI.Xaml.FocusState.Programmatic);
        }
    }
}


5、演示 ScrollViewer 的 Zoom 特性
Controls/ScrollViewerDemo/Zoom.xaml

<Page
    x:Class="Windows10.Controls.ScrollViewerDemo.Zoom"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.ScrollViewerDemo"
    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="10 0 10 10">

            <Button Name="bntZoom" Click="bntZoom_Click" Content="放大/縮小到 0.5 倍" Margin="5" />
            
            <!--
                Zoom - 放大/縮小
            
                ZoomMode - 是否啟用“放大/縮小”功能(Disabled, Enabled),預設值為 Enabled
                MaxZoomFactor - 內容放大的最大倍數,預設值 10
                MinZoomFactor - 內容放大的最小倍數,預設值 0.1
            -->
            
            <ScrollViewer Name="scrollViewer" Width="400" Height="400" HorizontalAlignment="Left" Margin="5"
                          ZoomMode="Enabled" MaxZoomFactor="2" MinZoomFactor="0.5">
                <Image Source="/Assets/StoreLogo.png" Width="400" Height="400" />
            </ScrollViewer>

        </StackPanel>
    </Grid>
</Page>

Controls/ScrollViewerDemo/Zoom.xaml.cs

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.ScrollViewerDemo
{
    public sealed partial class Zoom : Page
    {
        public Zoom()
        {
            this.InitializeComponent();

            this.Loaded += Zoom_Loaded;
        }

        private void Zoom_Loaded(object sender, RoutedEventArgs e)
        {
            /*
             * ZoomSnapPoints - “放大/縮小”的對齊點的集合,預設是空的
             * 
             * ZoomSnapPointsType - “放大/縮小”的對齊行為
             *     SnapPointsType.None - 不需要對齊,預設值
             *     SnapPointsType.Optional - 看情況,如果離某個對齊點很近則對齊
             *     SnapPointsType.Mandatory - 強制對齊,必須對齊到某一個對齊點
             *     SnapPointsType.OptionalSingle - 同 Optional,但不能跳過對齊點
             *     SnapPointsType.MandatorySingle - 同 Mandatory,但不能跳過對齊點
             *     
             * IsZoomChainingEnabled - 是否啟用 Zoom 的 Chaining
             * IsZoomInertiaEnabled - 是否啟用 Zoom 的 Inertia
             * ZoomFactor - 獲取當前的 Zoom 的倍數
             * 
             * ZoomToFactor() - Zoom 到指定的倍數
             */

            scrollViewer.ZoomSnapPointsType = SnapPointsType.OptionalSingle;

            scrollViewer.ZoomSnapPoints.Add(0.5f);
            scrollViewer.ZoomSnapPoints.Add(0.8f);
            scrollViewer.ZoomSnapPoints.Add(1.0f);
            scrollViewer.ZoomSnapPoints.Add(1.5f);
            scrollViewer.ZoomSnapPoints.Add(2.0f);
        }

        private void bntZoom_Click(object sender, RoutedEventArgs e)
        {
            scrollViewer.ChangeView(null, null, 0.5f);
        }
    }
}



OK
[源碼下載]


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • find命令:用於在文件樹中查找文件,並作出相應的處理 1、find命令的格式; find pathname -options [-print -exec -ok ...] {}\ 2、find命令的參數; pathname: find命令所查找的目錄路徑。例如用“.”來表示當前目錄,用“/”來表示 ...
  • 功能說明:設置指令別名 語法:alias[別名]=[指令名稱] 詳解:1)不加參數,顯示已有的別名列表 2)設置別名:alias NAME='VALUE',使用NAME時相當於執行VALUE的內容 特別事項:1)定義的別名指針對當前Shell有效 2)要永久有效,需要定義配置文件 當前用戶:~/.b ...
  • 數組(http://www.cnblogs.com/afei-24/p/6738128.html)的大小是固定的。如果元素的個數是動態的,就應使用集合類。 列表(http://www.cnblogs.com/afei-24/p/6824791.html) 隊列(待寫) 棧(待寫) 鏈表(待寫) 有序 ...
  • .NET Framework為動態列表List提供泛型類List<T>。這個類實現了IList,ICollection,IEnumerable,IList<T>,ICollection<T>,IEnumerable<T>介面。1.創建列表 創建一個賽車手類,下麵的例子會用到: 調用預設的構造函數,就 ...
  • 一轉眼務農6年了,呆過大公司也去過小作坊,碼農的人生除了摳腚還是摳腚。在所有呆過的公司里,感覺項目沒有不延期的,真的是因為自己不努力嗎?也沒有呀!上班不怎麼聊QQ回家也很少看動作片,還搞過幾次通宵擼碼的。 以前總感覺是項目經理把工時估少了,後來自己也做過項目管理,按照以往的經歷估工時,做到最後還是會 ...
  • 1 public static class DataSetUtilities { 2 public static void SendDataSet(DataSet ds) { 3 if (ds == null) { 4 throw new ArgumentException("SendDataSet... ...
  • 什麼是架構,談下你項目中用的是什麼樣的架構?!! What?這個問題怎麼熟悉!!原來是在面試的時候經常面試官會問出這個問題。 架構是項目系統實施的整個思維過程。他不是一件容易的事。 需要架構師能夠充分把握業務需求,對行業及項目具有前瞻性的眼光,對抽象思維有精深的能力,應對變化有足夠的反應,這樣才... ...
  • 《Effective C#》快速筆記 - C# 中的動態編程 靜態類型和動態類型各有所長,靜態類型能夠讓編譯器幫你找出更多的錯誤,因為編譯器能夠在編譯時進行大部分的檢查工作。C# 是一種靜態類型的語言,不過它加入了動態類型的語言特性,可以更高效地解決問題。 本系列 《Effective C#》快速筆 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...