我正在尝试为我用C#编写的小玩具程序创建一个WPF UI。程序只找到两点之间的最短路径。显示器创建一个正方形网格,显示它的单元格类型(空白区域,小行星,重力井,重力影响,开始,结束和路径)。
运行我创建的UI的最佳方式是什么?我刚刚在我的UI中将Window
变量设为公共变量,并在Application
类中创建了Program.cs
,并在Run
上调用了Window
。我曾尝试过Run
- 同时使用两个不同的窗口,只显示一个 - 我如何同时显示这两个窗口?
此外,是否有一种方法可以使我的网格在描述每个单元格的数组内容发生变化时更新?例如如果一个单元格从一个小行星变为另一个具有不同颜色的空白空间,是否有办法让我的UI知道它已更改并相应更新?
以下是用户界面的源代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace SpaceProbe
{
public class SpaceProbeView
{
public Window window = new Window();
public Window optionWindow = new Window();
Canvas canvas = new Canvas();
private int cellSize = 25;
SpaceProbeModel model;
int rowAndColumnSize;
SolidColorBrush emptySpaceBrush = Brushes.DarkBlue;
SolidColorBrush asteroidBrush = Brushes.DarkGreen;
SolidColorBrush gravityWellBrush = Brushes.Magenta;
SolidColorBrush gravityInfluencedBrush = Brushes.DarkMagenta;
SolidColorBrush pathBrush = Brushes.DarkTurquoise;
SolidColorBrush startBrush = Brushes.Black;
SolidColorBrush finishBrush = Brushes.DarkOrange;
Brush outlineBrush = Brushes.Black;
public SpaceProbeView(int size)
{
rowAndColumnSize = size;
window.Title = "Space Probe";
canvas.Width = rowAndColumnSize * cellSize;
canvas.Height = rowAndColumnSize * cellSize;
window.Width = rowAndColumnSize * cellSize;
window.Height = rowAndColumnSize * cellSize;
window.Content = canvas;
window.SizeToContent = SizeToContent.WidthAndHeight;
//window.Show();
optionWindow.Width = 100;
optionWindow.Height = 200;
//optionWindow.Show();
}
public void SetModel(SpaceProbeModel aModel)
{
model = aModel;
for (int row = 0; row < rowAndColumnSize; row++)
{
for (int column = 0; column < rowAndColumnSize; column++)
{
Rectangle rect = new Rectangle();
rect.Width = cellSize;
rect.Height = cellSize;
rect.Stroke = outlineBrush;
switch (model.cellType[row, column])
{
case SpaceProbeModel.type.start:
rect.Fill = startBrush;
break;
case SpaceProbeModel.type.finish:
rect.Fill = finishBrush;
break;
case SpaceProbeModel.type.asteroid:
rect.Fill = asteroidBrush;
break;
case SpaceProbeModel.type.gravityInfluenced:
rect.Fill = gravityInfluencedBrush;
break;
case SpaceProbeModel.type.gravityWell:
rect.Fill = gravityWellBrush;
break;
case SpaceProbeModel.type.empty:
rect.Fill = emptySpaceBrush;
break;
case SpaceProbeModel.type.path:
rect.Fill = pathBrush;
break;
}
Canvas.SetTop(rect, row * cellSize);
Canvas.SetLeft(rect, column * cellSize);
canvas.Children.Add(rect);
}
}
}
}
}
和我的Program.cs
班级:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace SpaceProbe
{
class Program
{
[STAThread]
static void Main(string[] args)
{
SpaceProbeView view = new SpaceProbeView(30);
SpaceProbeModel model = new SpaceProbeModel(30, 20, 1);
model.Start = new SpaceProbeModel.CellPair(5, 5);
model.FindShortestPath(20, 25);
view.SetModel(model);
Application app = new Application();
app.Run(view.window);
}
}
}