Xamarin.forms: [Bug] System.InvalidOperationException when using AppThemeBinding inside of OnPlatform

Created on 5 Aug 2020  Â·  3Comments  Â·  Source: xamarin/Xamarin.Forms

Description

I keep getting a System.InvalidOperationException when using AppThemeBinding inside of OnPlatform on both Android and iOS.

Steps to Reproduce

<Application.Resources>
    <Style x:Name="DefaultButton" TargetType="Button">
        <Setter Property="TextColor">
            <Setter.Value>
                <OnIdiom x:TypeArguments="Color">
                    <OnIdiom.Phone>
                        <OnPlatform x:TypeArguments="Color">
                            <On Platform="Android, iOS" Value="{AppThemeBinding Light={AppThemeBinding Light=Green, Dark=Red}}"/>
                        </OnPlatform>
                    </OnIdiom.Phone>
                    <OnIdiom.TV>#FFFFFF</OnIdiom.TV>
                </OnIdiom>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

Basic Information

  • Version with issue: 4.7.0.1239
  • Last known good version: none
  • IDE: VS for Mac
darkmode bug enhancement âž•

All 3 comments

@StephaneDelcroix

You can't use an AppThemeBinding to set a property of OnPlatform, as those props aren't Bindable.

I'm keeping this open as you should get a better error message.

If you want this to work, you might have some success by using BindingBase as x:TypeArguments

@StephaneDelcroix (sorry I saw late the closed issue https://github.com/xamarin/Xamarin.Forms/issues/10973) I believe even without OnPlatform it will throw an exception I mention the code snippet from https://github.com/xamarin/Xamarin.Forms/issues/10973#issuecomment-651676520

<ContentPage.Resources>
  <AppThemeBinding x:Key="FancyColor" x:Shared="false">
    <AppThemeBinding.Light><Color>HotPink</Color></AppThemeBinding.Light>
    <AppThemeBinding.Dark><Color>LimeGreen</Color></AppThemeBinding.Dark>
  </AppThemeBinding>
</ContentPage.Resources>

<StackLayout>
  <Label BackgroundColor="{StaticResource FancyColor}"/>
  <Label BackgroundColor="{StaticResource FancyColor}"/>
</StackLayout>

Throwing exception at the startup of the app

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

I don't think I need to open a separate issue but if you think so I will.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simontocknell picture simontocknell  Â·  3Comments

suihanhbr picture suihanhbr  Â·  3Comments

Stensan picture Stensan  Â·  3Comments

Hudhud picture Hudhud  Â·  3Comments

joseluisct picture joseluisct  Â·  3Comments