我正在使用WPF(C#)中的應用程序,它將使用網絡攝像頭,但我遇到了與主窗口有關的問題。當我在Windows 7中運行它時,我獲得了期望的外觀,但是當我在Windows 8.1機器上運行它時,它看起來不同。在Windows 8.1和Windows 7 WPF窗口不同
這是所需的外觀(Windows 7)中
這就是它看起來像在Windows 8.1(最大化時,它不會延伸到充滿整個屏幕)
這是我的代碼mainwindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cam="clr-namespace:WebcamControl;assembly=WebcamControl"
Title="WPF Webcam" Height="664.333" Width="645">
<Window.Resources>
<DataTemplate x:Key="DevicesListTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</Window.Resources>
<Grid Margin="0,0,0,0">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<cam:Webcam Name="WebcamCtrl" Margin="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<StackPanel Margin="0,0,0,0" Orientation="Horizontal" Grid.Row="1">
<ComboBox HorizontalAlignment="Left" Height="23" Width="200" VerticalAlignment="Center"
x:Name="AudioDevicesComboBox" ItemTemplate="{StaticResource DevicesListTemplate}" Margin="10,0,10,0"/>
<ComboBox Height="22" Width="200" HorizontalAlignment="Right" VerticalAlignment="Center"
x:Name="VideoDevicesComboBox" ItemTemplate="{StaticResource DevicesListTemplate}" Margin="0,0,10,0"/>
<TextBox x:Name="tbScan" HorizontalAlignment="Left" Height="22" Margin="0,0,10,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="100" KeyDown="tbScan_KeyDown"/>
<Button Content="Start" HorizontalAlignment="Left" Height="22" Margin="0,0,10,0" VerticalAlignment="Center" Width="78" Click="Button_Click_1"/>
</StackPanel>
</Grid>
這是從攝像頭用戶控制
<UserControl x:Class="Webcam"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
MinHeight="100" MinWidth="100"
mc:Ignorable="d">
<Grid>
<WindowsFormsHost Name="WinFormsHost" Margin="0" Background="{x:Null}">
<wf:Panel x:Name="WebcamPanel"/>
</WindowsFormsHost>
</Grid>
我還發現在Webcam.dll源代碼此事件的XAML。
Private Sub Webcam_SizeChanged(sender As Object, e As SizeChangedEventArgs) Handles Me.SizeChanged
If (deviceSource IsNot Nothing) Then
deviceSource.PreviewWindow.SetSize(New Size(CInt(Me.ActualWidth), CInt(Me.ActualHeight)))
End If
End Sub
任何幫助將不勝感激!
我在主窗口中的「cam:Webcam ..」項目旁添加了一個邊框,並且它確實拉伸,所以我認爲它必須是攝像頭用戶控件中不允許拉伸的東西。 – mattc19 2015-02-24 02:57:27
如果沒有_minimal_,_complete_代碼示例,任何人都很難提供具體的建議。例如。一個用純WPF代碼顯示Win7和Win8的區別,而不是使用網絡照相庫或硬件。這就是說,你是否嘗試過在用戶控件上使用'Stretch'屬性? – 2015-02-24 04:43:57
您是否嘗試將其包裝在Viewbox中? – 2015-02-24 08:36:15