我正在WPF文件中为学校中的项目设置游戏。我们需要制造一种飞扬的鸟,并且已经完成了鸟和碰撞检测。现在,我们需要根据给出的XML文件中的设置将矩形放置在WPF中。我不知道应该如何使用XML在WPF中获取矩形。
我尝试过在线搜索问题,但没有找到解决方案。我已经读过XML不应该用于这类应用程序,但是我们必须要做的是分配XML。
XML文件:
<GameLevel number="1">
<Settings>
<Name>Mania</Name>
<Dimensions>
<Width>1200</Width>
<Height>500</Height>
</Dimensions>
</Settings>
<Blocks>
<Block Position="100" GapHeight="100" />
<Block Position="250" GapHeight="20" />
<Block Position="320" GapHeight="400" />
<Block Position="540" GapHeight="300" />
<Block Position="870" GapHeight="50" />
</Blocks>
</GameLevel>
阅读
private void ReadXmlFile()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"XMLConfig.xml"); //laad xmlbestand in xmlDoc
XmlNodeList ListNodes =
xmlDoc.SelectNodes("//Employees/Employee"); //Zoek nodes
foreach (XmlNode Rectangles in ListNodes)
{
string empId = Rectangles["ID"].InnerText; //Selecteer ID node per Employee
}
}
编辑:我知道节点尚未完全完成,因此无需指出
答案 0 :(得分:0)
在WPF应用程序中在屏幕上显示一组框的通常方法是从ViewModel公开框坐标,然后让XAML在Canvas对象中将坐标显示为矩形。
WPF视图将其DataContext属性绑定到一个ViewModel,该ViewModel公开一个称为Blocks或其他具有相同描述性的List属性。这将绑定到将ItemHost属性设置为Canvas的ItemsControl,每个项目的X,Y,Width和Height属性都由您反序列化的XML设置中的值确定。
在下面的XAML部分中描述了XAML表示形式,而在下面的XmlGameConfiguration类中可以找到反序列化要由ViewModel在渲染视图时使用的XML文件的更有用的方法,该方法利用XmlSerializer和各种XmlXXX属性来反序列化您的xml文件。
XAML
<Window x:Class="SO56164654.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SO56164654"
xmlns:viewModel="clr-namespace:SO56164654.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="{x:Type viewModel:LevelViewModel}">
<ItemsControl ItemsSource="{Binding Blocks}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Width="{Binding Width}" Height="{Binding Height}" Fill="Black"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</Grid.Resources>
<ContentControl Content="{Binding CurrentLevel}" />
</Grid>
</Window>
ViewModels
public class LevelViewModel
{
public ObservableCollection<BlockViewModel> Blocks { get; }
}
public class BlockViewModel
{
public LevelBlock _model { get; set; }
public BlockViewModel(LevelBlock model)
{
_model = model;
}
public int X
{
get { return _model.Position; }
}
public int Y
{
get { return _model.Position; }
}
public int Height
{
get { return _model.GapHeight; }
}
public int Width
{
get { return _model.GapHeight; }
}
}
模型对象
[DataContract(Namespace = "")]
public class Game
{
[XmlArray("Levels")]
public List<GameLevel> Levels { get; set; }
}
[DataContract(Name = "GameLevel", Namespace = "")]
public class GameLevel
{
[XmlAttribute("number")]
public int Number { get; set; }
public GameLevelSettings Settings { get; set; }
[XmlArray("Blocks")]
[XmlArrayItem("Block")]
public List<LevelBlock> Blocks { get; set; }
}
public class GameLevelDimensions
{
public int Height { get; set; }
public int Width { get; set; }
}
public class GameLevelSettings
{
public string Name { get; set; }
public GameLevelDimensions Dimensions { get; set; }
}
public class LevelBlock
{
[XmlAttribute("Position")]
public int Position { get; set; }
[XmlAttribute("GapHeight")]
public int GapHeight { get; set; }
}
internal class XmlGameConfiguration : IGameConfiguration
{
private const string ConfigFileName = "game.config";
public XmlGameConfiguration(string configFilePath)
{
using (var stream = new FileStream(configFilePath, FileMode.Open))
{
var ser = new XmlSerializer(typeof(Game));
Game = (Game) ser.Deserialize(stream);
}
}
public XmlGameConfiguration() : this(ConfigFileName)
{}
public Game Game { get; private set; }
}