xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

XamlCompilation seems to break targetPlatform-specific namespaces #9971

Open chucker opened 4 years ago

chucker commented 4 years ago

Steps to Reproduce

  1. create a Xamarin Forms app with an iOS app
  2. add a Mac app
  3. in the MyApp.macOS project, add a XAML view:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="MyApp.macOS.Views.MyView">

    <ContentView.Content>
        <StackLayout Orientation="Vertical">
            <Label>macOS MyView</Label>
        </StackLayout>
    </ContentView.Content>

</ContentView>
  1. in the MyApp project, make a XAML page that references the view using a targetPlatform:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:macos="clr-namespace:MyApp.macOS.Views;assembly=MyApp.macOS;targetPlatform=macOS"
    x:Class="MyApp.Views.MyPage">

    <ContentPage.Content>
        <ContentView x:Name="ContentView">
            <OnPlatform x:TypeArguments="View">
                <On Platform="macOS" x:Name="OnMacOS">
                    <macos:MyView />
                </On>
            </OnPlatform>
        </ContentView>
    </ContentPage.Content>

</ContentPage>
  1. make this the main page:
public App()
{
    InitializeComponent();

    MainPage = new Views.MyPage();
}
  1. run the macOS app

Expected Behavior

The page should show a label that reads "macOS MyView".

Actual Behavior

The page is empty. The view seems to fail to resolve, and get silently removed.

However, with [assembly: XamlCompilation(XamlCompilationOptions.Compile)] removed, I cannot reproduce this.

Environment

=== Visual Studio Community 2019 for Mac === Version 8.4.8 (build 2) Installation UUID: 9a7856af-5a33-46d4-af11-dc30d3e1c6f4 GTK+ 2.24.23 (Raleigh theme) Xamarin.Mac 5.16.1.25 (issue-7441-d16-3-vsmac / 881172e73) Package version: 606000166 === Mono Framework MDK === Runtime: Mono 6.6.0.166 (2019-08/d9001b5ae70) (64-bit) Package version: 606000166 === Roslyn (Language Service) === 3.4.0-beta4-19562-05+ff930dec4565e2bc424ad3bf3e22ecb20542c87d === NuGet === Version: 5.3.0.6192 === .NET Core SDK === SDK: /usr/local/share/dotnet/sdk/3.1.102/Sdks SDK Version: 3.1.102 MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.6.0/lib/mono/msbuild/Current/bin/Sdks === .NET Core Runtime === Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 3.1.2 2.1.15 === Xamarin.Profiler === Version: 1.6.13.11 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler === Updater === Version: 11 === Apple Developer Tools === Xcode 11.3.1 (15715) Build 11C504 === Xamarin.Mac === Version: 6.10.0.21 (Visual Studio Community) Hash: 02c4b3bdc Branch: xcode11.3 Build date: 2020-02-18 14:13:31-0500 === Xamarin.Android === Not Installed === Microsoft Mobile OpenJDK === Java SDK: Not Found Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL === Android SDK Manager === Version: 16.4.0.10 Hash: 2c49a7d Branch: remotes/origin/d16-4 Build date: 2020-02-20 19:25:52 UTC === Android Device Manager === Version: 16.4.0.32 Hash: 7a5cb8b Branch: remotes/origin/d16-4 Build date: 2020-02-20 19:26:14 UTC === Xamarin Designer === Version: 16.4.0.479 Hash: 074544417 Branch: remotes/origin/d16-4 Build date: 2020-01-22 22:50:22 UTC === Xamarin.iOS === Version: 13.10.0.21 (Visual Studio Community) Hash: 02c4b3bdc Branch: xcode11.3 Build date: 2020-02-18 14:13:32-0500 === Xamarin Inspector === Version: 1.4.3 Hash: db27525 Branch: 1.4-release Build date: Mon, 09 Jul 2018 21:20:18 GMT Client compatibility: 1 === Build Information === Release ID: 804080002 Git revision: 4f35aa7e44fb398379e512d0bfd6f8df8d34b5ac Build date: 2020-02-27 16:16:52+00 Build branch: release-8.4 Xamarin extensions: 4f35aa7e44fb398379e512d0bfd6f8df8d34b5ac === Operating System === Mac OS X 10.15.3 Darwin 19.3.0 Darwin Kernel Version 19.3.0 Thu Jan 9 20:58:23 PST 2020 root:xnu-6153.81.5~1/RELEASE_X86_64 x86_64

Build Logs

https://gist.github.com/chucker/d4b18c62a2b93f219acb7ea5f189ab49

Further remarks

async void ContentPage_Appearing(System.Object sender, System.EventArgs e)
{
    await Task.Delay(TimeSpan.FromMilliseconds(0));

    var type = global::System.Reflection.Assembly.GetEntryAssembly().GetType("MyApp.macOS.Views.MyView");

    ContentView.Content = Activator.CreateInstance(type) as View;
}
StephaneDelcroix commented 4 years ago

this is a known an documented drawback. you can use platform specific xaml in a netstandard (or other) shared project because it will be resolved at runtime, but you can't compile it as it doesn't have a dependency on the platform project. Multi-targetting should probably help there, but I haven't tested it yet

chucker commented 4 years ago

this is a known an documented drawback. you can use platform specific xaml in a netstandard (or other) shared project because it will be resolved at runtime, but you can't compile it as it doesn't have a dependency on the platform project.

I could've sworn I didn't see it before, but the warning is in fact right there on the page I read. Sorry! :)

Still, wouldn't it be possible to extend this to a compiler warning if any imported namespace contains targetPlatform and XamlCompilation is enabled?

jfversluis commented 4 years ago

While it is a documented limitation, it should still trigger a compilation error when XAML compilation is enabled. That is the actual bug here. Thanks for reporting @chucker !