【发布时间】:2015-08-10 12:55:57
【问题描述】:
序列化此 XML 时出现奇怪的错误:
<?xml version="1.0" encoding="utf-8" ?>
<SplashScreen>
<Image>
<Path>Content/splash</Path>
</Image>
</SplashScreen>
错误:
“'System.InvalidOperationException' 类型的第一次机会异常 发生在 System.Xml.dll 附加信息: 反思 EasyRPG.SplashScreen 发生的错误类型。如果有一个处理程序 这个异常,程序可以安全地继续。”
XMLManager 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace EasyRPG.Managers {
public class XmlManager<T> {
public Type Type;
public T Load (String path) {
T instance;
using (TextReader reader = new StreamReader(path)){
XmlSerializer xml = new XmlSerializer(Type);
instance = (T)xml.Deserialize(reader);
}
return instance;
}
public void Save (string path, object obj) {
using (TextWriter writer = new StreamWriter(path)) {
XmlSerializer xml = new XmlSerializer(Type);
xml.Serialize(writer, obj);
}
}
}
}
我迷路了,我尝试了我所知道的一切(虽然不多),但仍然一无所获。
如果需要图片类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using EasyRPG;
namespace TEAM_TheCity.Source {
public class Image {
public float Alpha;
public string Path;
public string Text, FontName;
public Vector2 Position;
public Vector2 Scale;
public Rectangle SourceRect;
public SpriteFont font;
public GraphicsDevice GraphcsDevice;
public Texture2D Texture;
Vector2 origin;
ContentManager content;
RenderTarget2D renderTarget;
public SpriteBatch SpriteBatch;
public Image () {
Path = Text = String.Empty;
FontName = "Orbitron";
Position = Vector2.Zero;
Scale = Vector2.One;
Alpha = 1.0F;
SourceRect = Rectangle.Empty;
}
public void LoadContent(){
content = new ContentManager(ScreenManager.Manager.Content.ServiceProvider, "Content");
if(Path != String.Empty) Texture = content.Load<Texture2D>(Path);
font = content.Load<SpriteFont>(FontName);
Vector2 dimensions = Vector2.Zero;
if(Texture != null)
dimensions.X += Texture.Width;
dimensions.X += font.MeasureString(Text).X;
if(Texture != null)
dimensions.Y = Math.Max(Texture.Height, font.MeasureString(Text).Y);
else
dimensions.Y = font.MeasureString(Text).Y;
if(SourceRect == Rectangle.Empty)
SourceRect = new Rectangle(0,0, (int)dimensions.X, (int)dimensions.Y);
renderTarget = new RenderTarget2D(ScreenManager.Manager.GraphicsDevice,(int) dimensions.X, (int)dimensions.Y);
ScreenManager.Manager.GraphicsDevice.SetRenderTarget(renderTarget);
ScreenManager.Manager.GraphicsDevice.Clear(Color.Transparent);
ScreenManager.Manager.SpriteBatch.Begin();
if (Texture != null)
ScreenManager.Manager.SpriteBatch.Draw(Texture, Vector2.Zero, Color.White);
ScreenManager.Manager.SpriteBatch.DrawString(font, Text, Vector2.Zero, Color.White);
ScreenManager.Manager.SpriteBatch.End();
Texture = renderTarget;
ScreenManager.Manager.GraphicsDevice.SetRenderTarget(null);
}
public void UnloadContent(){
}
public void Update(GameTime gameTime){
}
public void Draw(SpriteBatch SpriteBatch) {
origin = new Vector2(SourceRect.Width / 2, SourceRect.Height / 2);
SpriteBatch.Draw(Texture, Position + origin, SourceRect, Color.White, 0.0f, origin, Scale, SpriteEffects.None, 0.0f);
}
}
}
还有 SplashScreen 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using TEAM_TheCity.Source;
namespace EasyRPG {
public class SplashScreen : GameScreen{
public Image Image;
public SplashScreen () {
}
public override void LoadContent () {
base.LoadContent();
Image.LoadContent();
}
public override void UnloadContent () {
base.LoadContent();
Image.UnloadContent();
}
public override void Update (GameTime gameTime) {
base.Update(gameTime);
Image.Update(gameTime);
}
public override void Draw (SpriteBatch spriteBatch) {
base.Draw(spriteBatch);
Image.Draw(spriteBatch);
}
}
}
GameScreen 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace EasyRPG {
public class GameScreen {
protected ContentManager content;
[XmlIgnore]
public Type Type;
public GameScreen () {
Type = this.GetType();
}
public virtual void LoadContent () {
content = new ContentManager(ScreenManager.Manager.Content.ServiceProvider, "Content");
}
public virtual void UnloadContent () {
content.Unload();
}
public virtual void Update (GameTime gameTime) {}
public virtual void Draw (SpriteBatch spriteBatch) {}
}
}
P.S.:抱歉,我写了这么多代码,但我是 XML 新手,我不知道什么是重要的,什么不是
【问题讨论】:
-
您两次发布了
SplashScreen课程。可以发GameScreen和Image吗? -
哦,谢谢,没注意到
-
我怀疑您会通过调查您的异常的
InnerException找到更多信息。我猜Image中的一个或多个公共字段不受支持。
标签: c# xml serialization xna monogame