Skip to content

Instantly share code, notes, and snippets.

View rsingh85's full-sized avatar

Ravi Singh rsingh85

View GitHub Profile
private TextBlock CreateCellTextBlock(Cell cell)
{
TextBlock cellTextBlock = new TextBlock();
cellTextBlock.DataContext = cell;
cellTextBlock.InputBindings.Add(CreateMouseClickInputBinding(cell));
cellTextBlock.SetBinding(
TextBlock.BackgroundProperty,
CreateCellAliveBinding()
);
private Binding CreateCellAliveBinding()
{
return new Binding
{
Path = new PropertyPath("Alive"),
Mode = BindingMode.TwoWay,
Converter = new LifeToColourConverter(
aliveColour: Brushes.Black,
deadColour: Brushes.White
)
public class ObservableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string memberName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(
this,
public class Cell : ObservableBase
{
public int Row { get; private set; }
public int Column { get; private set; }
private bool alive;
public bool Alive
{
get { return alive; }
set
public class LifeToColourConverter : IValueConverter
{
public SolidColorBrush AliveColour { get; private set; }
public SolidColorBrush DeadColour { get; private set; }
public LifeToColourConverter(SolidColorBrush aliveColour,
SolidColorBrush deadColour)
{
AliveColour = aliveColour;
DeadColour = deadColour;
public RelayCommand<object> EvolveCommand { get; private set; }
public RelayCommand<object> ResetCommand { get; private set; }
public RelayCommand<string> ToggleCellLifeCommand { get; private set; }
<Button DockPanel.Dock="Top" Command="{Binding EvolveCommand}">Evolve</Button>
<Button DockPanel.Dock="Top" Command="{Binding ResetCommand}" Width="Auto">Reset</Button>
private InputBinding CreateMouseClickInputBinding(Cell cell)
{
InputBinding cellTextBlockInputBinding = new InputBinding(
generationViewModel.ToggleCellLifeCommand,
new MouseGesture(MouseAction.LeftClick)
);
cellTextBlockInputBinding.CommandParameter =
string.Format("{0},{1}", cell.Row, cell.Column);
return cellTextBlockInputBinding;
@rsingh85
rsingh85 / CustomAuthenticationHandlerTests.cs
Created May 25, 2023 09:49
Unit tests for a custom auth handler
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Xunit;
@rsingh85
rsingh85 / gist:1ec488740e900361398ac719c7f5a325
Created May 25, 2023 12:25
MyApplicationExceptionTests.cs
public class MyApplicationExceptionTests
{
[Fact]
public void Constructor_SerializationInfoAndStreamingContext_SetProperties()
{
// Arrange
var message = "Test exception message";
var innerException = new Exception("Inner exception message");
// Create SerializationInfo and StreamingContext objects