要求:要有两个嵌入式.Net WebBrowser控件,每个控件都使用不同的IE会话。服务器页面设置用户的会话。它存储在内存中并在多个WebBrowser控件之间共享(就像标签一样)
在标准IE中处理此方法的方法是选择文件>新会议。这会在不同的进程中打开一个新的IE窗口,因此它们不会在内存中共享相同的会话
我唯一能做的就是结束第一个WebBrowser会话,因此在第二个WebBrowser中启动一个新会话。但是这意味着我失去了第一个会话,要求告诉我我不应该
以下列方式结束浏览器会话
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
有没有人知道WebBrowser控件如何在不同的会话/进程中被隔离?
答案 0 :(得分:0)
使用当前WebBrowser
方式无法实现。您在WebBrowser
的所有实例中共享一个会话。
我不知道是否有其他支持多个活动连接的网络浏览器控件(至少CefSharp doesn't),但我建议您找到解决方案。
答案 1 :(得分:0)
编辑:我在这里回答。 Awesomium能够做到这一点,但不是IE
就像之前提到的那样,目前的.Net WebBrowser无法做到这一点。我能够使用Awesomium完成此任务。如果您下载了.Net示例并修改了选项卡式浏览器,则可以通过添加新的WebControl和与之关联的新WebSession来测试它。只需为新会话组成一个新的DataPath目录(即.Cache2)
在我的例子中,我编辑了以下内容:
在TabView.xaml中添加了一个新的WebSession:
<awe:WebSessionProvider
x:Key="GlobalSession2"
x:Shared="False"
DataPath=".\Cache2"
Preferences="{x:Static local:MainWindow.WebPreferences}"/>
修改行以添加新行:
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="300"/>
<RowDefinition Height="300"/>
</Grid.RowDefinitions>
在第一个WebControl的周围结束后立即添加了一个新的WebControl(和周围的边界等)。这使用之前添加的新会话:
<Grid x:Name="browserContainer2" Grid.Row="2">
<Border
x:Name="browserBorder2"
BorderThickness="0,1,0,0"
BorderBrush="{DynamicResource ControlBorderBrush}"
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<awe:WebControl
x:Name="PART_Browser2"
Source="{Binding Source2, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
NativeView="{Binding NativeView, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
IsSourceView="{TemplateBinding IsSourceView}"
IsEnabled="{TemplateBinding IsSelected}"
WebSession="{Binding Source={StaticResource GlobalSession}}" />
</Border>
<ui:MetroProgressBar
Grid.ZIndex="100"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
Margin="0,3"
IsIndeterminate="{Binding IsNavigating, ElementName=PART_Browser}"
Visibility="{Binding IsNavigating, ElementName=PART_Browser, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<Grid
x:Name="statusGrid2"
Height="25"
VerticalAlignment="Bottom"
Opacity="0.8">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border
x:Name="statusBox2"
Background="{DynamicResource ControlBackgroundBrush}"
BorderBrush="{DynamicResource ControlBorderBrush}"
BorderThickness="0,1,1,0"
CornerRadius="0,5,0,0"
IsHitTestVisible="False">
<TextBlock
x:Name="targetURLBlock2"
VerticalAlignment="Center"
Margin="5,0"
IsHitTestVisible="False"
Focusable="False"
Text="{Binding TargetURL, ElementName=PART_Browser, Converter={StaticResource UrlConverter}}"
TextTrimming="CharacterEllipsis"/>
</Border>
<Grid
x:Name="zoomBox2"
Grid.Column="1"
Margin="5,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border
Grid.ZIndex="-100"
Grid.ColumnSpan="2"
Background="{DynamicResource ControlBackgroundBrush}"
BorderBrush="{DynamicResource ControlBorderBrush}"
BorderThickness="1,1,0,0"
CornerRadius="5,0,0,0"
IsHitTestVisible="False"/>
<TextBlock
VerticalAlignment="Center"
Margin="7,0"
Text="Zoom:"/>
<Slider
x:Name="zoomBar2"
Grid.Column="1"
DataContext="{Binding ElementName=PART_Browser}"
Style="{StaticResource FlatSlider}"
Margin="0,0,25,0"
Minimum="10"
Maximum="400"
Width="120"
VerticalAlignment="Center"
Value="{Binding Zoom}"
AutoToolTipPlacement="TopLeft"
IsSnapToTickEnabled="True"
IsMoveToPointEnabled="True"
SmallChange="1"
LargeChange="10"
TickFrequency="10"
Focusable="False">
<Slider.ContextMenu>
<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<MenuItem Command="{x:Static awe:WebControlCommands.ResetZoom}" CommandTarget="{Binding}" />
</ContextMenu>
</Slider.ContextMenu>
</Slider>
</Grid>
</Grid>
</Grid>
然后我在TabView.cs中添加了一个新的Source属性来存储第二个浏览器的URL:
public Uri Source2
{
get { return this.GetValue(SourceProperty2) as Uri; }
set { SetValue(SourceProperty2, value); }
}
public static readonly DependencyProperty SourceProperty2 =
DependencyProperty.Register("Source2",
typeof(Uri), typeof(TabView),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
最后硬编码两个URL以启动两个嵌入式浏览器
internal TabView( MainWindow parent, Uri url, bool isSourceView )
{
parentWindow = parent;
this.IsSourceView = isSourceView;
this.Source = new Uri("http://www.google.com");
this.Source2 = new Uri("http://www.google.com");
}