_click事件发生后如何更改值? [英] How do I change a value after a _click event has taken place?

查看:100
本文介绍了_click事件发生后如何更改值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和物理库在UWP中创建一个小游戏。



经过一些相当痛苦的调试后,我发现一些代码已经落后了执行_Click事件后的场景。这个代码需要在我的事件代码完成之前触发。



换句话说,这就是我想要发生的事情:



1.用户点击按钮

2.一些代码通过_Click事件执行

3.此代码的最后部分从列表中获取一些属性,关于刚刚添加到列表中的对象。



现在发生的是:

1.用户点击按钮

2.一些代码通过_Click事件执行

3.代码的最后部分试图获取一些属性,但由于这些属性尚未在底层代码中修改,我的代码崩溃。



我在获取更新的对象之前等待几毫秒左右没有问题,所以我可以轻松地删除最终代码并将其放在一个方法中运行时间稍晚(我更喜欢这个挖掘底层代码库)。因此,我想知道是否有类似于Page_Loaded事件的事情发生在基础代码更新正确之后。



这是我的_Click代码。这里没什么奇怪的。问题是底层代码在关闭此方法后运行。我需要一种在底层代码运行完毕后运行更多代码的方法。

I am creating a small game in UWP using C# and a physics library.

After some rather painful debugging, I have discovered that some code has been run behind the scene after a _Click event has been executed. This code needs to fire before my event code has completed.

In other words, this is what I want to happen:

1. user clicks button
2. some code executes via the _Click event
3. the final parts of this code fetches some attributes from a list, regarding the object just added to the list.

What happens now is this:
1. user clicks button
2. some code executes via the _Click event
3. the final part of the code tries to fetch some attributes, but as these haven't been modified yet in the underlying code, my code crashes.

I have no problem waiting a few milliseconds or so before fetching the updated object, so I can easily cut out the final code and place it in a method that runs a little later in time (I'd prefer this to digging into the underlying code library). Thus, I wonder if there is a something similar to a Page_Loaded event, taking place AFTER the underlying code has been updated correctly.

Here is my _Click code. Nothing strange here really. The problem is that the underlying code runs AFTER the closing of this method. I need a way to run more code AFTER that underlying code has finished running.

private void btnAddBomber_Click(object sender, RoutedEventArgs e)
{
    ucBomber bomber = new ucBomber();
    AddObjectRandomPosition(bomber, 50, 1000, 120, 120, 174, 100);
}





我的尝试:



我试图找到合适的活动,但无济于事。我不知道如何搜索这个。



What I have tried:

I have tried to find a suitable event, but to no avail. I am not sure of how to search for this.

推荐答案

在事件处理程序完成后你无法运行代码,并且没有任何事情发生事件处理程序完成后自动 - 除非您为同一事件添加了额外的处理程序,在这种情况下它们将依次执行。因此,检查事件并确保您没有在事件列表中添加第二个或第三个处理程序。



要证明问题所在,请注释掉点击处理程序内容 - 我很确定你的问题会消失。如果没有,那么当您单击按钮并且需要查看其余代码时会发生这种情况,这是巧合。

如果它确实消失了,那么先看看这两个您从Click事件处理程序调用的方法: ucBomber 构造函数,以及 AddObjectRandomPosition 方法。

很可能它是第二个,它可能做的是通过添加对象导致重新绘制,并且油漆处理程序的某些部分导致您的问题。
You can't run code "after" the event handler finishes, and there isn't anything which occurs automatically once the event handler is finished - unless you have added extra handlers to the same event, in which case they will be executed in turn. So check the event and make sure that you haven't added a second or third handler to the event list.

To prove what the problem is, comment out the click handler content - and I'm pretty sure that your problem will disappear. If it doesn't, then it's a coincidence that this happens when you click the button and you need to look at the rest of your code.
If it does disappear, then start by looking at the two methods you call from the Click event handler: the ucBomber constructor, and the AddObjectRandomPosition method.
The chances are the it's the second one, and what it may be doing is causing a re-paint via the adding in of the object and some part of the paint handler is causing your problem.


这篇关于_click事件发生后如何更改值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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