[quote=“KuzDeveloper, post:2, topic:8179, full:true”]
Why are you using static methods here?[/quote]
Because I have one static class to control the music in the game.
The static constructor will be called when the first call on the static class takes place. And thats in LoadContentAuto. Which will be invoked (reflection) after the LoadContent in the game.cs
protected override void LoadContent()
{
SpriteBatch = new SpriteBatch(GraphicsDevice);
ScreenManager.AddScreen(new Loading());
}
public class Loading : Screen
{
private EntityManager manager = new EntityManager();
public Loading()
{
manager += Entities.Information.CreateLoadingInformation();
Task.Factory.StartNew(StartLoadingContent);
}
private void StartLoadingContent()
{
List<Type> methods = GetType().GetTypeInfo().Assembly.GetTypes().Where(t => t.GetMethods().ToList().Where(m => m.Name == "LoadContentAuto").Count() > 0).ToList();
foreach (Type currentMethod in methods)
currentMethod.GetMethod("LoadContentAuto").Invoke(null, new object[1] { ResourceManager.ApplicationResources });
Game.Singleton.Components.Add(new PerformanceManager(Game.Singleton));
ScreenManager.AddScreen(new SceneSwitchScreen(null, new Town()));
RemoveScreen();
}
public static class Music
{
private static Dictionary<Title, Song> songDictionary = new Dictionary<Title, Song>();
static Music()
{
MediaPlayer.MediaStateChanged += MediaPlayer_MediaStateChanged;
}
public static void LoadContentAuto(ContentManager content)
{
songDictionary.Add(Title.Town, content.Load<Song>(@"Music/Town"));
}
Checked the sourcecode of monogame. LoadContent will be called after Initialize. So it should be fine, right?
At the callstack in VS I get:
LoadContent -> LoadContentAuto -> static constructor