Cannot build Xaml in Platforms/Windows folder

典 程 1 Reputation point
2022-10-01T14:03:15.67+00:00

In my MAUI project, I want to design xaml for different platforms seperately, but xaml file in Platform/Windows folder cannot compile.

Steps to reproduce:

  1. Create a MAUI App named Reproduce001.BuildXamlForWindowsOnly
  2. Disable platforms except Android and Windows.
  3. Modify MainPage.xaml: <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Reproduce001.BuildXamlForWindowsOnly"
    x:Class="Reproduce001.BuildXamlForWindowsOnly.MainPage">
     <local:MyCollectionView/>  
    
    </ContentPage>
  4. Modify MainPage.xaml.cs: namespace Reproduce001.BuildXamlForWindowsOnly; public partial class MainPage : ContentPage
    {
    public MainPage()
    {
    InitializeComponent();
    }
    }
  5. Create Platforms/Android/MyCollectionView.xaml and Platforms/Windows/MyCollectionView.xaml with the same content: <?xml version="1.0" encoding="utf-8" ?>
    <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Reproduce001.BuildXamlForWindowsOnly.MyCollectionView">
    <CollectionView ItemsSource="{Binding }">
    <CollectionView.ItemTemplate>
    <DataTemplate>
    <Grid ColumnDefinitions=",">
    <Label Grid.Column="0" Text="Content"/>
    <Entry Grid.Column="1" Text="{Binding }"/>
    </Grid>
    </DataTemplate>
    </CollectionView.ItemTemplate>
    <CollectionView.BindingContext>
    <x:Array Type="{x:Type x:String}">
    <x:String>Content1</x:String>
    <x:String>Content2</x:String>
    <x:String>Content3</x:String>
    </x:Array>
    </CollectionView.BindingContext>
    </CollectionView>
    </ContentView>
  6. Modify the namespace of Platforms/Android/MyCollectionView.xaml.cs and Platforms/Windows/MyCollectionView.xaml.cs to Reproduce001.BuildXamlForWindowsOnly
  7. Build project for Windows Machine and see error: 1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(2,2): XamlCompiler error WMC0001: Unknown type 'ContentView' in XML namespace 'http://schemas.microsoft.com/dotnet/2021/maui'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(5,4): XamlCompiler error WMC0001: Unknown type 'CollectionView' in XML namespace 'http://schemas.microsoft.com/dotnet/2021/maui'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(5,19): XamlCompiler error WMC0001: Unknown type 'Binding' in XML namespace 'http://schemas.microsoft.com/dotnet/2021/maui'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(7,8): XamlCompiler error WMC0001: Unknown type 'DataTemplate' in XML namespace 'http://schemas.microsoft.com/dotnet/2021/maui'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(8,10): XamlCompiler error WMC0001: Unknown type 'Grid' in XML namespace 'http://schemas.microsoft.com/dotnet/2021/maui'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(9,12): XamlCompiler error WMC0001: Unknown type 'Label' in XML namespace 'http://schemas.microsoft.com/dotnet/2021/maui'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(10,12): XamlCompiler error WMC0001: Unknown type 'Entry' in XML namespace 'http://schemas.microsoft.com/dotnet/2021/maui'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(10,34): XamlCompiler error WMC0001: Unknown type 'Binding' in XML namespace 'http://schemas.microsoft.com/dotnet/2021/maui'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(15,8): XamlCompiler error WMC0001: Unknown type 'Array' in XML namespace 'http://schemas.microsoft.com/winfx/2009/xaml'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(15,16): XamlCompiler error WMC0001: Unknown type 'Type' in XML namespace 'http://schemas.microsoft.com/winfx/2009/xaml'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(16,10): XamlCompiler error WMC0001: Unknown type 'String' in XML namespace 'http://schemas.microsoft.com/winfx/2009/xaml'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(17,10): XamlCompiler error WMC0001: Unknown type 'String' in XML namespace 'http://schemas.microsoft.com/winfx/2009/xaml'
    1>E:\VsProjects\MauiTestApps\test\Reproduce001.BuildXamlForWindowsOnly\Platforms\Windows\MyCollectionView.xaml(18,10): XamlCompiler error WMC0001: Unknown type 'String' in XML namespace 'http://schemas.microsoft.com/winfx/2009/xaml'
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 29,381 Reputation points Microsoft Vendor
    2022-10-03T08:12:44.603+00:00

    Hello @典 程 ,

    You cannot add a ContentView for Windows or Android under Platforms folder. It's a MAUI control, you should add it outside the Platforms folder, and you can add platform-specific code (Android/Windows native features)into Platforms folder. For more details, you can refer to .NET MAUI invoking platform code - .NET MAUI | Microsoft Learn

    And I checked the codes you provided, the main purpose is to add different MyCollectionView for Windows and Android, you can get the device platform, then add the logical code according to the DevicePlatform type.
    For example, you can add the logical codes in MyCollectionView.cs

    if (DeviceInfo.Current.Platform == DevicePlatform.Android)  
       {  
                      //Android  
        }  
        else if (DeviceInfo.Current.Platform == DevicePlatform.WinUI) {  
                      //Windows  
        }  
        else { ...}  
    

    Or use conditional compilation:

       #if ANDROID  
                    //Android  
        #endif  
        #if WINDOWS  
                           //Windows  
        #endif  
    

    Or in XAML

    <ContentPage ...  
                 xmlns:local="clr-namespace:Reproduce001.BuildXamlForWindowsOnly">     
        <OnPlatform x:TypeArguments="ContentView">  
            <On Platform="Android">  
                <local:MyCollectionView BackgroundColor="Yellow"></local:MyCollectionView>  
            </On>  
            <On Platform="WinUI">  
                <local:MyCollectionView BackgroundColor="Red"></local:MyCollectionView>// or other contentview  
            </On>  
        </OnPlatform>  
    </ContentPage>  
    

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments