Search code examples
xamarin.formscsprojmultitargeting

Xamarin.Forms SDK-Style multiplatform project with XAML


Have defined multitarget project:

<Project Sdk="MSBuild.Sdk.Extras/2.0.54">

    <PropertyGroup>
        <TargetFrameworks>netstandard2.0;xamarinios10;monoandroid9.0;</TargetFrameworks>
        <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
        <EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Xamarin.Forms" Version="4.6.0.800" />
        <PackageReference Include="Xamarin.Essentials" Version="1.5.3" />
        <Compile Include="**\Shared\*.cs" />
    </ItemGroup>

    <ItemGroup Condition=" $(TargetFramework.StartsWith('xamarinios')) ">
        <Compile Include="**\iOS\*.cs" />
    </ItemGroup>

    <ItemGroup Condition=" $(TargetFramework.StartsWith('monoandroid')) ">
        <Compile Include="**\Android\*.cs" />
    </ItemGroup>

    <ItemGroup>
        <EmbeddedResource Include="Shared\MyView.xaml">
          <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
        </EmbeddedResource>
    </ItemGroup>

</Project>

So, when i make my custom component only in code all compile and works well, but i would like to develop some custom controls using XAML. But i got error of ambiguous call to method InitializeComponent() in MyView object. How to make correct conditional configuration in .csproj for compiler to understand *.xaml and *.xaml.cs files ?


Solution

  • Adding these properties to common ItemGroup solved the problem:

        <None Remove="**\Shared\*.xaml" />
        <Compile Include="**\Shared\*.xaml.cs" DependentUpon="%(Filename)" />
        <EmbeddedResource Include="**\Shared\*.xaml" Generator="MSBuild:UpdateDesignTimeXaml" />