Archived
1
0
Fork 0

Change to binding element

This commit is contained in:
Ziedelth 2022-04-07 15:30:48 +02:00
parent 39bc524922
commit f238c1a720
3 changed files with 58 additions and 51 deletions

View file

@ -4,8 +4,8 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" mc:Ignorable="d"
Title="GameWindow" Height="450" Width="800"> Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}">
<Grid> <Grid>
<Image x:Name="ImageControl" /> <Image Source="{Binding Source}" />
</Grid> </Grid>
</Window> </Window>

View file

@ -10,7 +10,7 @@ public partial class GameWindow : Window
{ {
AttachConsole(-1); AttachConsole(-1);
InitializeComponent(); InitializeComponent();
new GameModel((int)Width, (int)Height, ImageControl); DataContext = new GameViewModel();
} }
[DllImport("kernel32.dll")] [DllImport("kernel32.dll")]

View file

@ -1,49 +1,56 @@
using System; using System;
using System.Windows.Controls; using System.ComponentModel;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Threading; using System.Windows.Threading;
namespace Tetris.ViewsModels; namespace Tetris.ViewsModels;
public class GameModel public class GameViewModel : INotifyPropertyChanged
{ {
private const int RendererHertz = 5; public event PropertyChangedEventHandler? PropertyChanged;
private const int GameRendererHertz = (1 / RendererHertz) * 1000;
private readonly WriteableBitmap _writeableBitmap; private const int RendererHertz = 5;
private const int GameRendererHertz = (1 / RendererHertz) * 1000;
public GameModel(int width, int height, Image image) private readonly int _width = 480, _height = 640;
{ private readonly WriteableBitmap _writeableBitmap;
_writeableBitmap = BitmapFactory.New(width, height);
image.Source = _writeableBitmap; public GameViewModel()
{
var dispatcherRenderTimer = new DispatcherTimer _writeableBitmap = BitmapFactory.New(_width, _height);
{ // image.Source = _writeableBitmap;
Interval = new TimeSpan(0, 0, 0, 0, GameRendererHertz)
}; var dispatcherRenderTimer = new DispatcherTimer
{
dispatcherRenderTimer.Tick += Render; Interval = new TimeSpan(0, 0, 0, 0, GameRendererHertz)
dispatcherRenderTimer.Start(); };
var dispatcherUpdateTimer = new DispatcherTimer dispatcherRenderTimer.Tick += Render;
{ dispatcherRenderTimer.Start();
Interval = new TimeSpan(0, 0, 0, 0, 25)
}; var dispatcherUpdateTimer = new DispatcherTimer
{
dispatcherUpdateTimer.Tick += Update; Interval = new TimeSpan(0, 0, 0, 0, 25)
dispatcherUpdateTimer.Start(); };
}
dispatcherUpdateTimer.Tick += Update;
private void Render(object? sender, EventArgs eventArgs) dispatcherUpdateTimer.Start();
{ }
_writeableBitmap.Lock();
_writeableBitmap.Clear(Colors.Black); public int Width => _width;
public int Height => _height;
_writeableBitmap.Unlock(); public ImageSource Source => _writeableBitmap;
}
private void Render(object? sender, EventArgs eventArgs)
private void Update(object? sender, EventArgs eventArgs) {
{ _writeableBitmap.Lock();
_writeableBitmap.Clear(Colors.Black);
}
_writeableBitmap.Unlock();
}
private void Update(object? sender, EventArgs eventArgs)
{
}
} }