patterncsharpModerate
English Draughts
Viewed 0 times
englishdraughtsstackoverflow
Problem
The basic UI for my checkers app is complete, so the next step is to come here for review. I have not built my Facade over my F# game library, so some of the type usages are a little strange; this will be done as soon as I get the library in a slightly more stable state.
MainPage.xaml
This is the opening view (and only view) of my app; it does not have any code-behind:
MainPageViewModel.cs is the VM for
`public class MainPageViewModel : INotifyPropertyChanged
{
private MainPage _page;
public MainPageViewModel(MainPage page)
{
_page = page;
GameController = new GameController.GameController(Board.defaultBoard, Types.Player.Black);
}
private string PlayerToString(Types.Player player) =>
player.IsWhite ? nameof(Types.Player.White) : nameof(Types.Player.Black);
private GameController.GameController _gameController;
public GameController.GameController GameController
{
get
{
return _gameController;
}
set
{
_gameController = value;
OnPropertyChanged();
OnPropertyChanged(nameof(Status));
}
}
private Types.Coord _selection;
public Types.Coord Selection
{
get
{
return _selection;
}
set
{
if (_selection == null)
{
_selection = value;
}
else if (_selection != null && PublicAPI.isValidMove(_selection, value, GameController))
{
GameController = PublicAPI.move(_selection, value, GameController).Value;
_selection = null;
}
else if (GameController.Board[value.Row][value.Column] == FSharpOption.None)
{
_selection = null;
}
else
{
_selection = value
MainPage.xaml
This is the opening view (and only view) of my app; it does not have any code-behind:
MainPageViewModel.cs is the VM for
MainPage:`public class MainPageViewModel : INotifyPropertyChanged
{
private MainPage _page;
public MainPageViewModel(MainPage page)
{
_page = page;
GameController = new GameController.GameController(Board.defaultBoard, Types.Player.Black);
}
private string PlayerToString(Types.Player player) =>
player.IsWhite ? nameof(Types.Player.White) : nameof(Types.Player.Black);
private GameController.GameController _gameController;
public GameController.GameController GameController
{
get
{
return _gameController;
}
set
{
_gameController = value;
OnPropertyChanged();
OnPropertyChanged(nameof(Status));
}
}
private Types.Coord _selection;
public Types.Coord Selection
{
get
{
return _selection;
}
set
{
if (_selection == null)
{
_selection = value;
}
else if (_selection != null && PublicAPI.isValidMove(_selection, value, GameController))
{
GameController = PublicAPI.move(_selection, value, GameController).Value;
_selection = null;
}
else if (GameController.Board[value.Row][value.Column] == FSharpOption.None)
{
_selection = null;
}
else
{
_selection = value
Solution
A couple of things:
Putting the
Add
You can add BindsTwoWayByDefault to EightPieceBoard.Selection but I don't mind being expliclit about it.
The viewmodel should not know about the view:
Same as above, don't add the grid as single child to the outer grid. This can hurts performance and is noisy to read. Better with a grid than a stackpanel though :)
...Putting the
StackPanel as single child of the grid is wasteful.Add
RowDefinitions to the grid. As a rule of thumb StackPanel is almost never right. After writing WPF for a couple of years you will probably find yourself using Grid ~95% of the time. Grid is a bit verbose but it is the panel you will not have to refactor away from. Also Grid makes reasoning about layout simpler. You can try this to bring down the verbosity a bit.You can add BindsTwoWayByDefault to EightPieceBoard.Selection but I don't mind being expliclit about it.
The viewmodel should not know about the view:
public MainPageViewModel(MainPage page) it makes testing harder and is smelly in general. Not a clean separation of responsibilites between view & viewmodel.
<Grid Name="BoardGrid"Same as above, don't add the grid as single child to the outer grid. This can hurts performance and is noisy to read. Better with a grid than a stackpanel though :)
Code Snippets
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
...<Grid>
<Grid Name="BoardGrid"Context
StackExchange Code Review Q#150105, answer score: 10
Revisions (0)
No revisions yet.