C# Can';在XNA中,无法在屏幕上显示Awesomium渲染

C# Can';在XNA中,无法在屏幕上显示Awesomium渲染,c#,xna,awesomium,C#,Xna,Awesomium,我目前正在使用有人建议将其与XNA结合使用的代码,但是我遇到了一个问题。。。每当我尝试绘制组件提供给我的结果纹理2D时,我得到: 当资源在GraphicsDevice上处于活动设置状态时,不能调用该资源上的SetData。在调用SetData之前,将其从设备中取消设置 这是我的密码: using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Mic

我目前正在使用有人建议将其与XNA结合使用的代码,但是我遇到了一个问题。。。每当我尝试绘制组件提供给我的结果纹理2D时,我得到:

当资源在GraphicsDevice上处于活动设置状态时,不能调用该资源上的SetData。在调用SetData之前,将其从设备中取消设置

这是我的密码:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Awesomium.Core;

namespace ProjectTest
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        AwesomiumComponent uiHud;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            Rectangle rect = new Rectangle(100, 100, 500, 500);
            uiHud = new AwesomiumComponent(this, rect);
            this.Components.Add(uiHud);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            uiHud.WebView.LoadURL("http://www.google.com/");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            //uiHud.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            Vector2 pos = new Vector2(10, 10);


            spriteBatch.Begin();
            spriteBatch.Draw(uiHud.WebViewTexture, pos, Color.White);
            spriteBatch.End();

            //uiHud.Draw(gameTime);
            // TODO: Add your drawing code here
            base.Draw(gameTime);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
使用冬虫夏草;
命名空间项目测试
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
AwesomiumComponent uiHud;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
矩形rect=新矩形(100100500500);
uiHud=新的AwesomiumComponent(此,rect);
此.Components.Add(uiHud);
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
uiHud.WebView.LoadURL(“http://www.google.com/");
//TODO:使用此.Content在此处加载游戏内容
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//TODO:在此处添加更新逻辑
//更新(游戏时间);
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
向量2位置=新向量2(10,10);
spriteBatch.Begin();
spriteBatch.Draw(uiHud.webview纹理、位置、颜色、白色);
spriteBatch.End();
//uiHud.Draw(游戏时间);
//TODO:在此处添加图形代码
基础。抽签(游戏时间);
}
}
}

我到底做错了什么?

您的
SpriteBatch
正在将web纹理设置到图形设备的第一个纹理阶段,并且从未将其取消设置。这导致Awesomium的
Draw()
方法失败,因为它试图对仍在使用的纹理调用
SetData()

spriteBatch.End()之后,取消设置纹理阶段:

GraphicsDevice.Textures[0] = null;

你确定你应该用spritebatch绘制你的hud吗?组件本身似乎公开了一个
Draw
方法。你试过用它来代替吗?@Dervall我试过了,但是我很确定它不是这样工作的。再说,我这样做的时候什么也没出现。哇,那真的很管用。。。但有没有一个不那么老套的解决方案?甚至编辑组件。一旦我开始使用大量纹理,你的解决方案似乎就行不通了,更不用说其他可能的问题了(?)你正在使用的纹理的数量无关紧要;只涉及一个纹理阶段。您需要在调用组件的
Draw()
方法之前取消设置该阶段,或者使用不同的纹理绘制某些内容,以便为您取消设置纹理。我怀疑后者将是更自然的解决方案。只是回应科尔的评论,它一点也不令人讨厌。基本上,所有渲染都是通过在图形设备上设置各种状态,然后调用其
Draw
方法之一来实际触发绘图来实现的
SpriteBatch
实际上是在其易于使用的包装器中执行
GraphicsDevice.Textures[0]=yourTexture
(以及一系列其他内容)。