committed by
GitHub
30 changed files with 620 additions and 315 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,77 @@ |
|||
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 ?? string.Empty); |
|||
await Application.Current.Clipboard.SetDataObjectAsync(dataObject); |
|||
} |
|||
|
|||
private async void PasteTextDataObject(object sender, RoutedEventArgs args) |
|||
{ |
|||
ClipboardContent.Text = await Application.Current.Clipboard.GetDataAsync(DataFormats.Text) as string ?? string.Empty; |
|||
} |
|||
|
|||
private async void CopyFilesDataObject(object sender, RoutedEventArgs args) |
|||
{ |
|||
var files = ClipboardContent.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); |
|||
if (files.Length == 0) |
|||
{ |
|||
return; |
|||
} |
|||
var dataObject = new DataObject(); |
|||
dataObject.Set(DataFormats.FileNames, files); |
|||
await Application.Current.Clipboard.SetDataObjectAsync(dataObject); |
|||
} |
|||
|
|||
private async void PasteFilesDataObject(object sender, RoutedEventArgs args) |
|||
{ |
|||
var fiels = await Application.Current.Clipboard.GetDataAsync(DataFormats.FileNames) as IEnumerable<string>; |
|||
ClipboardContent.Text = fiels != null ? string.Join(Environment.NewLine, fiels) : string.Empty; |
|||
} |
|||
|
|||
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(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
namespace Avalonia.MicroCom |
|||
{ |
|||
public abstract class CallbackBase : IUnknown, IMicroComShadowContainer |
|||
{ |
|||
private readonly object _lock = new object(); |
|||
private bool _referencedFromManaged = true; |
|||
private bool _referencedFromNative = false; |
|||
private bool _destroyed; |
|||
|
|||
public bool IsDestroyed => _destroyed; |
|||
|
|||
protected virtual void Destroyed() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
lock (_lock) |
|||
{ |
|||
_referencedFromManaged = false; |
|||
DestroyIfNeeded(); |
|||
} |
|||
} |
|||
|
|||
void DestroyIfNeeded() |
|||
{ |
|||
if(_destroyed) |
|||
return; |
|||
if (_referencedFromManaged == false && _referencedFromNative == false) |
|||
{ |
|||
_destroyed = true; |
|||
Destroyed(); |
|||
} |
|||
} |
|||
|
|||
public MicroComShadow Shadow { get; set; } |
|||
public void OnReferencedFromNative() |
|||
{ |
|||
lock (_lock) |
|||
_referencedFromNative = true; |
|||
} |
|||
|
|||
public void OnUnreferencedFromNative() |
|||
{ |
|||
lock (_lock) |
|||
{ |
|||
_referencedFromNative = false; |
|||
DestroyIfNeeded(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue