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 ?