在类外传递和事件处理程序

时间:2016-01-06 06:07:59

标签: c# oop event-handling

这个问题与编程有关,也与我的架构有关,如果需要更改的话。

基本上,我将微软乐队功能包装在一个类中,我在类中有一个订阅加速度计变化的方法如下:

_bandClient.SensorManager.Accelerometer.ReadingChanged += Accelerometer_ReadingChanged;

我希望能够从类外部调用一个方法(事件处理程序),所以我这样做了:

public async Task ToggleAccelerometer(EventHandler<BandSensorReadingEventArgs<IBandAccelerometerReading>> del)
        {
            if (!_sensorState[Sensor.Accelerometer])
            {
                _sensorState[Sensor.Accelerometer] = true;
                _bandClient.SensorManager.Accelerometer.ReadingChanged += del;
                await _bandClient.SensorManager.Accelerometer.StartReadingsAsync();
            }
            else
            {
                _sensorState[Sensor.Accelerometer] = false;
                await _bandClient.SensorManager.Accelerometer.StopReadingsAsync();
            }
        }

参数del实现如下:

private void Accelerometer_ValuesChanged(object sender, BandSensorReadingEventArgs<IBandAccelerometerReading> e)
        {
            IBandAccelerometerReading accelerometer = e.SensorReading;
            Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            IBandAccelerometerReading accelerometer = e.SensorReading;
            AccelXBlock.Text = accelerometer.AccelerationX.ToString();
            AccelYBlock.Text = accelerometer.AccelerationY.ToString();
            AccelZBlock.Text = accelerometer.AccelerationZ.ToString();
        }).AsTask();
        }

这是按预期工作的,我的问题是:

我应该改变我的节目吗?     设计(是我试图做一个不好的做法?),如果是这样,那么更好的方法是什么?

1 个答案:

答案 0 :(得分:1)

理想情况下,您应该在包装类中进行事件冒泡。您可以使用event查看它如何实现发布者/订阅者模式。见下文:

// Wrapper
public class BandManager
{
    // Callers will subscribe to this event
    public event EventHandler<BandSensorReadingEventArgs<IBandAccelerometerReading>> ReadingChanged;

    public async Task ToggleAccelerometer()
    {
        if (!_sensorState[Sensor.Accelerometer])
        {
            _sensorState[Sensor.Accelerometer] = true;
            _bandClient.SensorManager.Accelerometer.ReadingChanged += OnReadingChanged;
            await _bandClient.SensorManager.Accelerometer.StartReadingsAsync();
        }
        else
        {
            _sensorState[Sensor.Accelerometer] = false;
            await _bandClient.SensorManager.Accelerometer.StopReadingsAsync();
        }
    }

    private void OnReadingChanged(object sender, BandSensorReadingEventArgs<IBandAccelerometerReading> e)
    {
        var readingChangedHandler = ReadingChanged;

        if(readingChangedHandler != null)
        {
            ReadingChanged(this, e);
        }
    }
}

// Client
public class MainClass
{
    public async void Test()
    {
        var bandManager = new BandManager();
        bandManager.ReadingChanged += BandManager_ReadingChanged;
        var whatever = await bandManager.ToggleAccelerometer();
    }

    // This is event handler for your wrapper class
    private void BandManager_ReadingChanged(object sender, BandSensorReadingEventArgs<IBandAccelerometerReading> e)
    {
        // use 'e' as you need
    }
}