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

[Bug] [UWP] Project reference in UWP app to multi-target library yields APPX0702 error using XF 4.3+ (fine with 4.2) #8459

Closed CaseAlexander closed 4 years ago

CaseAlexander commented 4 years ago

Description

Using XF 4.3 and above (including 4.4.0.936621-pre1), referencing a multi-target library from UWP gives error APPX0702 because it is looking for the .xr.xml and Compact.xbf files in the wrong location. Using XF 4.2 works perfectly.

Steps to Reproduce

  1. Create a new blank Xamarin.Forms mobile project targeting UWP only
  2. Add a class library
  3. Manually edit the csproj so it uses MSBuild.Sdk.Extras 2.0.54 and targets netstandard2.0 and uap10.0.17763
  4. Add a Platform/UWP folder and create a new class
  5. Add a reference to the class library to the Xamarin.Forms project and the UWP project
  6. Upgrade XF to 4.3+ in all projects
  7. Try to build UWP app

(sample project included)

.csproj of class library

<Project Sdk="MSBuild.Sdk.Extras/2.0.54">
  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;uap10.0.17763</TargetFrameworks>
    ...
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Xamarin.Forms" Version="4.3.0.947036" /> <!-- does not work -->
    <!-->PackageReference Include="Xamarin.Forms" Version="4.2.0.910310" /--><!-- works just fine -->
    <Compile Remove="Platform\**\*.*" />
    <EmbeddedResource Remove="Platform\**\*.*" />
    <None Include="Platform\**\*.*" />
    <Compile Include="**\*.cs" Exclude="Platform\**\*.cs;obj\**\*.*;bin\**\*.*;bin;obj" />
  </ItemGroup>
  <ItemGroup Condition=" $(TargetFramework.StartsWith('uap10.0')) ">    
    <Compile Include="Platform\UWP\**\*.cs" />
    <Page Include="Platform\UWP\**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" />
    <None Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
    <Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
  </ItemGroup>
</Project>

Expected Behavior

app should compile using XF 4.3+ as it does using XF 4.2-

Actual Behavior

APPX0702 error becuase looking in wrong folder for .xr.xml and Compact.xbf files

error APPX0702: Payload file '\bin\Debug\uap10.0.17763\ClassLibrary1\ClassLibrary1.xr.xml' does not exist. error APPX0702: Payload file '\bin\Debug\uap10.0.17763\ClassLibrary1\Microsoft.UI.Xaml\DensityStyles\Compact.xbf' does not exist.

actual files are in '\bin\Debug\uap10.0.17763\ClassLibrary1.xr.xml' '\bin\Debug\uap10.0.17763\Microsoft.UI.Xaml\DensityStyles\Compact.xbf'

Basic Information

Reproduction Link

DemoApp.zip

YZahringer commented 4 years ago

Same error. Temp workaround on multi-target csproj:

<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition=" $(TargetFramework.StartsWith('uap10.0')) ">
    <Exec Command="XCOPY $(TargetDir)Microsoft.UI.Xaml $(TargetDir)$(ProjectName)\Microsoft.UI.Xaml\ /E /Y" />
    <Exec Command="COPY $(TargetDir) $(TargetDir)$(ProjectName)" />
  </Target>
samhouts commented 4 years ago

Interesting. I tested the sample project on 16.4, and it did not crash for me.

PureWeen commented 4 years ago

@CaseAlexander @YZahringer I don't think this is a Forms Bug

I feel like this is a bug with https://github.com/onovotny/MSBuildSdkExtras

If I setup a project that isn't multitargeted then it builds fine without any errors. It's able to find the xr.xaml files just fine

PureWeen commented 4 years ago

@CaseAlexander @YZahringer

Can you try adding

<GenerateLibraryLayout>true</GenerateLibraryLayout>

To your multi targeted project and let me know if that fixes?

PureWeen commented 4 years ago

For me the following fixed the build exception on VS 16.3

<GenerateLibraryLayout>true</GenerateLibraryLayout>

We also tested the included project on VS 16.4 without adding GenerateLibraryLayout and it's able to compile without any issues. To me this looks like a bug in the VS targets around UWP when it gathers together everything it's going to use for the APPX package.

Closing for now as this doesn't look like an issue with Forms

YZahringer commented 4 years ago

@PureWeen I confirm, GenerateLibraryLayout fix the build, thank you!