Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Why Am I Getting A Type Initialization Exception when Merging a ResourceDictionary In Code Behind?

$
0
0

Hey guys,

So I'm working on making an app more device friendly across small, large and tablet devices. I created a couple Resource Dictionaries for styling, and I have a bool to determine if the screen is small or large, then depending on what is returned from the bool determines which dictionary to merge...

But when I run the app, I get the following:

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'MyApp.Styles.LargeScreenDictionary' threw an exception.

In my debug output this is all I see:

System.TypeInitializationException: The type initializer for 'HthApp.Styles.LargeScreenDictionary' threw an exception.

Thread finished: <Thread Pool> #5
The thread 0x5 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #4
The thread 0x4 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #6
The thread 0x6 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #3
The thread 0x3 has exited with code 0 (0x0).

=================================================================
    Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries  
used by your application.
=================================================================

=================================================================
    Basic Fault Adddress Reporting
=================================================================
Memory around native instruction pointer (0x7c1360d678):0x7c1360d668  00 1c 40 b9 c0 03 5f d6 fd 7b bf a9 fd 03 00 91  ..@..._..{......
0x7c1360d678  08 20 40 b9 a8 00 c8 37 88 01 e0 37 00 00 40 f9  . @....7...7..@.
0x7c1360d688  fd 7b c1 a8 c0 03 5f d6 60 08 00 b0 01 0a 00 f0  .{...._.`.......
0x7c1360d698  03 0a 00 f0 00 24 06 91 21 80 08 91 63 8c 0a 91  .....$..!...c...

No native Android stacktrace (see debuggerd output).

===============================================
07-09 08:54:13.992 W/thackney.hthap( 3681): 0xebadde09 skipped times: 0==================
    Managed Stacktrace:
=================================================================
=================================================================

07-09 08:54:13.992 E/mono-rt ( 3681): /proc/self/maps:
07-09 08:54:13.992 E/mono-rt ( 3681): 12c00000-12c80000 rw-p 00000000 00:05 27566                              /dev/ashmem/dalvik-main space (region space)_947_947 (deleted)
07-09 08:54:13.992 E/mono-rt ( 3681): 12c80000-160c0000 ---p 00080000 00:05 27566                              /dev/ashmem/dalvik-main space (region space)_947_947 (deleted)
07-09 08:54:13.992 E/mono-rt ( 3681): 160c0000-16600000 ---p 034c0000 00:05 27566                              /dev/ashmem/dalvik-main space (region space)_947_947 (deleted)
07-09 08:54:13.992 E/mono-rt ( 3681): 16600000-166c0000 rw-p 03a00000 00:05 27566                              /dev/ashmem/dalvik-main space (region space)_947_947 (deleted)
07-09 08:54:13.992 E/mono-rt ( 3681): 166c0000-17b00000 ---p 03ac0000 00:05 27566                              /dev/ashmem/dalvik-main space (region space)_947_947 (deleted)
07-09 08:54:13.993 E/mono-rt ( 3681): 17b00000-17b40000 rw-p 04f00000 00:05 27566                              /dev/ashmem/dalvik-main space (region space)_947_947 (deleted)
07-09 08:54:13.993 E/mono-rt ( 3681): 17b40000-17f80000 ---p 04f40000 00:05 27566                              /dev/ashmem/dalvik-main space (region space)_947_947 (deleted)
07-09 08:54:13.993 E/mono-rt ( 3681): 17f80000-32c00000 rw-p 05380000 00:05 27566                              /dev/ashmem/dalvik-main space (region space)_947_947 (deleted)
07-09 08:54:13.993 E/mono-rt ( 3681): 6f9ee000-6fcc9000 rw-p 00000000 fe:03 1900625                            /data/dalvik-cache/arm64/system@framework@boot.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6fcc9000-6fcdf000 r--p 002db000 fe:03 1900625                            /data/dalvik-cache/arm64/system@framework@boot.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6fcdf000-6fe14000 rw-p 00000000 fe:03 1900634                            /data/dalvik-cache/arm64/system@framework@boot-core-libart.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6fe14000-6fe26000 r--p 00135000 fe:03 1900634                            /data/dalvik-cache/arm64/system@framework@boot-core-libart.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6fe26000-6fe66000 rw-p 00000000 fe:03 1900643                            /data/dalvik-cache/arm64/system@framework@boot-conscrypt.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6fe66000-6fe69000 r--p 00040000 fe:03 1900643                            /data/dalvik-cache/arm64/system@framework@boot-conscrypt.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6fe69000-6feb0000 rw-p 00000000 fe:03 1900649                            /data/dalvik-cache/arm64/system@framework@boot-okhttp.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6feb0000-6feb4000 r--p 00047000 fe:03 1900649                            /data/dalvik-cache/arm64/system@framework@boot-okhttp.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6feb4000-6ff1a000 rw-p 00000000 fe:03 1900657                            /data/dalvik-cache/arm64/system@framework@boot-bouncycastle.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6ff1a000-6ff21000 r--p 00066000 fe:03 1900657                            /data/dalvik-cache/arm64/system@framework@boot-bouncycastle.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6ff21000-6ff82000 rw-p 00000000 fe:03 1900664                            /data/dalvik-cache/arm64/system@framework@boot-apache-xml.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6ff82000-6ff89000 r--p 00061000 fe:03 1900664                            /data/dalvik-cache/arm64/system@framework@boot-apache-xml.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6ff89000-6ffcd000 rw-p 00000000 fe:03 1900672                            /data/dalvik-cache/arm64/system@framework@boot-ext.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6ffcd000-6ffd8000 r--p 00044000 fe:03 1900672                            /data/dalvik-cache/arm64/system@framework@boot-ext.art
07-09 08:54:13.993 E/mono-rt ( 3681): 6ffd8000-70be4000 rw-p 00000000 fe:03 1900682                            /data/dalvik-cache/arm64/system@framework@boot-framework.art
07-09 08:54:13.993 E/mono-rt ( 3681): 70be4000-70c44000 r--p 00c0c000 fe:03 1900682                            /data/dalvik-cache/arm64/system@framework@boot-framework.art
07-09 08:54:13.993 E/mono-rt ( 3681): 70c44000-70dcd000 rw-p 00000000 fe:03 1900685                            /data/dalvik-cache/arm64/system@framework@boot-telephony-common.art
07-09 08:54:13.994 F/libc    ( 3681): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x20 in tid 3681 (myco.myapp), pid 3681 (myco.myapp)
Thread started: <Thread Pool> #9
Thread started: <Thread Pool> #10
Thread started: <Thread Pool> #11

The Code I'm working with:

App.xaml

<Application
    x:Class="HthApp.App"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Application.Resources>
        <ResourceDictionary x:Name="MainDictionary">
            <Color x:Key="pageBackgroundColor">#FFFFFF</Color>
            <Color x:Key="flyoutGradientStart">#ED1B24</Color>
            <Color x:Key="flyoutGradientEnd">#8C0107</Color>
            <!-- More Styles ... truncated -->
        </ResourceDictionary>
    </Application.Resources>
</Application>

Code Behind

public partial class App : Application 
{
    const int smallWidthResolution = 768;
    const int smallHeightResolution = 1280;

    public App()
    {
        InitializeComponent();
        LoadStyles();
    }

    void LoadStyles()
    {
         if (IsSmallDevice())
         {
             MainDictionary.MergedDictionaries.Add(SmallScreenDictionary.Instance);
         } else 
         {
             MainDictionary.MergedDictionaries.Add(LargeScreenDictionary.Instance);
         }
    }

    public static bool IsSmallDevice()
    {
        // Get Display Metrics
        var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;

        // Get Width Metrics (in pixels)
        var mainWidth = mainDisplayInfo.Width;

        // Get Height Metrix (in pixels)
        var mainHeight = mainDisplayInfo.Height;

        // Return
        return (mainWidth <= smallWidthResolution && mainHeight <= smallHeightResolution);
    }

LargeDictionary.xaml SmallDictionary is identical set up, just different style setters

<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary
    x:Class="HthApp.Styles.LargeScreenDictionary"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
<!-- Lots of unique Styles -->
</ResourceDictionary>

LargeDictionary code behind

namespace MyApp.Styles
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LargeScreenDictionary : ResourceDictionary
    {
        public static LargeScreenDictionary Instance { get; } = new LargeScreenDictionary();

        public LargeScreenDictionary()
        {
            InitializeComponent();
        }
    }
}

What am I missing? Or should I be building styles for multiple resolutions in a different way?


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>