ImageFromXamarinUI icon indicating copy to clipboard operation
ImageFromXamarinUI copied to clipboard

Doesn't work with .Net Maui

Open trevortirrell opened this issue 3 years ago • 2 comments

I tried using this with .Net Maui, but was unable to build my app after adding this package. Any plans to update for .Net Maui? Or any quick fix I can use? Thanks.

trevortirrell avatar Mar 08 '22 20:03 trevortirrell

@trevortirrell, You can integrate the code manually inside your project. No need to add Nuget package. Just check the project structure and add code accordingly. I haven't built Maui apps, but manually adding the code would be better and would facilitate you with more customization. Just Navigate to folder ImageFromXamarinUI. Copy the code from VisualElementExtension.android.cs to your Platform specific code. Same thing can be done for iOS. Now in your shared project copy the code from VisualElementExtension.shared.cs

Now you can access platform specific implementation using Dependency Service in Xamarin.Forms, or check how it can be done in .Net Maui.

This can be done to almost all packages that do not have Maui support. I prefer adding code manually from Github. This way we get more control over the code and we can customize it as per our requirement. We can also avoid Obsolete code as we can change parts of the code that is deprecated.

junedmmn avatar Sep 23 '22 01:09 junedmmn

In maui you can use the feature out of the box by using the CaptureAsync Method, for more convenience i have created the extension method below in my projects:

` public static class VisualElementExtensions {

public static async ValueTask<byte[]> CaptureScreenshotAsync(this VisualElement view, ScreenshotFormat format, int quality = 100)
{
    if (view == null)
        return default;

    await Task.Yield();

    return await CaptureScreenshotInternalAsync(view, format, quality);
}

public static async Task<byte[]> CaptureScreenshotAsync(this VisualElement view)
{
    if (view == null)
        return default;

    await Task.Yield();

    return await CaptureScreenshotInternalAsync(view, ScreenshotFormat.Png, 80);
}

private static async Task<byte[]> CaptureScreenshotInternalAsync(VisualElement view, ScreenshotFormat format, int quality)
{
    byte[] screenshot = default;

    var screenshotResult = await view.CaptureAsync();
    if (screenshotResult != null)
    {
        using MemoryStream memoryStream = new();
        await screenshotResult.CopyToAsync(memoryStream, format, quality);
        memoryStream.Position = 0;
        screenshot = memoryStream.ToByteArray();
    }
    return screenshot;
}

} `

0xc3u avatar Aug 04 '23 12:08 0xc3u