mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.4k stars 536 forks source link

[Maui] SkiaSharp OnPaintSurface not called on iOS device connected to Windows dev computer #2001

Open SailDev opened 2 years ago

SailDev commented 2 years ago

Description

InvalidateSurface does not trigger OnPaintSurface, when the app is deployed to an iOS device, directly connected to the Windows dev computer.

The problem does not occur, when the app is deployed to a remotely connected device via Mac build-host.

Maybe this info saves some time for someone.

Reference: Maui Issue https://github.com/dotnet/maui/issues/6120

mattleibow commented 2 years ago

What version of Visual Studio are you using?

SailDev commented 2 years ago

Microsoft Visual Studio Community 2022 (64-bit) - Preview Version 17.2.0 Preview 3.0

naweed commented 2 years ago

I have the same issue. I am using VS for Mac 17.3 Preview. It works fine for Android and it correctly fires OnPaintSurface on InvalidateSurface, but on iOS, it is not calling OnPaintSurface.

naweed commented 2 years ago

Strangely, it works from my M1 Mac. I have the issue with Intel Mac. May be this is an Intel x86 architecture thing.

egvijayanand commented 2 years ago

It's not getting invoked on any of the platforms (tried iOS, Android, and Windows) with both the recent stable (2.88.0) and preview (2.88.1-preview.63) release versions.

Tried with both XAML and C# definitions, there is no change in the result.

Working with .NET MAUI GA and VS2022 for Windows ver. 17.3.0 Preview 1.1

ibocon commented 1 year ago

Operating System

Windows 11 Pro 21H2 image

Visual Stuido

Microsoft Visual Studio Enterprise 2022 (64-bit) Version 17.4.1 image

CS Project

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>net7.0-ios</TargetFrameworks>
        <PackageReference Include="SkiaSharp.Views.Maui.Controls" Version="2.88.3" />
    </ItemGroup>
</Project>

Windows

windows

Android

android

iOS

Windows

ios

macOS

macOS - iOS (1)

Mac Catalyst

macOS - mac catalyst (1)

Source Code

public sealed class PreviewView
    : SKCanvasView
{
    protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
    {
        base.OnPaintSurface(e);

        using SKCanvas skCanvas = e.Surface.Canvas;
        skCanvas.Clear();
        skCanvas.DrawColor(SKColors.Wheat);

        SKBitmap bitmap = _canvas.Paint(_paper);
        skCanvas.DrawBitmap(bitmap, new SKPoint(0, 0));
    }
}
yoshiask commented 1 year ago

I am also seeing this issue with 2.88.3, built with .NET MAUI 7 and Rider 2022.3.1. I can only test on Windows and Android, but OnPaintSurface is never called on either platform.

yoshiask commented 1 year ago

It looks like this issue may be caused by placing the SKCanvasView inside a StackLayout. Placing it directly inside a ScrollView, while of course doesn't scroll, does fire the PaintSurface event and renders.

Niv2023 commented 1 year ago

Had same issue now (VS 2022 17.6.3 and SkiaSharp 2.88.3) Spent quite some time to get it figured out. Herewith my finds

Niv2023 commented 1 year ago

I have placed under a ContentView....

JoacimWall commented 12 months ago

Any solution for this have the same problem on 8 rc1 and Mac m1 and on release off app to App Store. Same code work onAndriod

//Joacim

sps014 commented 10 months ago

Same issue in MAUI .NET 8 rc 2 on Windows and Android also with StackLayout.

VNGames commented 9 months ago

For me on Android it doesn't work when it is inside a grid and I set HorizontalOptions="center". Without it it works. If I try to wrap it into contentview or another grid to center it by centering a wrapper it also doesn't work. I use MeasureOverride to calculate size. It may affect. Overall it seems very unstable. I don't know whether the problem is in MAUI or SkiaSharp but this is a critical bug.

VNGames commented 9 months ago

I solved all my problems on Android where OnPaintSurface wasn't called. Haven't tried iOS yet. I use MeasureOverride to calculate the size of the control based on some properties and available height. OnPaintSurface isn't called when HorizontalOptions="Center". Also it isn't called/or it is but calculation is wrong (I forgot) if you want for example to have a HeightRequest="200" on the control but Width being calculated based on Height inside MeasureOverride. MAUI will just call MeasureOverride with infinite constraint for both width and height instead of (Double.Infinite, 200) To solve this I have to wrap control into ContentView and set HeightRequest="200" for ContentView. If I want to center the control I also should center this ContentView instead of control itself.

It is like returning back 8 years when Xamarin was a chaos and such kind of hacks were needed for everything.

vladtrabl commented 4 months ago

I had the same problem. I called InvalidateSurface intermittently with System.Timer and on IOS it doesn't call OnPaintSurface. MainThread.BeginInvokeOnMainThread(InvalidateSurface) solves this problem.

5CoJL commented 1 month ago

I'm facing the same issue, solely for iOS as well, and none of the workarounds here work. I am building with Pair to Mac using the Remote Device. I have tried MainThread.BeginInvokeOnMainThread(() => mySkCanvasView.InvalidateSurface()); to no avail.

Android works perfectly fine.

Edit: Removing VerticalOptions and HorizontalOptions in the XAML of my SKCanvasView worked, my bad.