0

I have the following :

File : UserControlTest.xaml

<UserControl
x:Class="MySolution.Views.UserControlTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MySolution.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:MySolution.ViewModels"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<UserControl.DataContext>
    <viewModels:MyViewModel/>
</UserControl.DataContext>
<Grid />
</UserControl>

File : MyViewModel .cs

namespace MySolution.ViewModels
{
    public class MyViewModel : NotifyPropertyChanged
    {
        try
        {
            var test = Global.Test;
        }
        catch (Exception ex)
        {
            LogHelper.WriteLogFile(ex);
        }
    }
}

File : Global.cs

namespace Expert.Engine
{
    public class Global
    {
        public static string Test { get; set; } = "Test Value";
    }
}

When i debug the XAML (attaching to process menu => XDesProc.exe relative to the above xaml file), the debugger is firing a 'System.TypeInitializationException' on Global.Test access in MyViewModel .cs :

try
{
    var test = Global.Test;
}
catch (Exception ex)
{
    LogHelper.WriteLogFile(ex);
}

Does anyone know why it does not work ? It seems that the CLR hasn't yet defined the Static class, but how make it work ?

midget
  • 5
  • 1
  • And `Global.Test` is indeed that simple as stated in question? Also, nothing in inner exception of that TypeInitializationException? – Evk Oct 19 '17 at 10:17
  • Not sure if this works with XAML, but worth a try: https://stackoverflow.com/questions/13698388/how-to-troubleshoot-and-debug-visual-studio-design-mode-errors/13698442#13698442 – user6144226 Oct 19 '17 at 11:05

2 Answers2

0

Thank you for your help. Problem solved : It was finally due to a problem of Null value during the object initalization. The designer couldn't resolve one object, so then i got an error message.

midget
  • 5
  • 1
-1

You can try this change you code in file "Global.cs"

public class Global change it public static class Global

khanh2990
  • 1
  • 2