почему не обновляется Binding Turn при совершении хода в моей программе? вот код:
1)
<ContentPage.BindingContext>
<vm:ViewM/>
</ContentPage.BindingContext>
<AbsoluteLayout>
<Frame BackgroundColor="#2196F3" Padding="12" CornerRadius="0"
AbsoluteLayout.LayoutBounds="0, 0, 1, 0.14"
AbsoluteLayout.LayoutFlags="All"
>
<Label Text="TicTacToe" TextColor="White" FontSize="36"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
/>
</Frame>
<Grid HorizontalOptions="Center"
AbsoluteLayout.LayoutBounds="0.5, 0.15"
AbsoluteLayout.LayoutFlags="PositionProportional">
<Label Text="X:" FontSize="36" Grid.Column="0" HorizontalTextAlignment="Center"/>
<Label Text="{Binding Xwins}" FontSize="36" Grid.Column="1" HorizontalTextAlignment="Center"/>
<Label Text="O:" FontSize="36" Grid.Column="3" HorizontalTextAlignment="Center"/>
<Label Text="{Binding Owins}" FontSize="36" Grid.Column="4" HorizontalTextAlignment="Center"/>
</Grid>
<Grid HorizontalOptions="Center"
AbsoluteLayout.LayoutBounds="0.5, 0.23"
AbsoluteLayout.LayoutFlags="PositionProportional">
<Label Text="Turn:" FontSize="36" Grid.Column="0" HorizontalTextAlignment="Center"/>
<Label Text="{Binding Turn}" FontSize="36" Grid.Column="1" HorizontalTextAlignment="Center"/>
</Grid>
<Grid x:Name="field"
AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 0.4"
AbsoluteLayout.LayoutFlags="All">
</Grid>
<Grid AbsoluteLayout.LayoutBounds="0.5, 0.9, 1, 0.1"
AbsoluteLayout.LayoutFlags="All">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Button Text="-" Grid.Column="0"
Command="{Binding MinusSize}"/>
<Label Text="{Binding fieldSize}" FontSize="42" Grid.Column="1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
<Button Text="+" Grid.Column="2"
Command="{Binding PlusSize}"/>
</Grid>
<Button Text="New game" FontSize="36"
AbsoluteLayout.LayoutBounds="0.5, 1, 1, 0.1"
AbsoluteLayout.LayoutFlags="All"
Command="{Binding StartGame}"
Clicked="ReDraw"/>
</AbsoluteLayout>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace TicTac
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
InitializeGameField();
}
private void InitializeGameField()
{
var viewModel = BindingContext as ViewM;
if (viewModel != null)
{
field.Children.Clear();
field.RowDefinitions.Clear();
field.ColumnDefinitions.Clear();
for (int row = 0; row < viewModel.fieldSize; row++)
{
for (int col = 0; col < viewModel.fieldSize; col++)
{
var button = new Button();
int index = row * viewModel.fieldSize + col;
button.SetBinding(Button.TextProperty, new Binding($"board[{index}]"));
button.Command = new Command(() =>
{
if (viewModel._game.Move((byte)index))
{
// Обновите кнопку
button.Text = viewModel.board[index].ToString();
}
});
field.Children.Add(button, col, row);
}
}
}
}
private void ReDraw(object sender, EventArgs e)
{
InitializeGameField();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
namespace TicTac
{
public class ViewM : INotifyPropertyChanged
{
internal Game _game;
public byte fieldSize { get; private set; } = 3;
public ViewM()
{
_game = new Game(fieldSize);
// Команды для управления размерами (эти команды у вас уже упомянуты в View, но не реализованы)
MinusSize = new Command(DecreaseFieldSize);
PlusSize = new Command(IncreaseFieldSize);
StartGame = new Command(StartNewGame);
}
public char[] board => _game.board;
public string Turn
{
get
{
return _game.Xturn ? "X" : "O";
}
}
public ICommand MinusSize { get; }
public ICommand PlusSize { get; }
public ICommand StartGame { get; }
private void DecreaseFieldSize()
{
fieldSize--;
OnPropertyChanged(nameof(fieldSize));
}
private void IncreaseFieldSize()
{
fieldSize++;
OnPropertyChanged(nameof(fieldSize));
}
private void StartNewGame()
{
_game = new Game(fieldSize);
OnPropertyChanged(nameof(Turn));
}
// Реализация INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Text;
namespace TicTac
{
internal class Game : INotifyPropertyChanged
{
public char[] board { get; private set; }
public bool Xturn { get; private set; } = true;
private byte size;
bool isWin= false;
public Game(byte fieldSize) {
size = fieldSize;
Xturn = true; //передача хода крестикам
board = new char[fieldSize*fieldSize];
for (int i = 0; i < (fieldSize*fieldSize); i++)
{
board[i] = 'N'; //заполнение поля "null'ом"
}
}
public bool Move(byte cell) { //ход
if (board[cell] == 'N') //проверка клетки на свободность
{
board[cell] = Xturn ? 'X' : 'O'; //занятие ячейки в зависимости от того чей ход
Xturn = !Xturn; //передача хода
OnPropertyChanged("Turn");
return true; //ход произошёл
}
else { return false; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}