gpt4 book ai didi

c# - 为什么没有设置 ScrollOwner?

转载 作者:行者123 更新时间:2023-11-30 16:54:27 25 4
gpt4 key购买 nike

我开发测试驱动并想要测试,我实现 IScrollInfo 的控件是否返回正确的 ScrollViewer。此外,我想测试我的控件是否在 ScrollOwner 上调用了 InvalidateScrollInfo

控件看起来像:

private class MyControl : UserControl, IScrollInfo
{
public ScrollViewer ScrollOwner { get; set; }

... other code to implement IScrollInfo
}

我的测试(nunit)看起来像:

[Test]
public void Should_be_scrollable()
{
var ControlToTest = new MyControl();
var scrollViewer = new ScrollViewer { Content = ControlToTest };
var window = new Window { Content = scrollViewer };
window.Show();

Assert.That(ControlToTest, Is.InstanceOf<IScrollInfo>());
Assert.That(ControlToTest.ScrollOwner, Is.SameAs(scrollViewer));
}

但不幸的是,Assert.That(ControlToTest.ScrollOwner, Is.SameAs(scrollViewer)); 失败了,因为 ScrollOwnernull

问题 为什么 ControlToTest.ScrollOwner 为空?

我做了什么:我查看了设置 ScrollOwner 的位置。这发生在 ScrollContentPresenter 中的 HookupScrollingComponents 中,后者又在 OnApplyTemplate 中调用。据我了解,在测量元素时会应用模板。这应该在显示窗口时完成。即使我添加 window.Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.Render); 形成:

[Test]
public void Should_be_scrollable()
{
var scrollViewer = new ScrollViewer { Content = ControlToTest };
var window = new Window { Content = scrollViewer };
window.Show();
window.Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.Render);

Assert.That(ControlToTest, Is.InstanceOf<IScrollInfo>());
Assert.That(ControlToTest.ScrollOwner, Is.SameAs(scrollViewer));
}

它没有帮助。 ScrollOwner 仍然是 null。为什么?

奖励问题:如何测试在 scrollViewer 上调用了 InvalidateScrollInfo()

最佳答案

CanContentScroll 必须设置为 true

比测试有效:

[Test]
public void Should_be_scrollable()
{
var ControlToTest = new MyControl();
var scrollViewer = new ScrollViewer
{
Content = ControlToTest,
CanContentScroll = true
};
var window = new Window { Content = scrollViewer };
window.Show();

Assert.That(ControlToTest, Is.InstanceOf<IScrollInfo>());
Assert.That(ControlToTest.ScrollOwner, Is.SameAs(scrollViewer));
}

现在可以使用 ControlToTest.ScrollOwner 进行进一步测试。

奖励问题的答案:当您将 ControlToTest.ViewportHeight 更改为小于测试中的 ControlToTest.ExtentHeight 时,您可以检查 ControlToTest .ScrollOwner.ComputedVerticalScrollBarVisibility 属性断言调用了 ScrollOwner.InvalidateScrollInfo()。这可能看起来像:

var scrollViewer = new ScrollViewer
{
Content = ControlToTest,
CanContentScroll = true,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto
};
var window = new Window { Content = scrollViewer, Height = 600 };
window.Show();
Assert.That(
ControlToTest.ScrollOwner.ComputedVerticalScrollBarVisibility,
Is.EqualTo(Visibility.Collapsed));

window.Height = 300;
Render();

Assert.That(
ControlToTest.ScrollOwner.ComputedVerticalScrollBarVisibility,
Is.EqualTo(Visibility.Visible));

private void Render()
{
ControlToTest.Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle);
}

关于c# - 为什么没有设置 ScrollOwner?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30501149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com