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,21 +1,24 @@
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
{ {
public event PropertyChangedEventHandler? PropertyChanged;
private const int RendererHertz = 5; private const int RendererHertz = 5;
private const int GameRendererHertz = (1 / RendererHertz) * 1000; private const int GameRendererHertz = (1 / RendererHertz) * 1000;
private readonly int _width = 480, _height = 640;
private readonly WriteableBitmap _writeableBitmap; private readonly WriteableBitmap _writeableBitmap;
public GameModel(int width, int height, Image image) public GameViewModel()
{ {
_writeableBitmap = BitmapFactory.New(width, height); _writeableBitmap = BitmapFactory.New(_width, _height);
image.Source = _writeableBitmap; // image.Source = _writeableBitmap;
var dispatcherRenderTimer = new DispatcherTimer var dispatcherRenderTimer = new DispatcherTimer
{ {
@ -34,6 +37,10 @@ public class GameModel
dispatcherUpdateTimer.Start(); dispatcherUpdateTimer.Start();
} }
public int Width => _width;
public int Height => _height;
public ImageSource Source => _writeableBitmap;
private void Render(object? sender, EventArgs eventArgs) private void Render(object? sender, EventArgs eventArgs)
{ {
_writeableBitmap.Lock(); _writeableBitmap.Lock();