question

yusuf2 avatar image
0 Votes"
yusuf2 asked Viorel-1 answered

Invalid Cast Exception in Xamarin Forms

Hi Everyone !
I have this project I need to loop throw layout to access each chiledren element, the app enters in break mode and shows this error :

System.InvalidCastException: 'Specified cast is not valid.'


  public  SilentKeyboardPage()
         {
              InitializeComponent();
    
     
                 foreach (WhiteKey wk in KeyboardAbsoluteLayout.Children)
                 {
                       
                             wk.Color = Color.Gold;
                 }        
         }







dotnet-csharpdotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

Check two of approaches:

 foreach( WhiteKey wk in KeyboardAbsoluteLayout.Children.OfType<WhiteKey>( )) 

or

 foreach( Object c in KeyboardAbsoluteLayout.Children) 
 {
    WhiteKey wk = c as WhiteKey;
    if( wk != null)
    {
        wk.Color = Color.Gold;
    }
 }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.