背水一戰 Windows 10 (55) - 控制項(集合類): ItemsControl - SemanticZoom, ISemanticZoomInformation

来源:http://www.cnblogs.com/webabcd/archive/2017/06/22/7063108.html
-Advertisement-
Play Games

背水一戰 Windows 10 之 控制項(集合類 - ItemsControl): SemanticZoom, ISemanticZoomInformation ...


[源碼下載]


背水一戰 Windows 10 (55) - 控制項(集合類): ItemsControl - SemanticZoom, ISemanticZoomInformation



作者:webabcd


介紹
背水一戰 Windows 10 之 控制項(集合類 - ItemsControl)

  • SemanticZoom
  • ISemanticZoomInformation



示例
1、SemanticZoom 的示例
Controls/CollectionControl/SemanticZoomDemo/SemanticZoomDemo.xaml

<Page
    x:Class="Windows10.Controls.CollectionControl.SemanticZoomDemo.SemanticZoomDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.CollectionControl.SemanticZoomDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    
    xmlns:common="using:Windows10.Common">

    <Grid Background="Transparent">

        <StackPanel Orientation="Horizontal" Margin="5" VerticalAlignment="Top">
            <Button Name="btnToggleActiveView" Content="ToggleActiveView" Click="btnToggleActiveView_Click" />
            <TextBlock Name="lblMsg" Margin="10 0 0 0" />
        </StackPanel>
        
        <!--
            SemanticZoom - SemanticZoom 控制項
                IsZoomOutButtonEnabled - 是否顯示用於切換視圖的按鈕(在 ZoomedInView 時,右下角會有個小按鈕),預設值為 false
                ZoomedInView - 放大後的視圖(需要實現 ISemanticZoomInformation 介面)
                ZoomedOutView - 縮小後的視圖(需要實現 ISemanticZoomInformation 介面)
                IsZoomedInViewActive - ZoomedInView 視圖是否為當前的活動視圖,預設值為 true
                CanChangeViews - 是否可以在兩個視圖之間切換(如果禁止的話則用戶將無法切換視圖,程式通過 IsZoomedInViewActive 或 ToggleActiveView() 來切換的話則會拋出異常),預設值為 true
        
            如果需要通過縮放手勢來切換 ZoomedInView 和 ZoomedOutView 的話,別忘了設置 ScrollViewer.ZoomMode="Enabled"
        
        
            註:
            1、ListViewBase 實現了 ISemanticZoomInformation 介面,其通過綁定 CollectionViewSource 數據即可使 SemanticZoom 中的兩個視圖進行有關聯的切換
            2、以下以 ListViewBase 和 SemanticZoom 和 CollectionViewSource 相結合為例,說明其行為
                a) ZoomedInView 用於顯示全部數據,包括組標題和每組的詳細數據,點擊組標題會進入到 ZoomedOutView 視圖
                b) ZoomedOutView 用於只顯示組標題,單擊組標題會進入到 ZoomedInView 視圖,並自動定位到對應的組
            3、關於如何自定義 ISemanticZoomInformation 實現,請參見 /Controls/CollectionControl/SemanticZoomDemo/ISemanticZoomInformationDemo.xaml
        -->

        <SemanticZoom Name="semanticZoom" Margin="5 50 5 5" 
                      ScrollViewer.ZoomMode="Enabled"
                      IsZoomOutButtonEnabled="True" IsZoomedInViewActive="False" CanChangeViews="True">
            
            <SemanticZoom.ZoomedInView>
                <GridView x:Name="gridViewDetails" Margin="5" ItemsSource="{x:Bind MyData.View}">
                    <GridView.ItemTemplate>
                        <DataTemplate x:DataType="common:NavigationModel">
                            <Grid Width="120" Background="Orange">
                                <TextBlock TextWrapping="Wrap" Text="{x:Bind Title}" />
                            </Grid>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                    <GridView.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate x:DataType="common:NavigationModel">
                                    <TextBlock Text="{x:Bind Title}" />
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                        </GroupStyle>
                    </GridView.GroupStyle>
                </GridView>
            </SemanticZoom.ZoomedInView>

            <SemanticZoom.ZoomedOutView>
                <GridView x:Name="gridViewSummary" Margin="5" ItemsSource="{x:Bind MyData.View.CollectionGroups}">
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <Grid Background="Orange" Width="100" Height="100">
                                <!--
                                    上下文數據為 ICollectionViewGroup 類型,其 Group 屬性保存著組對象(本例中 Group 就是每組的 NavigationModel 類型的對象)
                                    ListViewBase 實現了 ISemanticZoomInformation 介面,其在 StartViewChangeTo(SemanticZoomLocation source, SemanticZoomLocation destination) 時,通過 source.Item 獲取到的是一個 ICollectionViewGroup 類型的數據,其有兩個屬性:Group 和 GroupItems。關於 ISemanticZoomInformation 介面請參見:/Controls/CollectionControl/SemanticZoomDemo/MyFlipView.cs
                                -->
                                <TextBlock TextWrapping="Wrap" Text="{Binding Group.Title}" />
                            </Grid>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                </GridView>
            </SemanticZoom.ZoomedOutView>
            
        </SemanticZoom>
    </Grid>
</Page>

Controls/CollectionControl/SemanticZoomDemo/SemanticZoomDemo.xaml.cs

/*
 * SemanticZoom - SemanticZoom 控制項(繼承自 Control, 請參見 /Controls/BaseControl/ControlDemo/)
 *     ToggleActiveView() - 在 ZoomedInView, ZoomedOutView 兩個視圖之間切換
 *     ViewChangeStarted - 視圖切換開始時觸發的事件 
 *     ViewChangeCompleted - 視圖切換完成時觸發的事件
 *     
 * 
 * CollectionViewSource - 對集合數據啟用分組支持
 *     Source - 數據源
 *     View - 獲取視圖對象,返回一個實現了 ICollectionView 介面的對象
 *     IsSourceGrouped - 數據源是否是一個被分組的數據
 *     ItemsPath - 數據源中,子數據集合的屬性名稱
 *     
 * ICollectionView - 支持數據分組是 ICollectionView 的作用之一
 *     CollectionGroups - 組數據集合
 */

using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows10.Common;

namespace Windows10.Controls.CollectionControl.SemanticZoomDemo
{
    public sealed partial class SemanticZoomDemo : Page
    {
        public CollectionViewSource MyData
        {
            get
            {
                XElement root = XElement.Load("SiteMap.xml");
                var items = LoadData(root);

                // 構造數據源
                CollectionViewSource source = new CollectionViewSource();
                source.IsSourceGrouped = true;
                source.Source = items;
                source.ItemsPath = new PropertyPath("Items");

                return source;
            }
        }

        public SemanticZoomDemo()
        {
            this.InitializeComponent();
        }

        private void btnToggleActiveView_Click(object sender, RoutedEventArgs e)
        {
            semanticZoom.ToggleActiveView();
        }

        // 解析 xml 數據
        private List<NavigationModel> LoadData(XElement root)
        {
            if (root == null)
                return null;

            var items = from n in root.Elements("node")
                        select new NavigationModel
                        {
                            Title = (string)n.Attribute("title"),
                            Url = (string)n.Attribute("url"),
                            Items = LoadData(n)
                        };

            return items.ToList();
        }
    }
}


2、ISemanticZoomInformation 的示例
Controls/CollectionControl/SemanticZoomDemo/MyFlipView.cs

/*
 * 開發一個實現了 ISemanticZoomInformation 介面的自定義 FlipView
 * 由於本例僅用於 SemanticZoom 中 ZoomedInView 的演示,所以並沒有實現 ISemanticZoomInformation 的全部邏輯
 * 兩個 ISemanticZoomInformation 對象之間交互的核心邏輯就是通過 SemanticZoomLocation source 獲取數據,通過 SemanticZoomLocation destination 設置數據
 * 
 * 
 * ISemanticZoomInformation - 用於 SemanticZoom 的 ZoomedInView 和 ZoomedOutView(說明詳見本例中的註釋)
 * SemanticZoomLocation - 用於設置或獲取 ISemanticZoomInformation 在 SemanticZoom 中的狀態(說明詳見本例中的註釋)
 */

using Windows.UI.Xaml.Controls;
using Windows10.Common;

namespace Windows10.Controls.CollectionControl.SemanticZoomDemo
{
    public class MyFlipView : FlipView, ISemanticZoomInformation
    {
        public MyFlipView()
            : base()
        {

        }

        /// <summary>
        /// 視圖完成了變化後調用,比如視圖完成了顯示或隱藏之後都會調用這個(與 InitializeViewChange() 是一對)
        /// </summary>
        public void CompleteViewChange() { }

        /// <summary>
        /// 完成 ZoomedInView -> ZoomedOutView 的切換時調用(與 StartViewChangeFrom() 是一對)
        /// </summary>
        public void CompleteViewChangeFrom(SemanticZoomLocation source, SemanticZoomLocation destination) { }

        /// <summary>
        /// 完成 ZoomedOutView -> ZoomedInView 的切換時調用(與 StartViewChangeTo() 是一對)
        /// </summary>
        public void CompleteViewChangeTo(SemanticZoomLocation source, SemanticZoomLocation destination) { }

        /// <summary>
        /// 視圖將要發生變化時調用,比如視圖將要被顯示或將要被隱藏之前都會先調用這個(與 CompleteViewChange() 是一對)
        /// </summary>
        public void InitializeViewChange() { }

        /// <summary>
        /// 是否為活動視圖
        /// </summary>
        public bool IsActiveView { get; set; }

        /// <summary>
        /// 是否為 ZoomedInView 視圖
        /// </summary>
        public bool IsZoomedInView { get; set; }

        /// <summary>
        /// 所屬的 SemanticZoom
        /// </summary>
        public SemanticZoom SemanticZoomOwner { get; set; }

        /// <summary>
        /// 開始 ZoomedInView -> ZoomedOutView 的切換時調用(與 CompleteViewChangeFrom() 是一對)
        /// </summary>
        public void StartViewChangeFrom(SemanticZoomLocation source, SemanticZoomLocation destination) { }

        /// <summary>
        /// 開始 ZoomedOutView -> ZoomedInView 的切換時調用(與 CompleteViewChangeTo() 是一對)
        /// </summary>
        /// <param name="source">在 ZoomedOutView 時被選中的數據</param>
        /// <param name="destination">需要傳遞給 ZoomedInView 的數據</param>
        public void StartViewChangeTo(SemanticZoomLocation source, SemanticZoomLocation destination)
        {
            /*
             * 註:
             * 1、ListViewBase 實現了 ISemanticZoomInformation 介面,其通過綁定 CollectionViewSource 數據即可使 SemanticZoom 中的兩個視圖進行有關聯地切換,參見 /Controls/CollectionControl/SemanticZoomDemo/SemanticZoomDemo.xaml
             * 2、對於 ListViewBase 來說,它運行到這裡時,通過 source.Item 獲取到的是一個 ICollectionViewGroup 類型的數據,其有兩個屬性:Group 和 GroupItems
             */


            // 獲取在 ZoomedOutView 中被選中的項,即被選中的父親
            NavigationModel model = source.Item as NavigationModel;

            // 將此父親的所有子數據傳遞給 ZoomedInView,接下來會執行 MakeVisible() 方法
            destination.Item = model.Items;
        }

        /// <summary>
        /// 開始 ZoomedOutView -> ZoomedInView 之後,會調用此方法
        /// 一般在此處重整 ZoomedInView 的數據源,或者滾動 ZoomedInView 中的內容到指定的項以對應 ZoomedOutView 中被選中的數據
        /// </summary>
        /// <param name="item">由 StartViewChangeTo() 方法傳遞給 ZoomedInView 的數據</param>
        public void MakeVisible(SemanticZoomLocation item)
        {
            // 將 FlipView 的數據源指定為被選中的父親的所有子數據
            this.ItemsSource = item.Item;
        }
    }
}

Controls/CollectionControl/SemanticZoomDemo/ISemanticZoomInformationDemo.xaml

<Page
    x:Class="Windows10.Controls.CollectionControl.SemanticZoomDemo.ISemanticZoomInformationDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.CollectionControl.SemanticZoomDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">

        <Button Name="btnDisplayZoomedOutView" Content="切換至 ZoomedInView 視圖" Click="btnDisplayZoomedOutView_Click" VerticalAlignment="Top" Margin="10 0 10 10" />
        
        <SemanticZoom x:Name="semanticZoom" IsZoomedInViewActive="False" Margin="10 50 10 10">

            <SemanticZoom.ZoomedInView>
                <local:MyFlipView Name="flipView" Width="600" Height="300" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <FlipView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Title}" FontSize="32" />
                        </DataTemplate>
                    </FlipView.ItemTemplate>
                    <FlipView.ItemContainerStyle>
                        <Style TargetType="FlipViewItem">
                            <Setter Property="Background" Value="Blue" />
                        </Style>
                    </FlipView.ItemContainerStyle>
                </local:MyFlipView>
            </SemanticZoom.ZoomedInView>

            <SemanticZoom.ZoomedOutView>
                <GridView Name="gridView">
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <Grid Background="Orange" Width="100" Height="100">
                                <TextBlock TextWrapping="Wrap" Text="{Binding Title}" />
                            </Grid>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                </GridView>
            </SemanticZoom.ZoomedOutView>
            
        </SemanticZoom>
        
    </Grid>
</Page>

Controls/CollectionControl/SemanticZoomDemo/ISemanticZoomInformationDemo.xaml.cs

/*
 * 演示 SemanticZoom 如何與自定義的 ISemanticZoomInformation 類結合使用(本例開發了一個實現了 ISemanticZoomInformation 介面的自定義 FlipView,參見 MyFlipView.cs)
 * ZoomedInView 用自定義的 FlipView 演示,ZoomedOutView 用 GridView 演示
 * 
 * 
 * 註:
 * ListViewBase 實現了 ISemanticZoomInformation 介面,所以可以在 SemanticZoom 的兩個視圖間有關聯地切換。如果想讓其它控制項也實現類似的功能,就必須使其實現 ISemanticZoomInformation 介面
 */

using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Windows.UI.Xaml.Controls;
using Windows10.Common;

namespace Windows10.Controls.CollectionControl.SemanticZoomDemo
{
    public sealed partial class ISemanticZoomInformationDemo : Page
    {
        public ISemanticZoomInformationDemo()
        {
            this.InitializeComponent();
            XElement root = XElement.Load("SiteMap.xml");
            var items = LoadData(root);

            // 綁定數據
            gridView.ItemsSource = items;
        }

        // 獲取數據
        private List<NavigationModel> LoadData(XElement root)
        {
            if (root == null)
                return null;

            var items = from n in root.Elements("node")
                        select new NavigationModel
                        {
                            Title = (string)n.Attribute("title"),
                            Url = (string)n.Attribute("url"),
                            Items = LoadData(n)
                        };

            return items.ToList();
        }

        private void btnDisplayZoomedOutView_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            semanticZoom.IsZoomedInViewActive = false;
        }
    }
}



OK
[源碼下載]


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

-Advertisement-
Play Games
更多相關文章
  • // 1. asp頁面使用EasyUI框架需要的Css樣式和JS <script src="../script/jquery-easyui-1.4.5/jquery.min.js" type="text/javascript" charset = "utf-8"></script> <script ...
  • 把一個 dic綁定到了listview上,有時候下拉列表會報這個異常。因為直接使用了itemssource = dic,而dic在另一個線程上不定期更新,這樣如果直接綁定的話就會報這個錯誤,原因是直接綁定的話會把itemssource的記憶體地址直接指向dic的記憶體地址,當dic更新後,會導致記憶體地址 ...
  • 訂單系統設計 總體設計 1.每次下單時間少於3秒 2.庫存驗證不存在多買的情況 3.訂單能夠按照不同供應商進程拆分 4. 物流信息能夠回傳 訂單狀態機設計 1.待系統審核 2.待支付 3.待發貨 4.待簽收 5.已完成 6.訂單關閉 訂單狀態流轉如下圖示: 1)審核失敗 2)未支付(待支付24小時) ...
  • 1.根據單個分隔字元用split截取 例如 即可得到sArray[0]="GT123",sArray[1]="1"; 2.利用多個字元來分隔字元串 例如 得到sArray[0]="GTAZB",sArray[1]="Jiang",sArray[2]="Ben",sArray[3]="123"; 3根 ...
  • 在前端 UI 開發中,有時,我們會遇到這樣的需求:在一個 ScrollViewer 中有很多內容,而我們需要實現在執行某個操作後能夠定位到其中指定的控制項處;這很像在 HTML 頁面中點擊一個鏈接後定位到當前網頁上的某個 anchor。 要實現它,首先我們需要看 ScrollViewer 為我們提供的 ...
  • TypeInfo,PropertyInfo,MethodInfo,FieldInfo ...
  • 在這篇博客中,我們將介紹如下內容: ==運算符與基元類型 ==運算符與引用類型 ==運算符與String類型 ==運算符與值類型 ==運算符與泛型 ==運算符與基元類型 我們分別用兩種方式比較兩個整數,第一個使用的是Equals(int)方法,每二個使用的是==運算符: 運行上面的示例,兩個語句出的 ...
  • 微軟利用OAuth2為RESTful API提供了完整的鑒權機制,但是可能微軟保姆做的太完整了,在這個機制中指定了數據持久化的方法是用EF,而且對於用戶、許可權等已經進行了封裝,對於系統中已經有了自己的用戶表,和不是採用EF做持久化的系統來說限制太大,不自由,而且現實中很多情況下,授權伺服器和資源服務 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...