0

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

问题是我不时得到荒谬的大小,例如:ActualWidth == 448900, ActualHeight == 286090

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

SizeChanged 处理程序:

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 方法的顶部。

4

0 回答 0