首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

Windows 8 開發入門(十四):剪貼簿操作全攻略——文字、圖片、HTML、檔案四種格式

     編輯:關於C#

Windows 8 DataPackage 支援四種標準資料格式互傳:文字、位圖、HTML、StorageItems(檔案)。本文示範各格式的複製與貼上完整流程。

共用資料封裝物件

DataPackage dp = new DataPackage(); // 中繼載體

1. 文字(Text)

// 複製文字
private void CopyText_Click(object sender, RoutedEventArgs e) {
    dp.SetText(SourceText.Text);
    Clipboard.SetContent(dp);
}

// 貼上文字
private async void PasteText_Click(object sender, RoutedEventArgs e) {
    var content = Clipboard.GetContent();
    if (content.Contains(StandardDataFormats.Text)) {
        TargetText.Text = await content.GetTextAsync();
    }
}

2. 圖片(Bitmap)

// 複製圖片(從專案資產載入)
private async void CopyImage_Click(object sender, RoutedEventArgs e) {
    var uri = new Uri("ms-appx:///Assets/iphone0426_006.jpg");
    var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
    dp.SetBitmap(RandomAccessStreamReference.CreateFromFile(file));
    Clipboard.SetContent(dp);
}

// 貼上圖片
private async void PasteImage_Click(object sender, RoutedEventArgs e) {
    var content = Clipboard.GetContent();
    if (content.Contains(StandardDataFormats.Bitmap)) {
        var streamRef = await content.GetBitmapAsync();
        var stream = await streamRef.OpenReadAsync();
        var bitmap = new BitmapImage();
        bitmap.SetSource(stream);
        TargetImage.Source = bitmap;
    }
}

3. HTML

// 複製 HTML(從 WebView 取得)
private void CopyHtml_Click(object sender, RoutedEventArgs e) {
    var html = HtmlFormatHelper.CreateHtmlFormat(
        SourceHtml.InvokeScript("eval", new[] { "document.documentElement.outerHTML;" }));
    dp.SetHtmlFormat(html);
    Clipboard.SetContent(dp);
}

// 貼上 HTML
private async void PasteHtml_Click(object sender, RoutedEventArgs e) {
    var content = Clipboard.GetContent();
    if (content.Contains(StandardDataFormats.Html)) {
        var html = await content.GetHtmlFormatAsync();
        TargetHtml.NavigateToString(html);
    }
}

4. 檔案

// 複製多個檔案
private async void CopyFile_Click(object sender, RoutedEventArgs e) {
    var files = new List<StorageFile>();
    var uri1 = new Uri("ms-appx:///Assets/iphone0426_006.jpg");
    var uri2 = new Uri("ms-appx:///Assets/iphone0426_010.jpg");
    files.Add(await StorageFile.GetFileFromApplicationUriAsync(uri1));
    files.Add(await StorageFile.GetFileFromApplicationUriAsync(uri2));
    dp.SetStorageItems(files);
    Clipboard.SetContent(dp);
}

// 貼上檔案(複製到文件庫)
private async void PasteFile_Click(object sender, RoutedEventArgs e) {
    var content = Clipboard.GetContent();
    TargetFile.Text = "";
    if (content.Contains(StandardDataFormats.StorageItems)) {
        var items = await content.GetStorageItemsAsync();
        foreach (var file in items) {
            var copied = await file.CopyAsync(KnownFolders.DocumentsLibrary, file.Name, NameCollisionOption.ReplaceExisting);
            TargetFile.Text += $"{file.Name} 已複製到 文件庫\\文件\\{file.Name}\r";
        }
    }
}

前端 XAML 佈局(四組複製/貼上區塊)

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <!-- 文字區 -->
    <TextBox Name="SourceText" Text="複製源" .../>
    <Button Name="CopyText" Content="複製文字" Click="CopyText_Click" .../>
    <Button Name="PasteText" Content="貼上文字" Click="PasteText_Click" .../>
    <TextBlock Name="TargetText" .../>

    <!-- 圖片區 -->
    <Image Name="SourceImage" Source="Assets/iphone0426_006.jpg" .../>
    <Button Name="CopyImage" Content="複製圖片" Click="CopyImage_Click" .../>
    <Button Name="PasteImage" Content="貼上圖片" Click="PasteImage_Click" .../>
    <Image Name="TargetImage" .../>

    <!-- HTML 區 -->
    <WebView Name="SourceHtml" Source="https://wap.easou.com/" .../>
    <Button Name="CopyHtml" Content="複製 HTML" Click="CopyHtml_Click" .../>
    <Button Name="PasteHtml" Content="貼上 HTML" Click="PasteHtml_Click" .../>
    <WebView Name="TargetHtml" .../>

    <!-- 檔案區 -->
    <Button Name="CopyFile" Content="複製檔案" Click="CopyFile_Click" .../>
    <Button Name="PasteFile" Content="貼上檔案" Click="PasteFile_Click" .../>
    <TextBlock Name="TargetFile" .../>
</Grid>

Manifest 設定(存取文件庫與 .jpg 檔案類型)

參考:文件庫存取權限設定

剪貼簿四格式執行畫面
圖 1:文字、圖片、HTML、檔案複製貼上示範

原始碼下載:Win8ClipBoard2.rar

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved