我有一个容器层次结构,其中包括一个朝向顶部的滚动查看器,因此我可以看到我的整个表单。在里面,我可以一个捕获元素,它在启动时以640 * 480分辨率连接到网络摄像头。
我看到的行为,我不想并且似乎无法阻止,是捕获元素尝试采用与图像分辨率相同的渲染大小 640 * 480,即使它设置为拉伸也是如此。因此,它会将所有容器推出到带有滚动查看器的容器。我希望它的行为像其他控件一样,如按钮等,它只采用可用的内部大小,而滚动查看器只有在真正需要时才参与。
下面的代码演示了这一切,但注释/取消注释以查看按钮而不是 CaptureElement:
Inherits Page
Private mc As New MediaCapture
Private ce As New CaptureElement
Private WithEvents TopGrid As New Grid
Private vs As New ScrollViewer
Private WithEvents CamGrid As New Grid
Private but As New Button
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Content = TopGrid
TopGrid.Width = 600
TopGrid.Height = 200
TopGrid.Children.Add(vs)
vs.Content = CamGrid
vs.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible
vs.VerticalScrollBarVisibility = ScrollBarVisibility.Visible
but.Content = "hello"
but.HorizontalAlignment = HorizontalAlignment.Stretch
but.VerticalAlignment = VerticalAlignment.Stretch
ce.HorizontalAlignment = HorizontalAlignment.Stretch
but.VerticalAlignment = VerticalAlignment.Stretch
InitCam()
CamGrid.Children.Add(ce)
'CamGrid.Children.Add(but)
End Sub
Private Async Sub InitCam()
Await mc.InitializeAsync
ce.Source = mc
Dim t = mc.StartPreviewAsync
End Sub
Private Sub CamGrid_SizeChanged(sender As Object, e As SizeChangedEventArgs) Handles CamGrid.SizeChanged
Dim w = e.NewSize.Width
Dim h = e.NewSize.Height
Dim a = 1
End Sub
我找到了答案。在更高级别滚动查看器中对要管理其大小的控件使用画布。Canvas 不会扩展以包含其子项,因此不会导致冲突/递归。