Using Reflection Api in Silverlight

Aucun commentaire

Recently i needed to get static method contained by a Class to display their visual result.
Indeed The idea was to display Robert Penner's Equation used by tweened. For doing such task you just have to use Reflection Api provided by Silverlight Framework that you can find in System.Reflection package. You will find the code I used in this article.

First it is the code for the UserControl TweenedEquationThumb :

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Tweened;
using System.Diagnostics;
 
namespace TweenEquationSlideShow
{
	public partial class TweenedEquationThumb : UserControl
	{
        public Func TweenEquation;
        double EndValue;
        double StartValue;
        double BaseValue;
        Tween showTween = new Tween();
 
        public TweenedEquationThumb(Func tweenEquation, double endValue, double startValue, double baseValue)
        {
                // Required to initialize variables
                InitializeComponent();
                TweenEquation = tweenEquation;
                StartValue = startValue;
                EndValue = endValue;
                BaseValue = baseValue;
                TweenDoubleDescription tdc = new TweenDoubleDescription(animPath,         TweenableProperties.Left, TweenEquation, 2, 180, 20);
                 tdc.Definition = 200;
                showTween.AddSequences(tdc);
                showTween.TweenCompleted += new Tween.StoryboardComplete(showTween_TweenCompleted);
                Loaded += new RoutedEventHandler(TweenedEquationThumb_Loaded);
 
        }
 
        void showTween_TweenCompleted(Tween sender, object freeReference)
        {
            sender.Stop();
        }
 
        void TweenedEquationThumb_Loaded(object sender, RoutedEventArgs e)
        {
            MouseLeftButtonDown += new MouseButtonEventHandler(TweenedEquationThumb_MouseLeftButtonDown);
            DrawCurve();
        }
 
        void TweenedEquationThumb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            showTween.Start();
        }
 
        private void DrawCurve()
        {
 
            double ecart = Math.Round((double)1/200, 3);
            Debug.WriteLine("ecart :: "+ecart);
 
            for (int i = 0; i < 200; i++)
            {
                double currentTime = ecart * i;
                double XTimeValue = currentTime * 180;
                double YValue = BaseValue + (double)Math.Round(this.TweenEquation(currentTime, StartValue, EndValue, 1) * 1000) / 1000;
 
                Ellipse bulle = new Ellipse();
                bulle.Width = 1.2;
                bulle.Height = 1.2;
                bulle.Fill = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0xff, 0xff) );
                Curve.Children.Add(bulle);
                Canvas.SetLeft(bulle, 20+XTimeValue);
                Canvas.SetTop(bulle, YValue);
 
                EquationName.Text = TweenEquation.Method.Name;
 
            }
        }
     }
}

And here the code for Page.xaml.cs :

namespace TweenEquationSlideShow
{
	public partial class Page : UserControl
	{
		public Page()
		{
			// Required to initialize variables
			InitializeComponent();
			Loaded += new RoutedEventHandler(Page_Loaded);
		}
 
		void Page_Loaded(object sender, RoutedEventArgs e)
		{
			//In first time I get the TweenedEquations' Type
			Type myTypeEq = typeof(Tweened.TweenedEquations);
 
			var b = true;
			//Then I get All methods contained with GetMethods() wich returns me an array of MethodInfo
			foreach (MemberInfo memberInfo in myTypeEq.GetMethods())
			{
				//If methods' name begin with "Ease" 
				if (memberInfo.Name.IndexOf("Ease", 0) != -1)
				{
					//then I get each method
					MethodInfo myMethod = myTypeEq.GetMethod(memberInfo.Name);
					//And i create a new delegate with returned MethodInfo Object
					var myD = Delegate.CreateDelegate(typeof (Func<double ,double,double,double,double>),myMethod);
					//In fact we get the reference of that method without calling her
					//I create a new instance of TweenedEquationThumb with a first argument taking my Delegate
                    			TweenedEquationThumb tetb = new TweenedEquationThumb((Func</double><double ,double,double,double,double>)myD, -55, 0, 65);
                    			//if you want you could invoke each method using Invoke method of MethodInfo object
                    			if (b) CurveIn.Children.Add(tetb); 
                    			else CurveOut.Children.Add(tetb); 
                    			b = !b
                		}
            		}
        	}
	}
}</double>
Bookmark and Share

Leave a Reply

Security Code: