因此,對於最後一兩天我一直在研究this article以獲得一些暴露於依賴項屬性和路由命令,並希望利用一些示例代碼來解決另一個項目中的內容縮放問題。該項目恰好是用vb.net編寫的,而這個示例代碼是用C#編寫的。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
這將導致兩個投訴:
'completedEvent' 不是[命名空間] .AnimationHelper
「公共事件完成(的事件。 ..)'是一個事件,不能直接調用。使用RaiseEvent ...
(1)對我來說有點神祕,因爲completedEvent(作爲EventHandler)是方法聲明中的參數之一。從行頭刪除RaiseEvent並像普通方法那樣調用它似乎滿足了視覺工作室的要求,但我不知道它是否會在運行時工作,或者它是否有效。在(2)中,語法對我來說很可疑,將RaiseEvent添加到行頭會導致與(1)類似的類似投訴。
我打算繼續在vb.net的代理和事件中搜索堆棧和更好的引擎,因爲它顯然是我停止避免學習它們如何工作的時間。同時,建議/建議是最受歡迎的。