如何在 WPF/C 中检索 Dispatcher.BeginInvoke 的值#

How retrieve value of a Dispatcher.BeginInvoke in WPF/C#

这是我在这个论坛上的第一个问题。我是一名法国学生,很抱歉我的英语不好。在我的代码中,我想从 Dispatcher.BeginInvoke. 中提取一个值,这是我的代码中有问题的部分。

public partial class Jouer : Window
{
    // private Timer timer_creer_obstacle;
    private Timer timer_personnage_chute;
    private Timer timer_obstacles_avance;
    private Personnage perso;
    private Decor decor;
    public Jouer()
    {
        InitializeComponent();
        perso = new Personnage("canard-de-bain.png", World);
        decor = new Decor(World);
        // Timer qui gère l'avancé des obstacles
        timer_obstacles_avance = new Timer();
        timer_obstacles_avance.Enabled = true;
        timer_obstacles_avance.Interval = 60;
        timer_obstacles_avance.Elapsed += timer_obstacles_avance_event;
        timer_obstacles_avance.AutoReset = true;
        // Timer qui gère la chute du personnage
        timer_personnage_chute = new Timer();
        timer_personnage_chute.Enabled = true;
        timer_personnage_chute.Interval = 60;
        timer_personnage_chute.Elapsed += timer_personnage_chute_event;
        timer_personnage_chute.AutoReset = true;
    }
    public void timer_personnage_chute_event(object sender, ElapsedEventArgs e)
    {
        perso.Dispatcher.BeginInvoke( // Dispatcher pour utiliser le multithearding
            DispatcherPriority.Normal,
            new Action(() =>
            {
                perso.Chuter();
                perso.WorlFarmeCollision();
            }));

这里是 WorldFarmeCollision 的代码 return 我是个傻瓜。

public bool WorlFarmeCollision()
{
    if (this.Y >= 350 || this.Y <= 0)
    {
        App.Current.Shutdown();
        return true;
    }
    else
        return false;
}

我想要 perso.WordFarmeCollision return 一个可以在我的 Jouer class.

中使用的值

感谢您的回答。

我正在用心做这件事,因为我目前没有设置 WPF 环境。不过,这应该会为您指明正确的方向...

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

    }


    private void button_Click(object sender, RoutedEventArgs e)
    {
        Perso perso = new Perso();

        Func<bool> getWorlFarmeCollisionAsyncDelegate = perso.WorlFarmeCollision;

        getWorlFarmeCollisionAsyncDelegate.BeginInvoke(
              (resultASync) =>
              {
                  var methodDelegate = (Func<bool>)resultASync.AsyncState;
                  perso.Dispatcher.BeginInvoke(
                  new Action(() => perso.BoolProperty = methodDelegate.EndInvoke(resultASync)));

              },
              getWorlFarmeCollisionAsyncDelegate);
    }
}

public class Perso
{
    public Dispatcher Dispatcher => Application.Current.Dispatcher;

    public bool BoolProperty { get; set; }

    public bool WorlFarmeCollision()
    {
        return true;
    }

}

BeginInvoke是异步执行的,所以你不能立即得到return值。

改为Invoke

public void timer_personnage_chute_event(object sender, ElapsedEventArgs e)
{
    bool result = perso.Dispatcher.Invoke( // Dispatcher pour utiliser le multithearding
        () =>
        {
            perso.Chuter();
            return perso.WorlFarmeCollision();
        }
        , DispatcherPriority.Normal);

参考:https://msdn.microsoft.com/en-us/library/hh199426(v=vs.110).aspx