我尝试使用RX写一个类来管理动画序列。不幸的是,我发现它不起作用,当我调试它时,我发现我总是得到_sequence的值是null;似乎No result is返回。我怀疑 AnimationSequence() 方法有问题。但我不确定,任何专家可以帮助我吗?
public class AnimationManager : IDisposable
{
private IDisposable _sequence;
private ISequentialResult _result;
private readonly List<Func<IObserver<ISequentialResult>, ISequentialResult, ISequentialResult>> _animationQueue
= new List<Func<IObserver<ISequentialResult>, ISequentialResult, ISequentialResult>>();
public void AddAnimation(Func<IObserver<ISequentialResult>, ISequentialResult, ISequentialResult> sequence)
{
_animationQueue.Add(sequence);
}
public void Run(Action<Exception> exception, Action<ISequentialResult> completed)
{
var sequence = AnimationSequence().ToObservable();
_sequence = sequence.Subscribe(
next => { },
exception,
() => completed(_result));
}
private IEnumerable<IObservable<Object>> AnimationSequence()
{
var x = 0;
_result = new DefaultSeqResult(null) { Value = this };
while (x < _animationQueue.Count)
{
var sequence = _animationQueue[x];
var step = Observable.Create<ISequentialResult>(
observer =>
{
var newResult = sequence(observer, _result);
newResult.Results.Add(_result);
newResult.Results.AddRange(_result.Results);
_result = newResult;
return () => { };
}
);
yield return step;
x++;
}
_result.Results.Add(_result);
}
public void Dispose()
{
if (_sequence != null) _sequence.Dispose();
}
}