Created
July 23, 2016 00:06
-
-
Save sfdesigner/598f98454e8aac84c7600e21dd553f84 to your computer and use it in GitHub Desktop.
Copy and Paste from Clipboard in C# for Universal Windows App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Page | |
x:Class="ClipboardSandbox.MainPage" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="using:ClipboardSandbox" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
mc:Ignorable="d"> | |
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="20"> | |
<StackPanel Orientation="Vertical" Padding="20"> | |
<TextBlock Text="Text to copy to clipboard" Margin="0 10 0 0"/> | |
<TextBox x:Name="TextToCopy" Margin="0 10 0 0"></TextBox> | |
<Button x:Name="CopyText" Content="Copy Text" Click="CopyText_Click" Margin="0 10 0 0"/> | |
<TextBlock Text="Text to paste from clipboard" Margin="0 10 0 0"/> | |
<TextBox x:Name="TextToPaste" Margin="0 10 0 0"></TextBox> | |
<Button x:Name="PasteText" Content="Paste Text" Click="PasteText_Click" Margin="0 10 0 0"/> | |
<TextBlock x:Name="StatusText" Text="Ready!" Margin="0 50 0 0" /> | |
</StackPanel> | |
</Grid> | |
</Page> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices.WindowsRuntime; | |
using Windows.Foundation; | |
using Windows.Foundation.Collections; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
using Windows.UI.Xaml.Controls.Primitives; | |
using Windows.UI.Xaml.Data; | |
using Windows.UI.Xaml.Input; | |
using Windows.UI.Xaml.Media; | |
using Windows.UI.Xaml.Navigation; | |
using Windows.ApplicationModel.DataTransfer; | |
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 | |
namespace ClipboardSandbox | |
{ | |
/// <summary> | |
/// An empty page that can be used on its own or navigated to within a Frame. | |
/// </summary> | |
public sealed partial class MainPage : Page | |
{ | |
public MainPage() | |
{ | |
this.InitializeComponent(); | |
} | |
private void CopyText_Click(object sender, RoutedEventArgs e) | |
{ | |
DataPackage CopyData = new DataPackage(); | |
CopyData.SetText(TextToCopy.Text); | |
Clipboard.SetContent(CopyData); | |
StatusText.Text = "Text copied to clipboard"; | |
} | |
private void PasteText_Click(object sender, RoutedEventArgs e) | |
{ | |
DataPackageView PasteData; | |
PasteData = Clipboard.GetContent(); | |
if (PasteData.Contains(StandardDataFormats.Text)) | |
{ | |
IAsyncOperation<string> ClipboardAsync = PasteData.GetTextAsync(); | |
string StringData = ClipboardAsync.GetResults(); | |
TextToPaste.Text = StringData; | |
StatusText.Text = "Text pasted from clipboard"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment