Windows 8 Store Apps學習(40) 剪下板操作

來源:互聯網
上載者:User

剪下板: 複製/粘貼文本, html, 圖片, 檔案

介紹

重新想象 Windows 8 Store Apps 之 剪下板

Clipboard - 剪下板

複製/粘貼文本

複製/粘貼html

複製/粘貼圖片

複製/粘貼檔案

樣本

1、示範剪下板的基本應用

Clipboard/Demo.xaml

<Page    x:Class="XamlDemo.Clipboard.Demo"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:XamlDemo.Clipboard"    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" />                <Button Name="btnCopyText" Content="複製一段文本到剪下板" Click="btnCopyText_Click_1" Margin="0 10 0 0" />                <Button Name="btnPasteText" Content="粘貼剪下板中的文本" Click="btnPasteText_Click_1" Margin="0 10 0 0" />                <Button Name="btnShowAvailableFormats" Content="擷取剪下板中包含的資料的格式類型" Click="btnShowAvailableFormats_Click_1" Margin="0 10 0 0" />                            <Button Name="btnClear" Content="清除剪下板中的全部內容" Click="btnClear_Click_1" Margin="0 10 0 0" />            </StackPanel>    </Grid></Page>

Clipboard/Demo.xaml.cs

/* * Clipboard - 剪下板 *     SetContent() - 將指定的 DataPackage 存入剪下板 *     GetContent() - 從剪下板中擷取 DataPackage 對象 *     Clear() - 清除剪下板中的全部資料 *     Flush() - 正常情況下,關閉 app 後,此 app 儲存到剪下板的資料就會消失;調用此方法後,即使關閉 app,剪下板中的資料也不會消失 *     ContentChanged - 剪下板中的資料發生變化時所觸發的事件 *      * DataPackage - 用於封裝 Clipboard 或 ShareContract 的資料(詳細說明見 ShareContract 的 Demo) *     SetText(), SetUri(), SetHtmlFormat(), SetRtf(), SetBitmap(), SetStorageItems(), SetData(), SetDataProvider() - 設定複製到剪下板的各種格式的資料(註:一個 DataPackage 可以有多種不同格式的資料) *     RequestedOperation - 操作類型(DataPackageOperation 枚舉: None, Copy, Move, Link),沒發現此屬性有任何作用 *      * DataPackageView - DataPackage 對象的唯讀版本,從剪下板擷取資料或者共用目標接收資料均通過此對象來擷取 DataPackage 對象的資料(詳細說明見 ShareContract 的 Demo) */    using System;using Windows.ApplicationModel.DataTransfer;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation;    namespace XamlDemo.Clipboard{    public sealed partial class Demo : Page    {        public Demo()        {            this.InitializeComponent();        }            protected override void OnNavigatedTo(NavigationEventArgs e)        {            Windows.ApplicationModel.DataTransfer.Clipboard.ContentChanged += Clipboard_ContentChanged;        }            protected override void OnNavigatedFrom(NavigationEventArgs e)        {            Windows.ApplicationModel.DataTransfer.Clipboard.ContentChanged -= Clipboard_ContentChanged;        }            void Clipboard_ContentChanged(object sender, object e)        {            lblMsg.Text += Environment.NewLine;            lblMsg.Text += "剪下板中的內容發生了變化";        }            // 複製一段文本到剪下板        private void btnCopyText_Click_1(object sender, RoutedEventArgs e)        {            // 構造儲存到剪下板的 DataPackage 對象            DataPackage dataPackage = new DataPackage();            dataPackage.SetText("I am webabcd: " + DateTime.Now.ToString());                try            {                Windows.ApplicationModel.DataTransfer.Clipboard.SetContent(dataPackage); // 儲存 DataPackage 對象到剪下板                Windows.ApplicationModel.DataTransfer.Clipboard.Flush(); // 當此 app 關閉後,依然保留剪下板中的資料                lblMsg.Text = "已將內容複寫到剪下板";            }            catch (Exception ex)            {                lblMsg.Text = ex.ToString();            }        }            // 顯示剪下板中的文本資料        private async void btnPasteText_Click_1(object sender, RoutedEventArgs e)        {            // 擷取剪下板中的資料            DataPackageView dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();                // 如果剪下板中有文本資料,則擷取並顯示該文本            if (dataPackageView.Contains(StandardDataFormats.Text))            {                try                {                    string text = await dataPackageView.GetTextAsync();                    lblMsg.Text = text;                }                catch (Exception ex)                {                    lblMsg.Text = ex.ToString();                }            }            else            {                lblMsg.Text = "剪下板中無常值內容";            }        }            // 顯示剪下板中包含的資料的格式類型,可能會有 StandardDataFormats 枚舉的格式,也可能會有自訂的格式(關於自訂格式可以參見:ShareContract 的 Demo)        private void btnShowAvailableFormats_Click_1(object sender, RoutedEventArgs e)        {            DataPackageView dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();            if (dataPackageView != null && dataPackageView.AvailableFormats.Count > 0)            {                var availableFormats = dataPackageView.AvailableFormats.GetEnumerator();                while (availableFormats.MoveNext())                {                    lblMsg.Text += Environment.NewLine;                    lblMsg.Text += availableFormats.Current;                }            }            else            {                lblMsg.Text = "剪下板中無任何內容";            }        }            // 清除剪下板中的全部資料        private void btnClear_Click_1(object sender, RoutedEventArgs e)        {            Windows.ApplicationModel.DataTransfer.Clipboard.Clear();        }    }}

2、示範如何複製 html 資料到剪下板,以及如何從剪下板中擷取 html 資料

Clipboard/CopyHtml.xaml

<Page    x:Class="XamlDemo.Clipboard.CopyHtml"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:XamlDemo.Clipboard"    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" />                <Button Name="btnCopyHtml" Content="複製一段 html 到剪下板" Click="btnCopyHtml_Click_1" Margin="0 10 0 0" />                <Button Name="btnPasteHtml" Content="粘貼剪下板中的 html" Click="btnPasteHtml_Click_1" Margin="0 10 0 0" />            </StackPanel>    </Grid></Page>

Clipboard/CopyHtml.xaml.cs

/* * 示範如何複製 html 資料到剪下板,以及如何從剪下板中擷取 html 資料  *  * HtmlFormatHelper - 在 Clipboard 中傳遞 html 資料或在 ShareContract 中傳遞 html 資料時的協助類 *     CreateHtmlFormat() - 封裝需要傳遞的 html 字串,以便以 html 方式傳遞資料 *     GetStaticFragment() - 解鎖裝傳遞過來的經過封裝的 html 資料,從而擷取初始需要傳遞的 html 字串 */    using System;using Windows.ApplicationModel.DataTransfer;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;    namespace XamlDemo.Clipboard{    public sealed partial class CopyHtml : Page    {        public CopyHtml()        {            this.InitializeComponent();        }            // 複製 html 字串到剪下板        private void btnCopyHtml_Click_1(object sender, RoutedEventArgs e)        {            DataPackage dataPackage = new DataPackage();            // 封裝一下需要複製的 html 資料,以便以 html 的方式將資料複製到剪下板            string htmlFormat = HtmlFormatHelper.CreateHtmlFormat("<body>I am webabcd</body>");            dataPackage.SetHtmlFormat(htmlFormat);                try            {                Windows.ApplicationModel.DataTransfer.Clipboard.SetContent(dataPackage);                lblMsg.Text = "已將內容複寫到剪下板";            }            catch (Exception ex)            {                lblMsg.Text = ex.ToString();            }        }            // 顯示剪下板中的 html 資料        private async void btnPasteHtml_Click_1(object sender, RoutedEventArgs e)        {            DataPackageView dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();                if (dataPackageView.Contains(StandardDataFormats.Html))            {                try                {                    // 封裝後的資料                    string htmlFormat = await dataPackageView.GetHtmlFormatAsync();                    // 封裝前的資料                    string htmlFragment = HtmlFormatHelper.GetStaticFragment(htmlFormat);                        lblMsg.Text = "htmlFormat(封裝後的資料): ";                    lblMsg.Text += Environment.NewLine;                    lblMsg.Text += htmlFormat;                    lblMsg.Text += Environment.NewLine;                    lblMsg.Text += Environment.NewLine;                    lblMsg.Text += "htmlFragment(封裝前的資料): ";                    lblMsg.Text += Environment.NewLine;                    lblMsg.Text += htmlFragment;                }                catch (Exception ex)                {                    lblMsg.Text = ex.ToString();                }            }            else            {                lblMsg.Text = "剪下板中無 html 內容";            }        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.