New UWP Community Toolkit - RotatorTile

来源:https://www.cnblogs.com/shaomeng/archive/2018/04/07/RotatorTile.html
-Advertisement-
Play Games

UWP Community Toolkit 中有一個為圖片或磁貼提供輪播效果的控制項 - RotatorTile,本篇我們結合代碼詳細講解 RotatorTile 的實現。 RotatorTile 提供了一種類似 Windows 10 磁貼的輪播方式,可以輪流播放開發者設置的內容序列,支持設置輪... ...


概述

UWP Community Toolkit  中有一個為圖片或磁貼提供輪播效果的控制項 - RotatorTile,本篇我們結合代碼詳細講解  RotatorTile 的實現。

RotatorTile 提供了一種類似 Windows 10 磁貼的輪播方式,可以輪流播放開發者設置的內容序列,支持設置輪播方向,包括上下左右四個方向;接下來看看官方示例的截圖:

Source: https://github.com/Microsoft/UWPCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI.Controls/RotatorTile

Doc: https://docs.microsoft.com/zh-cn/windows/uwpcommunitytoolkit/controls/rotatortile

Namespace: Microsoft.Toolkit.Uwp.UI.Controls; Nuget: Microsoft.Toolkit.Uwp.UI.Controls;

 

開發過程

代碼分析

RotatorTile 控制項包括 RotatorTile.cs 和 RotatorTile.xaml,分別是控制項的定義處理類和樣式文件,分別來看一下:

1. RotatorTile.xaml

RotatorTile.xaml 是 RotatorTile 控制項的樣式文件,我們看 Template 部分,輪播效果的實現主要是靠 StackPanel 中排列的兩個 Content,分別代表 current 和 next 內容,根據設置的輪播方向,設置 StackPanel 的排列方向;輪播時,使用 TranslateTransform 來實現輪播的元素切換動畫;

<Style TargetType="controls:RotatorTile">
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:RotatorTile">
                <Grid Background="{TemplateBinding Background}">
                    <Canvas x:Name="Scroller"
                            DataContext="{x:Null}">
                        <StackPanel x:Name="Stack">
                            <StackPanel.RenderTransform>
                                <TranslateTransform x:Name="Translate" Y="0" />
                            </StackPanel.RenderTransform>
                            <ContentPresenter x:Name="Current"
                                                Content="{Binding}"
                                                ContentTemplate="{TemplateBinding ItemTemplate}"
                                                DataContext="{x:Null}" />
                            <ContentPresenter x:Name="Next"
                                                Content="{Binding}"
                                                ContentTemplate="{TemplateBinding ItemTemplate}"
                                                DataContext="{x:Null}" />
                        </StackPanel>
                    </Canvas>
                    <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="RotationDelay" Value="0:0:5" />
    <Setter Property="ExtraRandomDuration" Value="0:0:5" />
</Style>

2. RotatorTile.cs

RotatorTile 控制項的定義和主要處理類,來看看類的結構:

 

首先看一下 OnApplyTemplate() 方法,他會獲取控制項的模板,根據當前輪播方向處理 StackPanel 容器,初始化並開始輪播動畫;這也是 RotatorTile 控制項的主要流程:使用 Timer,根據設置的間隔時間和輪播的方向,在 Tick 事件中不斷按照某個方向去做平移動畫,動畫中不斷更新當前顯示元素為下一個元素,並不斷相應中途的顯示元素集合變化事件;

同時控制項會響應 RotatorTile_SizeChanged 事件,根據新的尺寸去修改顯示元素和容器的尺寸;響應 RotatorTile_Loaded 和 RotatorTile_Unloaded,處理 Timer 的開始和結束處理;

RotatorTile.cs 繼承自 Control 類,先看一下它定義了哪些依賴屬性:

  • ExtraRandomDuration - 一個隨機時間區間的上限,輪播時一個 0~ExtraRandomDuration 的隨機值會被作為輪播間隔使用; 
  • RotationDelay - 輪播的間隔,時間修改時會觸發 OnRotationDelayInSecondsPropertyChanged 事件;
  • ItemsSource - 輪播內容集合的數據源,變化時觸發 OnItemsSourcePropertyChanged 事件;
  • ItemTemplate - 輪播內容的內容模板;
  • RotateDirection - 輪播的方向,分別上 下 左 右四個方向;
  • CurrentItem - 輪播時當前的 Item,變化時觸發 OnCurrentItemPropertyChanged 事件;

首先來看 OnItemsSourcePropertyChanged 事件,它的主要邏輯在方法 Incc_CollectionChanged(s, e) 中:

  • 首先 action 處理會被分為 5 種:Remove,Add,Replace,Move 和 Reset;
  • 對 Remove action,根據刪除後的開始索引與當前索引,結束索引之間的關係,去更新下一個元素,或設置當前索引,或更新上下文;
  • 對 Add action,根據添加後的開始索引與當前索引的關係,以及當前索引與 0 的關係,去開始輪播,或設置當前索引,或更新上下文;
  • 對 Replace action,如果當前索引介於新的開始索引和結束索引之間,則更新下一個元素;
  • 對 Move action,如果當前索引介於新的開始索引和結束索引之間,獲取它的新索引;
  • 對 Reset action,重新開始輪播;
private void Incc_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.Action == NotifyCollectionChangedAction.Remove)
    {
        if (e.OldItems?.Count > 0)
        {
            int endIndex = e.OldStartingIndex + e.OldItems.Count;
            if (_currentIndex >= e.NewStartingIndex && _currentIndex < endIndex)
            {
                // Current item was removed. Replace with the next one
                UpdateNextItem();
            }
            else if (_currentIndex > endIndex)
            {
                // Items were removed before the current item. Just update the changed index
                _currentIndex -= (endIndex - e.NewStartingIndex) - 1;
            }
            else if (e.NewStartingIndex == _currentIndex + 1)
            {
                // Upcoming item was changed, so update the datacontext
                _nextElement.DataContext = GetNext();
            }
        }
    }
    else if (e.Action == NotifyCollectionChangedAction.Add)
    {
        int endIndex = e.NewStartingIndex + e.NewItems.Count;
        if (e.NewItems?.Count > 0)
        {
            if (_currentIndex < 0)
            {
                // First item loaded. Start the rotator
                Start();
            }
            else if (_currentIndex >= e.NewStartingIndex)
            {
                // Items were inserted before the current item. Update the index
                _currentIndex += e.NewItems.Count;
            }
            else if (_currentIndex + 1 == e.NewStartingIndex)
            {
                // Upcoming item was changed, so update the datacontext
                _nextElement.DataContext = GetNext();
            }
        }
    }
    else if (e.Action == NotifyCollectionChangedAction.Replace)
    {
        int endIndex = e.OldStartingIndex + e.OldItems.Count;
        if (_currentIndex >= e.OldStartingIndex && _currentIndex < endIndex + 1)
        {
            // Current item was removed. Replace with the next one
            UpdateNextItem();
        }
    }
    else if (e.Action == NotifyCollectionChangedAction.Move)
    {
        int endIndex = e.OldStartingIndex + e.OldItems.Count;
        if (_currentIndex >= e.OldStartingIndex && _currentIndex < endIndex)
        {
            // The current item was moved. Get its new location
            _currentIndex = GetIndexOf(CurrentItem);
        }
    }
    else if (e.Action == NotifyCollectionChangedAction.Reset)
    {
        // Significant change or clear. Restart.
        Start();
    }
}

接著來看 OnCurrentItemPropertyChanged(d, e) 方法的處理,主要處理邏輯在 RotateToNextItem() 中:

  • 首先判斷是否有兩個或者更多的元素,如果沒有則退出處理;
  • 定義 Storyboard,動畫時間是 500ms,方向和輪播的目標屬性根據當前輪播的方向去計算;
  • 在動畫結束時,開始準備下一個顯示的元素;
private void RotateToNextItem()
{
    // Check if there's more than one item. if not, don't start animation
    bool hasTwoOrMoreItems = false;
    ...

    if (!hasTwoOrMoreItems) { return;}

    var sb = new Storyboard();
    if (_translate != null)
    {
        var anim = new DoubleAnimation
        {
            Duration = new Duration(TimeSpan.FromMilliseconds(500)),
            From = 0
        };
        if (Direction == RotateDirection.Up)
        {
            anim.To = -ActualHeight;
        }
        else if (Direction == RotateDirection.Down) {...}
        else if (Direction == RotateDirection.Right) {...}
        else if (Direction == RotateDirection.Left) {...}

        anim.FillBehavior = FillBehavior.HoldEnd;
        anim.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
        Storyboard.SetTarget(anim, _translate);
        if (Direction == RotateDirection.Up || Direction == RotateDirection.Down)
        {
            Storyboard.SetTargetProperty(anim, "Y");
        }
        else
        {
            Storyboard.SetTargetProperty(anim, "X");
        }

        sb.Children.Add(anim);
    }

    sb.Completed += async (a, b) =>
    {
        if (_currentElement != null)
        {
            _currentElement.DataContext = _nextElement.DataContext;
        }

        // make sure DataContext on _currentElement has had a chance to update the binding
        // avoids flicker on rotation
        await System.Threading.Tasks.Task.Delay(50);

        // Reset back and swap images, getting the next image ready
        sb.Stop();
        if (_translate != null)
        {
            UpdateTranslateXY();
        }

        if (_nextElement != null)
        {
            _nextElement.DataContext = GetNext(); // Preload the next tile
        }
    };
    sb.Begin();
}

我們看到有兩個方法中都調用了 UpdateTranslateXY() 方法,來更新平移時的 X 或 Y:

對於 Left 和 Up,只需要充值 X 或 Y 為 0;對於 Right 和 Down,需要把對應的 X 或 Y 設置為 -1 × 對應的高度或寬度,讓動畫從負一倍尺寸平移到 0;

private void UpdateTranslateXY()
{
    if (Direction == RotateDirection.Left || Direction == RotateDirection.Up)
    {
        _translate.X = _translate.Y = 0;
    }
    else if (Direction == RotateDirection.Right)
    {
        _translate.X = -1 * ActualWidth;
    }
    else if (Direction == RotateDirection.Down)
    {
        _translate.Y = -1 * ActualHeight;
    }
}

 

調用示例

我們定義了一個 RotatorTile,動畫間隔 1s,方向向上,來看一下 gif 圖顯示的運行結果:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <controls:RotatorTile x:Name="Tile1"
                                Height="200"
                                Background="LightGray"
                                RotationDelay="0:0:1"
                                ExtraRandomDuration="0:0:1"
                                Direction="Up"
                                ItemTemplate="{StaticResource PhotoTemplate}" />
</Grid>

 

總結

到這裡我們就把 UWP Community Toolkit 中的 RotatorTile 控制項的源代碼實現過程和簡單的調用示例講解完成了,希望能對大家更好的理解和使用這個控制項有所幫助。歡迎大家多多交流,謝謝!

最後,再跟大家安利一下 UWPCommunityToolkit 的官方微博:https://weibo.com/u/6506046490大家可以通過微博關註最新動態。

衷心感謝 UWPCommunityToolkit 的作者們傑出的工作,Thank you so much, UWPCommunityToolkit authors!!!

 


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

-Advertisement-
Play Games
更多相關文章
  • 聊到二進位以及位運算就不得不說說,原碼,反碼,補碼了,網上對於原碼反碼補碼的解釋過於複雜,我這裡把教程里的一些總結搬出來讓大家參考一下:對於有符號的而言; 1.二進位最高位是符號位,0表示正數,1表示負數; 2.正數的原碼反碼補碼都一樣; 3.負數的反碼等於它的原碼符號位不變,其他位取反,1變0,0 ...
  • 參考資料:網易雲網課李興華:http://study.163.com/course/courseMain.htm?courseId=1455026 一、字元串一旦定義不可改變 一開始也許並不太好理解,先觀察以下代碼 結果: 以上代碼似乎主觀上覺得String內容不是改變了嗎,但並不是這樣的,下麵通過 ...
  • 回顧TCP粘包/拆包問題解決方案 上文詳細說了TCP粘包/拆包問題產生的原因及解決方式,並以LineBasedFrameDecoder為例演示了粘包/拆包問題的實際解決方案,本文再介紹兩種粘包/拆包問題的解決方案:分隔符和定長解碼器。在開始本文之前,先回顧一下解決粘包/拆包問題的幾個方式: 消息長度 ...
  • 當需要定時修改資料庫時,一般我們都選擇起一個定時進程去改庫。如果將這種定時任務寫入業務中,寫成一個介面呢,定時進程顯得有些不太合適?如果需要定時修改100次資料庫,常規做法會啟動100個進程,雖然這種進程非常輕量級,但還是會感覺不爽。實際上我們可以使用threading.Timer創建相應的線程來執 ...
  • 概述 UWP Community Toolkit 中有一個開發者工具集 DeveloperTools,可以幫助開發者在開發過程中進行 UI 和功能的調試,本篇我們結合代碼詳細講解 DeveloperTools 的實現。 DeveloperTools 中目前包括了兩個工具: AlignmentGrid ...
  • 通常說,a++是先取值後運算,++a是先運算後取值。 ++ 是一個“自增運算符”,自增運算符有兩種形式:首碼自增(++a)和尾碼自增(a++)。 運算符和操作數合起來就是一個表達式(a++、++a都是表達式,a就是操作數)。註意:每一個表達式本身都有值(和其類型),有的表達式還有“副作用”。比如自增 ...
  • 沒有找到可以直接禁止的屬性,但是找到兩個間接禁止的方式。 方式一: //onClickRow: function (rowIndex, rowData) { // $(this).datagrid('unselectRow', rowIndex); //}, 方式二: onClickRow: fun ...
  • 1、兩種種註釋符: //單行註釋; /* */多行註釋 2、幾種常見的變數: int (整數) double(浮點數)decimal(常用於金錢這個精密計算) string(字元串 )char(單個字元) 變數的命名:以字母或者下劃線開始,但命名要有意義,便於理解;Pascal規範用於類或方法的命名 ...
一周排行
    -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 ...