Invalid cast when tryng to set Grid dimensions data in XAML

Jesse Knott 666 Reputation points
2023-01-03T04:14:36.327+00:00

Hello, perhaps there is a better way to do this I am not thinking of?
In my XAML I am trying to set conditional formatting for a Gid.

            <Image  
                Grid.Row="{StaticResource Key=imageRow}"  
                Grid.RowSpan="{StaticResource Key=imageRowSpan}"  
                Grid.Column="{StaticResource Key=imageColumn}"  
                Grid.ColumnSpan="{StaticResource Key=imageColumnSpan}"  
                Aspect="AspectFit"  
                HeightRequest="{StaticResource Key=maxHeight}"  
                HorizontalOptions="Center"  
                Source="{Binding Path=Photo, Converter={StaticResource Key=FillImageFromBytes}}"  
                WidthRequest="{StaticResource Key=imageWidth}" />  

In order to determine these dimensions, I am using.

    <OnPlatform x:Key="imageRow" x:TypeArguments="x:Int16">  
        <On Platform="iOS,Android">  
            <OnIdiom  
                x:TypeArguments="x:Int16"  
                Phone="0"  
                Tablet="1" />  
        </On>  
        <On Platform="WinUI,MacCatalyst" Value="1" />  
    </OnPlatform>  

    <OnPlatform x:Key="imageColumnSpan" x:TypeArguments="x:Int16">  
        <On Platform="iOS,Android">  
            <OnIdiom  
                x:TypeArguments="x:Int16"  
                Phone="2"  
                Tablet="0" />  
        </On>  
        <On Platform="WinUI,MacCatalyst" Value="0" />  
    </OnPlatform>  

    <OnPlatform x:Key="imageRowSpan" x:TypeArguments="x:Int16">  
        <On Platform="iOS,Android">  
            <OnIdiom  
                x:TypeArguments="x:Int16"  
                Phone="0"  
                Tablet="1" />  
        </On>  
        <On Platform="WinUI,MacCatalyst" Value="5" />  
    </OnPlatform>  

    <OnPlatform x:Key="imageColumn" x:TypeArguments="x:Int16">  
        <On Platform="iOS,Android">  
            <OnIdiom  
                x:TypeArguments="x:Int16"  
                Phone="0"  
                Tablet="1" />  
        </On>  
        <On Platform="WinUI,MacCatalyst" Value="0" />  
    </OnPlatform>  

When the page loads I get an exception thrown

Message: Unable to cast object of type 'Xamarin.Forms.OnPlatform`1[System.Int16]' to type 'Xamarin.Forms.GridLength'.
Type: System.InvalidCastException
Data: System.Collections.ListDictionaryInternal

When coding the grid manually you can just specify a value using integers, so I don't understand why this method doesnt work?

Any help or guidance is greatly appreciated!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,818 questions
{count} votes