UWP开发:自动生成迷宫&自动寻路算法(1)

(1)前端篇

首先,我们创建一个新的Universal Windows Platform程序。这些小方块是通过GridView来罗列的,这样可以避免MainPaga.xaml的<Rectangle>元素堆叠太多,并且也好处理数据。

现在我们来看MainPage.xaml到底应该怎么写。

 1 <Grid Background="Black">
 2         <StackPanel Orientation="Vertical">
 3             <GridView Width="1400" Height="730" x:Name="GridView" Margin="0,20,0,0">
 4                 <GridView.ItemTemplate>
 5                     <DataTemplate>
 6                         <Rectangle Width="50" Height="50" Fill="{Binding Color}"/>
 7                     </DataTemplate>
 8                 </GridView.ItemTemplate>
 9             </GridView>
10             <Button Height="40" Width="200" Content="寻路" Background="CadetBlue" HorizontalAlignment="Center" BorderBrush="CadetBlue" Click="ButtonBase_OnClick"/>
11         </StackPanel>
12     </Grid>
1 <Rectangle Width="50" Height="50" Fill="{Binding Color}"/>

就是地图的地砖啦~

1 GridView.ItemsSource = Rects;

这里的ItemSource我在C#的后端再进行绑定。

==========================

怎么做到寻路的时候更新地形呢?

首先我考虑到可不可以用ObservableCollection呢?

答案是:不可行!因为ObservableCollection<T>只关注个数的变化,不关注元素的修改。

那怎么做呢?

1 GridView.ItemsSource = null;
2 GridView.ItemsSource = Rects;

取消绑定再重新绑定就可以了!

原文地址:https://www.cnblogs.com/ldzhangyx/p/6099844.html