unoplatform / uno

Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
https://platform.uno
Apache License 2.0
9.06k stars 735 forks source link

[Mac Catalyst] UIKit Consistency error thrown when picking file #15504

Closed mcocaroUX closed 9 months ago

mcocaroUX commented 9 months ago

Current behavior

When calling FileOpenPicker.PickSingleFileAsync it throws: image

Expected behavior

FileOpenPicker.PickSingleFileAsync should open file picker correctly

How to reproduce it (as minimally and precisely as possible)

Repro project: MacIssue.zip

  1. Run repro
  2. Click Pick File button

Workaround

I tried using UIKit.UIApplication.SharedApplication.InvokeOnMainThread but I got the same result.

Works on UWP/WinUI

None

Environment

No response

NuGet package version(s)

<PackageVersion Include="CommunityToolkit.WinUI.UI.Controls" Version="7.1.2" />
    <PackageVersion Include="CommunityToolkit.Mvvm" Version="8.2.2" />
    <PackageVersion Include="Geolocation" Version="1.2.1" />
    <PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
    <PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
    <PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.4.231219000" />

    <PackageVersion Include="Uno.Core.Extensions.Logging.Singleton" Version="4.0.1" />
    <PackageVersion Include="Uno.Extensions.Core.WinUI" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Authentication" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Authentication.WinUI" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Configuration" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Hosting" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Hosting.WinUI" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Http" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Http.WinUI" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Http.Refit" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Localization" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Localization.WinUI" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Logging.OSLog" Version="1.7.0" />
    <PackageVersion Include="Uno.Extensions.Logging.WinUI" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Navigation" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Navigation.WinUI" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Navigation.Toolkit.WinUI" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Reactive" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Reactive.WinUI" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Serialization" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Serialization.Http" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Serialization.Refit" Version="4.0.0" />
    <PackageVersion Include="Uno.Extensions.Reactive.Messaging" Version="4.0.0" />

    <PackageVersion Include="Uno.Material.WinUI" Version="4.1.1" />
    <PackageVersion Include="Uno.Toolkit.WinUI" Version="5.1.5" />
    <PackageVersion Include="Uno.Toolkit.WinUI.Material" Version="5.1.5" />
    <PackageVersion Include="Uno.Resizetizer" Version="1.3.0" />
    <PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="$(UnoVersion)" />
    <PackageVersion Include="Uno.UniversalImageLoader" Version="1.9.36" />
    <PackageVersion Include="Uno.WinUI" Version="$(UnoVersion)" />
    <PackageVersion Include="Uno.WinUI.Lottie" Version="$(UnoVersion)"/>
    <PackageVersion Include="Uno.WinUI.DevServer" Version="$(UnoVersion)" />
    <PackageVersion Include="Xamarin.Google.Android.Material" Version="1.10.0.2" />
    <PackageVersion Include="Mapsui.Uno.WinUI" Version="4.1.2" />

Affected platforms

Mac Catalyst

IDE

Visual Studio Code

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

ajpinedam commented 9 months ago

@mcocaroUX Is there any specific reason why you're using the

  var window = (Application.Current as App)?.Window;

  ...

  var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
  WinRT.Interop.InitializeWithWindow.Initialize(fileOpenPicker, hwnd);

What is the result you get when calling the picker with this code?

public async ValueTask PickImage(CancellationToken ct)
{
    var fileOpenPicker = new FileOpenPicker
    {
        SuggestedStartLocation = PickerLocationId.ComputerFolder
    };

    string[] fileTypes = { ".jpg", ".jpeg", ".png", ".gif" };
    fileOpenPicker.FileTypeFilter.AddRange(fileTypes);

    var ret = await fileOpenPicker.PickSingleFileAsync();

    if (ret?.Path != null)
    {
        // Do something with the file
    }
    else
    {
        // User cancelled
    }
}
mcocaroUX commented 9 months ago

@ajpinedam Thanks for your answer. As described on the documentation those lines are needed in order to the code wont fail.

I tested the code you sent and the outcome remains the same

ajpinedam commented 9 months ago

@ajpinedam Thanks for your answer. As described on the documentation those lines are needed in order to the code wont fail.

I tested the code you sent and the outcome remains the same

Thanks for the reply. Yes, I re-ran the sample you provided and was able to reproduce the issue you reported.

We pushed a fix yesterday, which should have already been merged and made available in one of the latest Uno versions.

cc: @mcocaroUX

jeromelaban commented 9 months ago

The backport is now available in 5.1.64.

mcocaroUX commented 9 months ago

@jeromelaban @ajpinedam This fix doesn't seem to be available on 5.1.64, I'm still getting the same issue

jeromelaban commented 9 months ago

@mcocaroUX make sure to validate that the output binaries contain the right uno versions. You can check that by looking at the assembly versions.

mcocaroUX commented 9 months ago

@jeromelaban Thanks. I always clean my projects when having this kind of issues. Anyways I checked the binaries and they seem to have the correct version

jeromelaban commented 9 months ago

@mcocaroUX Can you provide a new stack trace of the issue?

mcocaroUX commented 9 months ago

@jeromelaban We solved it, it was some missing entitlement on our side. Thanks for your help!

jeromelaban commented 9 months ago

Thanks. Can you tell which entitlement you were missing? We may be able to enhance our docs with it.

mcocaroUX commented 9 months ago

Sure! com.apple.security.files.user-selected.read-write