开发者

Best approach for XAML game

I am developing a board game with a grid. Every grid has some options for the end user. The idea is that an options menu will be displayed under the grid when the user clicks a grid cell. My XAML looks like:

Mainpage.xaml:

<Grid>
     <Grid.RowDefinitions>
         <RowDefinition Height="450"/>
         <RowDefinition Height="50"/>
     </Grid.RowDefinitions>
     <game:Board Grid.Row="0"/> 
     <game:Chooser Grid.Row="1" d:IsHidden="False"/>
</Grid>

The Board is a grid which contains a square object in each cell. Each square object does have MouseLeftButtonDown listener. When clicking the square object, the chooser menu must open and some pa开发者_Go百科rameters must be passed to this object.

What would be the best approach to do this as the chooser is a member of another class? Do I need to pass the chooser object (which is in MainPage.xaml) to the board and square object? Or is there a way to propagate the event to the chooser?

Cheers Tom


I would do three things:

  • First, have each board tile all raise the same custom RoutedEvent (called something like "Chosen, which has been defined on a base class "Tile").
  • Second, have the Board listen for the Tile.Chosen event. Once the board sees the event, have it set a new property "ChosenTile"/"ChosenTileInfo"/or whatever it is you need.
  • Lastly, create a DependencyObject on the Chooser control (it would be called something like "Target"), so that you could bind it to ChosenTile.

The code above would end up looking something like:

<Grid>
     <Grid.RowDefinitions>
         <RowDefinition Height="450"/>
         <RowDefinition Height="50"/>
     </Grid.RowDefinitions>
     <game:Board Grid.Row="0" x:Name="MyBoard"/> 
     <game:Chooser Grid.Row="1" Target="{Binding ChosenTile, ElementName=MyBoard}"/>
</Grid>

The other option is to still do the first part, but instead of creating a DependencyObject, just raise your own event and do everything else in code-behind.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜