簡體   English   中英

困難保存方法從C#轉換后提高與代表事件

[英]Difficulty with preserving methods raising events with delegates after conversion from C#

因此,在過去的一兩天里,我一直在研究本文 ,以期深入了解依賴項屬性和路由命令,並希望利用一些示例代碼來解決另一個項目中的內容擴展問題。 該項目恰好是用vb.net編寫的,而此示例代碼是C#。

好的沒問題。 我見過的大多數教程和演示項目都使用C#,並且我發現在vb.net中閱讀代碼並編寫等效代碼是了解實際情況並使他們更舒適地使用的一種很好的方法。 這很耗時,但以我的經驗水平來說是值得的(#00FF00)

不久之后,我就遇到了回調方法事件的問題。 考慮以下方法:

public static class AnimationHelper
{

...

public static void StartAnimation(UIElement animatableElement,
                                  DependencyProperty dependencyProperty,
                                  double toValue,
                                  double animationDurationSeconds,
                                  EventHandler completedEvent)
{
    double fromValue = (double)animatableElement.GetValue(dependencyProperty);

    DoubleAnimation animation = new DoubleAnimation();
    animation.From = fromValue;
    animation.To = toValue;
    animation.Duration = TimeSpan.FromSeconds(animationDurationSeconds);

    animation.Completed += delegate(object sender, EventArgs e)
    {
        //
        // When the animation has completed bake final value of the animation
        // into the property.
        //
        animatableElement.SetValue(dependencyProperty, animatableElement.GetValue(dependencyProperty));
        CancelAnimation(animatableElement, dependencyProperty);

        if (completedEvent != null)
        {
            completedEvent(sender, e);
        }
    };

    animation.Freeze();

    animatableElement.BeginAnimation(dependencyProperty, animation);
}

抄寫這個方法vb.net是除了妥善處理DoubleAnimation是的Completed事件和回調方法的方式簡單。 我的最佳嘗試如下所示:

Public NotInheritable Class AnimationHelper

...

Public Shared Sub StartAnimation(...)

...

animation.Completed += Function(sender As Object, e As EventArgs)

            animatableElement.SetValue(dependencyProperty, animatableElement.GetValue(dependencyProperty))
            CancelAnimation(animatableElement, dependencyProperty)
            RaiseEvent completedEvent(sender, e)
        End Function
...

End Sub

這導致了兩個投訴:

  1. 'completedEvent'不是[namespace] .AnimationHelper的事件

  2. “公共事件已完成(...)”是事件,不能直接調用。 使用RaiseEvent ...

(1)是一個謎給我因為completedEvent(如事件處理程序)是在該方法中聲明的參數之一。 從行的開頭刪除RaiseEvent並像普通方法一樣調用它似乎可以滿足Visual Studio的要求,但是我不認為這在運行時是否可行,或者這樣做是否有效。 (2)語法看起來可疑對我來說,增加的RaiseEvent到行頭引起類似(1)類似的投訴。

我將繼續在vb.net上搜索堆棧和更大的Internet,以獲取有關委托和事件的良好入門,因為顯然是時候我停止避免學習它們的工作原理了。 在此期間,我們非常歡迎您提出建議/建議。

  1. 您為什么嘗試提高該事件處理程序? EventHandler只是一個委托。 只需按照C#執行即可:

     if completedEvent IsNot Nothing then completedEvent(sender, e) end if 

我相信這里的主要問題是,您沒有使用AddHandler以VB方式添加處理程序。 如果將代碼重寫為:

Dim handler As EventHandler = Sub(sender, e)
    x.SetValue(dependencyProperty, x.GetValue(dependencyProperty))
    CancelAnimation(x, dependencyProperty)
    ' See note below...
    RaiseEvent completedEvent(sender, e)
End Sub

AddHandler animation.Completed, handler

...我相信它會起作用。 目前尚不清楚RaiseEvent部分是否仍然會引起問題,但至少要訂閱animation.Completed應該沒問題。 如果RaiseEvent部分引起問題,請按照Daniel的回答直接調用委托。

注意這里使用Sub而不是Function ,因為委托不返回任何東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM