本文介绍了在WPF中添加eventhandler(compositiontarget.rendering)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

排序新手问题,但是这里有...



我想在我的WPF 2D游戏中使用CompositionTarget.Rendering来更新窗口。但是我对事件处理程序等感到厌烦,所以当我尝试这个并且需要一些帮助时没有任何反应。



我有我的MainWindow和一个画布在那里的行动。单击播放按钮后,游戏开始播放。 Button的click事件(在MainWindow.xaml.cs中)调用GameController.cs来处理游戏逻辑(比如游戏循环)。



第三类,然后从GameController的GameLoop方法调用BoardSetup的DrawGame方法,并重绘画布上的对象。 (这适用于DispatcherTimer,但不适用。)我被建议使用CompositionTarget.Rendering( [])但不知道如何添加它。



那么我应该如何以及在哪里(什么文件和方法(如果有的话))添加CompositionTarget.Rendering事件处理程序以使我的DrawGame方法重绘游戏?



实际上我并不关心游戏渲染的频率,只要每秒至少说12次。对我来说重要的是游戏同样运行速度无论游戏中有多少个对象(今天游戏循环就是这样做的,只是结果永远不会在屏幕上更新)。因此,我将丢弃我的DispatcherTimer来使用CompositionTarget.Rendering。



请帮忙!



我尝试了什么:



我试过在MainWindow.XAML.cs和游戏循环中使用CompositionTarget.Rendering但是UI仍然没有重绘。我也尝试了myCanvas.UpdateLayout()但它没有在屏幕上绘制任何东西(因为我猜运行的游戏循环)。

解决案


Hi, Sort of a newbie question, but here goes...

I am trying to use CompositionTarget.Rendering in my WPF 2D game to update the window. However I suck at event handlers etc so nothing happens when I try this and need some help.

I have my MainWindow and a canvas in there where the action is. The game starts when I click a Play button. The Button's click event (in MainWindow.xaml.cs) calls GameController.cs which takes care of the game logic (like the game loop).

A third class, BoardSetup's, DrawGame method is then called from the GameController's GameLoop method and redraws the objects on the canvas. (This works with a DispatcherTimer but not otherwise.) I was advised to use CompositionTarget.Rendering (c# - How do I refresh the UI smartest in a 2D WPF game? - Stack Overflow[^]) but don't know how to add it.

So how and where (what file and method (if any) should I add the CompositionTarget.Rendering event handler to make my DrawGame method redraw the game?

Actually I don't care of exactly how often the game renders as long as it's at least say 12 times per second. What's important to me is that the game runs at the same speed regardless of how many objects are in the game (and the gameloop does that today, it's just that the result is never updated onscreen). Therefore I'm throwing out my DispatcherTimer to use CompositionTarget.Rendering instead.

Please help!

What I have tried:

I've tried to use CompositionTarget.Rendering in MainWindow.XAML.cs and in the game loop but the UI is still not redrawn. I have also simply tried myCanvas.UpdateLayout() but it doesn't draw anything onscreen (because of the running gameloop I guess).

解决方案


这篇关于在WPF中添加eventhandler(compositiontarget.rendering)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 16:24