在WPF中添加eventhandler(compositiontarget.rendering) [英] Adding an eventhandler (compositiontarget.rendering) in WPF

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

问题描述

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



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



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



第三类,然后从GameController的GameLoop方法调用BoardSetup的DrawGame方法,并重绘画布上的对象。 (这适用于DispatcherTimer,但不适用。)我被建议使用CompositionTarget.Rendering( c# - 如何刷新2D WPF游戏中最智能的UI? - Stack Overflow [ ^ ])但不知道如何添加它。



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



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



请帮忙!



我尝试了什么:



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

解决案
其实,从CompositionTarget.Rendering事件打电话给我gameloop的伎俩。我现在已成功尝试了30个动态对象以及300个动态对象。如果我有300个移动物体,物体移动不顺畅,但时间准确:我已经运行游戏四分钟(确切地说,时间是正确的到第二个(至少 - 我用手机的计时器确认) :))。



我希望在几周内写一篇关于我的游戏引擎的文章。如果有人现在需要我的代码,那么只需PM我左右。


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).

解决方案

Actually, calling my gameloop from the CompositionTarget.Rendering event does the trick. I have now successfully tried with 30 dynamic objects as well as 300 dynamic objects. The objects are not moving smoothly if I have 300 moving objects, but the time is accurate: I have run the game for four minutes (exactly, and the time is correct to the second (at least - I'm confirming with my cellphone's timer :) ).

I will write an article about my game engine within a few weeks hopefully. If anyone needs my code now, then just PM me or so.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆