コード例 #1
0
        /// <summary>
        /// Add an effect to the launchpad
        /// </summary>
        /// <param name="effect"></param>
        /// <param name="updateFrequency"></param>
        public void RegisterEffect(ILaunchpadEffect effect, TimeSpan updateFrequency)
        {
            try
            {
                // Register any observables being used
                CompositeDisposable effectDisposables = new CompositeDisposable();

                // If this effect needs the ability to change its frequency
                if (effect.WhenChangeUpdateFrequency != null)
                {
                    // Subscribe to the event to change the frequency and add it to this effects disposables
                    effectDisposables.Add(
                        effect
                        .WhenChangeUpdateFrequency
                        .Subscribe(newFrequency =>
                    {
                        // Change the frequency for this effect
                        OnChangeEffectUpdateFrequency(effect, newFrequency);
                    }));
                }

                // If this effect will notify us it needs to be unregistered
                if (effect.WhenComplete != null)
                {
                    effectDisposables.Add(
                        effect
                        .WhenComplete
                        .Subscribe(_ =>
                    {
                        // Unregister the effect and destroy its disposables
                        UnregisterEffect(effect);
                    }));
                }

                EffectsDisposables.Add(effect, effectDisposables);

                // Create an update timer at the specified frequency
                EffectsTimers.Add(effect, new Timer(state => effect.Update(), null, 0, (int)updateFrequency.TotalMilliseconds));

                // Initiate the effect (provide all buttons and button changed event
                effect.Initiate(this);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }