The type 'RadioButton' does not support direct content

capsicum 101 Reputation points
2021-02-20T15:03:12.19+00:00

I am using the new RadioButton in Xam.Forms 5
The Android implementation doesn't support views, so I must have a different layout according to the platform.

Something like this, for example:

<RadioButton Value="{x:Static local:UdpTransmitMode.UdpUnicast}" >  
   <OnPlatform x:TypeArguments="Content">  
      <On Platform="iOS" Value="Apple" />  
      <On Platform="Android, UWP" Value="Google" />  
   </OnPlatform>			  
</RadioButton>  

But I get the error The type 'RadioButton' does not support direct content

But it does, I can

<RadioButton Content="GOOGLE"  
    Value="{x:Static local:SettingsMode.GOOG}" />  

Without any issues.

I am following the examples on the page: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/radiobutton#respond-to-radiobutton-state-changes

..what am I doing wrong?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,316 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-02-22T06:55:57.11+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The error shows that we should use OnPlatform inside the RadioButton.Content tag , and specify the actual type as x:TypeArguments.

    Please refer to official sample code .

    ---

    First I tried with type x:String and x:Object get the following result

       <RadioButton >  
               <RadioButton.Content>  
                   <OnPlatform x:TypeArguments="x:String">  
                       <On Platform="iOS" Value="Apple" />  
                       <On Platform="Android, UWP" Value="Google" />  
                   </OnPlatform>  
               </RadioButton.Content>  
           </RadioButton>  
    

    70477-capture.png

       <RadioButton >  
               <RadioButton.Content>  
                   <OnPlatform x:TypeArguments="x:Object">  
                       <On Platform="iOS" Value="Apple" />  
                       <On Platform="Android, UWP" Value="Google" />  
                   </OnPlatform>  
               </RadioButton.Content>  
           </RadioButton>  
    

    70478-cap2ture.png

    ---

    Then I following the way provided by docs but still no luck

       xmlns:sys="clr-namespace:System;assembly=mscorlib"  
         
       <RadioButton >  
               <RadioButton.Content>  
                   <OnPlatform x:TypeArguments="sys:String">    //sys:Object does not work either  
                       <On Platform="iOS" Value="Apple" />  
                       <On Platform="Android, UWP" Value="Google" />  
                   </OnPlatform>  
               </RadioButton.Content>  
           </RadioButton>  
    

    70502-capture.png

    ---

    Xamarin.Forms 5.0 defines the type of RadioButton.Content as object not string , maybe it is the cause.

    I'm afraid we have to use Binding .

       public string myContent {  
                   get  
                   {  
                       if (Device.RuntimePlatform == Device.iOS)  
                           return "Apple";  
                       else if (Device.RuntimePlatform == Device.UWP | Device.RuntimePlatform == Device.Android)  
                           return "Google";  
                       return "";  
                   }  
               }  
         
         
       <RadioButton Content="{Binding myContent}" />  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    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