Question

(Windows Store app, using VB/XAML)

I am currently playing sound effects in my game from App.Xaml.vb via App.Current (to play sounds from other pages), and it works well. However, to make my code neater I had the idea to move all my sound effects into a module where they can still be accessed from anywhere in the app.

It seems that a module only loads up into memory when it's first needed (is this correct)? Meaning that the first time I try to play a sound effect it is silent (because the module hasn't had time to load what it needs to).

Can I force a module into memory upon app launch so that it is accessible to all my app pages? I currently load all the sounds (there's only about half a dozen) when my app launches anyway, but doing it in a discrete module would be 'neater'.

I've tried adding 'Imports MyNamespace.AudioModule', but that doesn't make the module load before being accessed/used the first time.

I can get it to work with what feels like an ugly, unnecessary hack: if I play a sound (from the module) when the app first launches, the module initialises (all sounds are loaded in the constructor), and sounds work after that point (this first sound is silent because the module hasn't loaded yet).

Is there an obvious, fundamental answer I'm missing here?

Was it helpful?

Solution

For VB Module or C# Static class the constructor is run and values populated the first time a method or function is called. One way to force this would be to call a dummy method.

Example from Microsoft Exchange 2013 101 Code Samples:

// C#
CertificateCallback.Initialize(); // Instantiate static class

public static class CertificateCallback {
    static CertificateCallback() { // Static constructor
        ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
    }

public static void Initialize() { // Dummy method to force initialization
}
...
}

'VB.NET
CertificateCallback.Initialize() // Instantiate static class

Public Module CertificateCallback
    Sub New() ' Static constructor
        ServicePointManager.ServerCertificateValidationCallback = AddressOf CertificateValidationCallBack
    End Sub

Public Sub Initialize() // Dummy method to force initialization
End Sub
...
End Module
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top