从SizeChanged事件控制荒谬的大小

时间:2013-01-24 11:14:34

标签: c# silverlight

我有我的视频播放器。只要加载它就会被隐藏(应用于对象的1对1像素大小)。完成加载后,它会触发JavaScript函数并调整大小到所需大小(640 x 480px即)。实际上SizeChanged事件被触发。在该事件中,处理器媒体元素被调整大小以保持适当的宽高比。

问题是我不时会看到荒谬的大小:ActualWidth == 448900, ActualHeight == 286090

任何人都遇到了至少与此相似的麻烦?我无法在我的测试环境中重现它,所以我不知道它是如何,何时以及为什么会发生。任何建议都可能有所帮助。

SizeChanged handler:

private void Player_SizeChanged(object sender, SizeChangedEventArgs e)
{
    Size pSize = e.NewSize;
    if (_controlPanel.Visibility == System.Windows.Visibility.Visible)
        pSize = new Size(e.NewSize.Width, e.NewSize.Height - _controlPanel.PanelRoot.Height);

    _videoPanel.OnParentSizeChanged(pSize);
}

VideoPanel.OnParentSizeChanged:

public void OnParentSizeChanged(Size newSize)
{
    //nothing to do here
    if (_lastLayoutSize != null &&
        _lastLayoutSize.Width == newSize.Width &&
        _lastLayoutSize.Height == newSize.Height)
        return;

    _lastLayoutSize = new Size(newSize.Width, newSize.Height);
    ApplyAspectRatio();
}

VideoPanel.ApplyAspectRatio:

public void ApplyAspectRatio()
{
    //security checks
    if (_lastLayoutSize == null)
        return;

    if (_mediaSource == null)
    {
        if (_imgThumbnail != null)
        {
            this.Width = _lastLayoutSize.Width;
            this.Height = _lastLayoutSize.Height;
        }

        return;
    }

    if (_mediaSource.Width == 0 || _mediaSource.Height == 0)
        return;

    double dScale = 1.0;
    double dSourceHeight = 1.0;
    double dSourceWidth = 1.0;

    lock (_msSync)
    {
        dSourceWidth = _mediaSource.Width;
        dSourceHeight = _mediaSource.Height;

        double dHeightRatio = 1.0;
        double dWidthRatio = 1.0;

        dHeightRatio = _lastLayoutSize.Height / dSourceHeight;
        dWidthRatio = _lastLayoutSize.Width / dSourceWidth;

        dScale = System.Math.Min(dHeightRatio, dWidthRatio);
    }

    double nWidth = dSourceWidth * dScale;
    double nHeight = dSourceHeight * dScale;

    Width = MaxWidth = MinWidth = nWidth;
    Height = MaxHeight = MinHeight = nHeight;

    this.InvalidateArrange();
}

我删除了用于记录的代码。它只是将一些文本写入隔离存储。那些荒谬的值出现在该调用链的顶部,即Player_SizeChanged方法。

0 个答案:

没有答案