如何初始化我的WPF Monogame ContentManager?

时间:2015-03-05 01:01:00

标签: wpf monogame

我已经跟随monogame wpf sample和旧版XNA wpf sample在WPF中创建了一个视图,其中包含一个通过monogame加载的模型(我也使用mvvmlight,但这不应该太重要我希望)。

除了contentManager之外,其他一切都有效。使用GraphicsDevice的本地实例

可以很好地显示顶点之外的多维数据集

根据我是使用IntPtr.Zero还是实际的窗口句柄来创建GraphicsDevice的实例,我得到了一个不同的错误,并且没有提供很多细节,所以我不知道应该告诉哪些......

当我使用IntPtr.Zero

        _services = new ServiceContainer();
        _services.AddService(typeof(IGraphicsDeviceService), _graphicsDeviceService);
        ContentManager content = new ContentManager(_services, "");
        var model = content.Load<Model3D>("psp"); // At this line

我收到有关

的错误消息
"The type initializer for 'Microsoft.Xna.Framework.TitleContainer' threw an exception."
Inner Exception: The process has no package identity. (Exception from HRESULT: 0x80073D54)

但是当我使用Actual WindowPointer时,我收到此错误

Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'E:\Documents\Visual Studio 2013\Projects\XSITE2DEV\xSite2Dev\MvvmMonogameTest\bin\Debug\MvvmMonogameTest.vshost.exe'.

Additional information: The runtime has encountered a fatal error. The address of the error was at 0x71985144, on thread 0x5f38. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

在GraphicsDeviceService中创建GraphicsDevice的第一个实例

我通过使用这些方法得到我的窗口指针

/// <summary>
/// Gets a reference to the singleton instance.
/// </summary>
public static GraphicsDeviceService AddRef(int width, int height)
{
    var singletonInstance = SimpleIoc.Default.GetService(typeof(IGraphicsDeviceService)) as GraphicsDeviceService;

    // Increment the "how many controls sharing the device" reference count.
    if (Interlocked.Increment(ref _referenceCount) == 1)
    {
        // If this is the first control to start using the
        // device, we must create the device.
        singletonInstance.EnsureGraphicsDevice();
    }

    return singletonInstance;
}

private void EnsureGraphicsDevice()
{
    if (_graphicsDevice != null)
        return;

    //CreateDevice(IntPtr.Zero, 1, 1);
    CreateDevice(new WindowInteropHelper(Application.Current.MainWindow).Handle, 1, 1);
}

private void CreateDevice(IntPtr windowHandle, int width, int height)
{
    _parameters = new PresentationParameters
    {
        BackBufferWidth = Math.Max(width, 1),
        BackBufferHeight = Math.Max(height, 1),
        BackBufferFormat = SurfaceFormat.Color,
        DepthStencilFormat = DepthFormat.Depth24,
        DeviceWindowHandle = windowHandle,
        PresentationInterval = PresentInterval.Immediate,
        IsFullScreen = false
    };

    _graphicsDevice = new GraphicsDevice(
        GraphicsAdapter.DefaultAdapter,
        GraphicsProfile.HiDef,
        _parameters);

    if (DeviceCreated != null)
        DeviceCreated(this, EventArgs.Empty);
}

我觉得使用实际的窗口指针是正确的方法,但错误没有进一步的细节,所以我无法随身携带..

1 个答案:

答案 0 :(得分:1)

我在使用您的实现时遇到了类似的错误。对我来说,它发生了,因为Game.Instance为空,如果你在MonoGame源代码中查找GraphicsDevice的源代码,你会发现构造函数依赖于它(至少在OpenGL中)。这个链接可能会有所帮助:

https://github.com/mono/MonoGame/issues/2586

基本上,您有两种选择:

  • 引入Game(例如继承自Game)将在构造函数中设置Game.Instance
  • 自定义MonoGame来源以使用您的句柄

顺便说一句,我并非100%确定这是导致错误消息的原因。

我知道这篇文章已经过时了,我很可能为时已晚,但对于遇到此错误的其他人来说,这可能会有所帮助。