5 changed files with 106 additions and 4 deletions
@ -0,0 +1,23 @@ |
|||||
|
<UserControl x:Class="ControlCatalog.Pages.ClipboardPage" |
||||
|
xmlns="https://github.com/avaloniaui" |
||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
||||
|
<StackPanel Orientation="Vertical" Spacing="4"> |
||||
|
<TextBlock Classes="h2">Example of ClipboardPage capabilities</TextBlock> |
||||
|
|
||||
|
<Button Click="CopyText" Content="Copy text to clipboard" /> |
||||
|
<Button Click="PasteText" Content="Paste text from clipboard" /> |
||||
|
<Button Click="CopyTextDataObject" Content="Copy text to clipboard (data object)" /> |
||||
|
<Button Click="PasteTextDataObject" Content="Paste text from clipboard (data object)" /> |
||||
|
|
||||
|
<Button Click="CopyFilesDataObject" Content="Copy files to clipboard (data object)" /> |
||||
|
<Button Click="PasteFilesDataObject" Content="Paste files from clipboard (data object)" /> |
||||
|
|
||||
|
<Button Click="GetFormats" Content="Get clipboard formats" /> |
||||
|
<Button Click="Clear" Content="Clear clipboard" /> |
||||
|
|
||||
|
<TextBox x:Name="ClipboardContent" |
||||
|
MinHeight="100" |
||||
|
AcceptsReturn="True" |
||||
|
Watermark="Text to copy of file names per line" /> |
||||
|
</StackPanel> |
||||
|
</UserControl> |
||||
@ -0,0 +1,72 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
using Avalonia; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Input; |
||||
|
using Avalonia.Interactivity; |
||||
|
using Avalonia.Markup.Xaml; |
||||
|
|
||||
|
namespace ControlCatalog.Pages |
||||
|
{ |
||||
|
public partial class ClipboardPage : UserControl |
||||
|
{ |
||||
|
public ClipboardPage() |
||||
|
{ |
||||
|
InitializeComponent(); |
||||
|
} |
||||
|
|
||||
|
private TextBox ClipboardContent => this.Get<TextBox>("ClipboardContent"); |
||||
|
|
||||
|
private void InitializeComponent() |
||||
|
{ |
||||
|
AvaloniaXamlLoader.Load(this); |
||||
|
} |
||||
|
|
||||
|
private async void CopyText(object sender, RoutedEventArgs args) |
||||
|
{ |
||||
|
await Application.Current.Clipboard.SetTextAsync(ClipboardContent.Text); |
||||
|
} |
||||
|
|
||||
|
private async void PasteText(object sender, RoutedEventArgs args) |
||||
|
{ |
||||
|
ClipboardContent.Text = await Application.Current.Clipboard.GetTextAsync(); |
||||
|
} |
||||
|
|
||||
|
private async void CopyTextDataObject(object sender, RoutedEventArgs args) |
||||
|
{ |
||||
|
var dataObject = new DataObject(); |
||||
|
dataObject.Set(DataFormats.Text, ClipboardContent.Text); |
||||
|
await Application.Current.Clipboard.SetDataObjectAsync(dataObject); |
||||
|
} |
||||
|
|
||||
|
private async void PasteTextDataObject(object sender, RoutedEventArgs args) |
||||
|
{ |
||||
|
ClipboardContent.Text = (string)await Application.Current.Clipboard.GetDataAsync(DataFormats.Text); |
||||
|
} |
||||
|
|
||||
|
private async void CopyFilesDataObject(object sender, RoutedEventArgs args) |
||||
|
{ |
||||
|
var dataObject = new DataObject(); |
||||
|
dataObject.Set(DataFormats.FileNames, ClipboardContent.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)); |
||||
|
await Application.Current.Clipboard.SetDataObjectAsync(dataObject); |
||||
|
} |
||||
|
|
||||
|
private async void PasteFilesDataObject(object sender, RoutedEventArgs args) |
||||
|
{ |
||||
|
var fiels = (IEnumerable<string>)await Application.Current.Clipboard.GetDataAsync(DataFormats.FileNames); |
||||
|
ClipboardContent.Text = string.Join(Environment.NewLine, fiels); |
||||
|
} |
||||
|
|
||||
|
private async void GetFormats(object sender, RoutedEventArgs args) |
||||
|
{ |
||||
|
var formats = await Application.Current.Clipboard.GetFormatsAsync(); |
||||
|
ClipboardContent.Text = string.Join(Environment.NewLine, formats); |
||||
|
} |
||||
|
|
||||
|
private async void Clear(object sender, RoutedEventArgs args) |
||||
|
{ |
||||
|
await Application.Current.Clipboard.ClearAsync(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue