AvaloniaUI / Avalonia.Samples

Avalonia.Samples aims to provide some minimal samples focusing on a particular issue at a time. This should help getting new users started.
https://www.avaloniaui.net
606 stars 103 forks source link

Clipboard does copy/paste objects when complied with .Net8 #94

Closed cornerbowlsoftware closed 2 months ago

cornerbowlsoftware commented 2 months ago

Describe the bug

Clipboard does copy/paste objects when complied with .Net8

Steps to Reproduce

Create a new Avalonia project. Set the targets to: net7.0 Create a model that includes the [Serializable] attribute. For example:

    [Serializable]
    public class MyTestModel
    {
        public int Id { get; set; }
        public string? Name { get; set; }

        public MyTestModel() 
        { 
        }
    }

Update the MainView to create the model and copy then paste the model to and from the clipboard like so:

public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();
        Loaded += MainView_Loaded;
    }

    private void MainView_Loaded(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        TestAsync().GetAwaiter().GetResult();
    }

    private async Task TestAsync()
    {
        var m = new MyTestModel()
        {
            Id = 1,
            Name = "Test",
        };

        var dataObject = new DataObject();
        dataObject.Set("my-app-person", m);

        var clipboard = TopLevel.GetTopLevel(this)!.Clipboard!;
        await clipboard.SetDataObjectAsync(dataObject);

        var r = await clipboard.GetDataAsync("my-app-person");
        Debug.WriteLine(r);
    }
}

Run the program. Notice the variable "r" is returned as expected. Modify both Avalonia projects to target to net8.0 Run the program. Notice the variable "r" is null.

cornerbowlsoftware commented 2 months ago

Oops wrong project. Please delete me.

timunie commented 2 months ago

Don't do GetAwaiter.GetResult. that's evil imo

cornerbowlsoftware commented 2 months ago

@timunie I know thanks. It's a test app to show off the bug with the least lines of code.