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.