Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

System.TypeInitializationException has been thrown!

iameinariameinar Member ✭✭

I create custom control for my project, in which I customize the cell of DataTemplete in CollectionView.ItemTemple, but when page is run the Type Initialization Exception is occurs! is there any solution?

Best Answer

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @iameinar

    From that information, the exception is:

    System.TypeInitializationException: The type initializer for 'Stores.Controls.CustomCell.CellView' threw an exception. ---> System.ArgumentException: Default value did not match return type. Property: System.Boolean CellView.HasBorderColor Default value type: RuntimeType,
    Parameter name: defaultValue
    

    and the key bit from the stack trace would appear to be:

    Stores.Controls.CustomCell.CellView..cctor () [0x00090] in /Users/shahzaibsiddiqui/Documents/PROJECTS/Stores 15July/Stores/Controls/CustomCell/CellView.xaml.cs:30
    

    It would seem that the default value specified for HasBorderColor is not of the same type as the expected type. From the name, I would assume that the return type is a bool and that your definition looks something like the following.

    public static readonly BindableProperty HasBorderColorProperty
        = BindableProperty.Create(
           nameof(HasBorderColor),
           typeof(bool),
           typeof(CellView),
           HasBorderColorDefault);
    

    If that is the case, either your definition does not have typeof(bool) or HasBorderColorDefault is not a bool.

    If that's not the case, post the code of your CellView.xaml.cs and the XAML of CellView.xaml

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    @iameinar said:
    I create custom control for my project, in which I customize the cell of DataTemplete in CollectionView.ItemTemple, but when page is run the Type Initialization Exception is occurs! is there any solution?

    Post the entire stack trace from the exception.

  • iameinariameinar Member ✭✭

    System.TypeInitializationException: The type initializer for 'Stores.Controls.CustomCell.CellView' threw an exception. ---> System.ArgumentException: Default value did not match return type. Property: System.Boolean CellView.HasBorderColor Default value type: RuntimeType,
    Parameter name: defaultValue
    at Xamarin.Forms.BindableProperty..ctor (System.String propertyName, System.Type returnType, System.Type declaringType, System.Object defaultValue, Xamarin.Forms.BindingMode defaultBindingMode, Xamarin.Forms.BindableProperty+ValidateValueDelegate validateValue, Xamarin.Forms.BindableProperty+BindingPropertyChangedDelegate propertyChanged, Xamarin.Forms.BindableProperty+BindingPropertyChangingDelegate propertyChanging, Xamarin.Forms.BindableProperty+CoerceValueDelegate coerceValue, Xamarin.Forms.BindableProperty+BindablePropertyBindingChanging bindingChanging, System.Boolean isReadOnly, Xamarin.Forms.BindableProperty+CreateDefaultValueDelegate defaultValueCreator) [0x000b3] in D:\a\1\s\Xamarin.Forms.Core\BindableProperty.cs:85
    at Xamarin.Forms.BindableProperty.Create (System.String propertyName, System.Type returnType, System.Type declaringType, System.Object defaultValue, Xamarin.Forms.BindingMode defaultBindingMode, Xamarin.Forms.BindableProperty+ValidateValueDelegate validateValue, Xamarin.Forms.BindableProperty+BindingPropertyChangedDelegate propertyChanged, Xamarin.Forms.BindableProperty+BindingPropertyChangingDelegate propertyChanging, Xamarin.Forms.BindableProperty+CoerceValueDelegate coerceValue, Xamarin.Forms.BindableProperty+CreateDefaultValueDelegate defaultValueCreator) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableProperty.cs:145
    at Stores.Controls.CustomCell.CellView..cctor () [0x00090] in /Users/shahzaibsiddiqui/Documents/PROJECTS/Stores 15July/Stores/Controls/CustomCell/CellView.xaml.cs:30
    --- End of inner exception stack trace ---
    at Xamarin.Forms.ElementTemplate.CreateContent () [0x00026] in D:\a\1\s\Xamarin.Forms.Core\ElementTemplate.cs:82
    at Xamarin.Forms.Platform.iOS.TemplatedCell.Bind (Xamarin.Forms.DataTemplate template, System.Object bindingContext, Xamarin.Forms.ItemsView itemsView) [0x00052] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\TemplatedCell.cs:91
    at Xamarin.Forms.Platform.iOS.ItemsViewController1[TItemsView].UpdateTemplatedCell (Xamarin.Forms.Platform.iOS.TemplatedCell cell, Foundation.NSIndexPath indexPath) [0x00012] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewController.cs:199 at Xamarin.Forms.Platform.iOS.ItemsViewController1[TItemsView].GetCell (UIKit.UICollectionView collectionView, Foundation.NSIndexPath indexPath) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewController.cs:84
    at Xamarin.Forms.Platform.iOS.ItemsViewController1[TItemsView].GetPrototype () [0x0005d] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewController.cs:263 at Xamarin.Forms.Platform.iOS.ItemsViewLayout.DetermineCellSize () [0x0004e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewLayout.cs:211 at Xamarin.Forms.Platform.iOS.ListViewLayout.ConstrainTo (CoreGraphics.CGSize size) [0x0001e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ListViewLayout.cs:16 at Xamarin.Forms.Platform.iOS.ItemsViewLayout.UpdateConstraints (CoreGraphics.CGSize size) [0x0003f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewLayout.cs:96 at Xamarin.Forms.Platform.iOS.ItemsViewLayout.InvalidateLayout () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewLayout.cs:545 at at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSend_Int64(intptr,intptr,long) at UIKit.UIView.set_SemanticContentAttribute (UIKit.UISemanticContentAttribute value) [0x00015] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.2.1/src/Xamarin.iOS/UIView.g.cs:4698 at Xamarin.Forms.Platform.iOS.FlowDirectionExtensions.UpdateFlowDirection (UIKit.UIView view, Xamarin.Forms.IVisualElementController controller) [0x00041] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Extensions\FlowDirectionExtensions.cs:35 at Xamarin.Forms.Platform.iOS.ItemsViewRenderer2[TItemsView,TViewController].UpdateFlowDirection () [0x0000e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewRenderer.cs:136
    at Xamarin.Forms.Platform.iOS.ItemsViewRenderer2[TItemsView,TViewController].SetUpNewElement (TItemsView newElement) [0x00064] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewRenderer.cs:103 at Xamarin.Forms.Platform.iOS.StructuredItemsViewRenderer2[TItemsView,TViewController].SetUpNewElement (TItemsView newElement) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\StructuredItemsViewRenderer.cs:41
    at Xamarin.Forms.Platform.iOS.SelectableItemsViewRenderer2[TItemsView,TViewController].SetUpNewElement (TItemsView newElement) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\SelectableItemsViewRenderer.cs:34 at Xamarin.Forms.Platform.iOS.ItemsViewRenderer2[TItemsView,TViewController].OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs1[TElement] e) [0x0000c] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewRenderer.cs:36 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].SetElement (TElement element) [0x00122] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:292
    at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:157
    at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00014] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:227
    at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:100
    at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () [0x0001e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:49
    at Xamarin.Forms.Platform.iOS.PageRenderer.ViewDidLoad () [0x0008f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\PageRenderer.cs:245
    at at (wrapper managed-to-native) ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper(intptr,intptr)
    at UIKit.UIViewController.get_View () [0x0002a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.2.1/src/Xamarin.iOS/UIViewController.g.cs:3172
    at Xamarin.Forms.Platform.iOS.PageRenderer.get_NativeView () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\PageRenderer.cs:111
    at Xamarin.Forms.Platform.iOS.PageRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x0003d] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\PageRenderer.cs:127
    at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00014] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:227
    at Xamarin.Forms.Platform.iOS.NavigationRenderer.CreateViewControllerForPage (Xamarin.Forms.Page page) [0x00008] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:379
    at Xamarin.Forms.Platform.iOS.NavigationRenderer.OnPushAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x0001d] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:355
    at Xamarin.Forms.NavigationPage.PushAsyncInner (Xamarin.Forms.Page page, System.Boolean animated) [0x00084] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:441
    at Xamarin.Forms.NavigationPage.PushAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x0014e] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:254
    at Stores.Pages.SignIn.SignInPage.SignInClicked (System.Object sender, System.EventArgs e) [0x0006b] in /Users/shahzaibsiddiqui/Documents/PROJECTS/Stores 15July/Stores/Pages/SignIn/SignInPage.xaml.cs:28
    at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
    at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.2.1/src/Xamarin.iOS/Foundation/NSAction.cs:178
    at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
    at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.2.1/src/Xamarin.iOS/UIKit/UIApplication.cs:86
    at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.2.1/src/Xamarin.iOS/UIKit/UIApplication.cs:65
    at Stores.iOS.Application.Main (System.String[] args) [0x00001] in /Users/shahzaibsiddiqui/Documents/PROJECTS/Stores 15July/Stores.iOS/Main.cs:17
    ~~~~

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @iameinar

    From that information, the exception is:

    System.TypeInitializationException: The type initializer for 'Stores.Controls.CustomCell.CellView' threw an exception. ---> System.ArgumentException: Default value did not match return type. Property: System.Boolean CellView.HasBorderColor Default value type: RuntimeType,
    Parameter name: defaultValue
    

    and the key bit from the stack trace would appear to be:

    Stores.Controls.CustomCell.CellView..cctor () [0x00090] in /Users/shahzaibsiddiqui/Documents/PROJECTS/Stores 15July/Stores/Controls/CustomCell/CellView.xaml.cs:30
    

    It would seem that the default value specified for HasBorderColor is not of the same type as the expected type. From the name, I would assume that the return type is a bool and that your definition looks something like the following.

    public static readonly BindableProperty HasBorderColorProperty
        = BindableProperty.Create(
           nameof(HasBorderColor),
           typeof(bool),
           typeof(CellView),
           HasBorderColorDefault);
    

    If that is the case, either your definition does not have typeof(bool) or HasBorderColorDefault is not a bool.

    If that's not the case, post the code of your CellView.xaml.cs and the XAML of CellView.xaml

Sign In or Register to comment.