- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有没有一种方法可以根据加载的初始网页使用网络浏览器控件自动调整窗口大小?
对 .Height 或 .Width 属性没有任何运气,也尝试了 ActualWidth、ActualHeight 属性。
这是 XAML。
<dialog:Window x:Class="MyApplication.Widgets.Controls.DiagnosticsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dialog="clr-namespace:UI.Common.Dialog;assembly=UI.Common"
xmlns:controls="clr-namespace:UI.Common.Controls;assembly=UI.Common"
mc:Ignorable="d"
Height="500" Width="600"
ShowInTaskbar="False"
ResizeMode="NoResize"
Title="{Binding Path=WindowTitle}"
WindowStartupLocation="CenterOwner">
<controls:DialogFrame Width="{Binding Path=WindowWidth}" Height="Auto" CloseCommand="Close">
<WebBrowser x:Name="DiagnosticsWebBrowser" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</controls:DialogFrame>
</dialog:Window>
我试着做了以下但没有任何运气, WPF: How to auto-size WebBrowser inside a Popup?
这是我目前得到的...(两者都没有正确调整大小)。
最佳答案
WPF Web 浏览器控件不会公开调整窗口大小所需的加载页面大小。最简单的解决方案是使用 WinForms WebBrowser(如 Noseratio 建议的那样),您可以使用 WindowsFormsHost 元素将其嵌入到 WPF 窗口中:
<Window
x:Class="MyApplication.Widgets.Controls.DiagnosticsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
mc:Ignorable="d"
SizeToContent="WidthAndHeight"
WindowStartupLocation="Manual">
<WindowsFormsHost>
<forms:WebBrowser
x:Name="DiagnosticsWebBrowser"
Width="420"
Height="240"
ScrollBarsEnabled="False"
DocumentCompleted="DiagnosticsWebBrowser_DocumentCompleted" />
</WindowsFormsHost>
</Window>
不要忘记将 System.Windows.Forms 程序集添加到您的项目中。
然后,在您的代码隐藏中:
// Called after the document has been rendered
private void DiagnosticsWebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
// Resize the window
int width = WebBrowser.Document.Body.ScrollRectangle.Size.Width;
width = Math.Min(width, (int)SystemParameters.WorkArea.Width - 100);
int height = WebBrowser.Document.Body.ScrollRectangle.Size.Height;
height = Math.Min(height, (int)SystemParameters.WorkArea.Height - 100);
DiagnosticsWebBrowser.Size = new System.Drawing.Size(width, height);
UpdateLayout();
// Re-center the window
WindowStartupLocation = WindowStartupLocation.Manual;
Left = (SystemParameters.WorkArea.Width - ActualWidth) / 2 + SystemParameters.WorkArea.Left;
Top = (SystemParameters.WorkArea.Height - ActualHeight) / 2 + SystemParameters.WorkArea.Top;
}
请务必注意,网页没有“固有”页面大小 - 即,它们可以用完网络浏览器提供的任何空间。作为解决方法,我们创建了一个 WebBrowser,其大小为我们的首选最小值(上例中为 420x240),然后在呈现文档后,我们通过查看其 ScrollRectangle 来检查文档最终是否大于该值。
一些注意事项:我将窗口的大小限制为 SystemParamters.WorkArea,这样如果我们有一个非常大的文档,我们最终不会得到一个比我们的桌面大的窗口。我进一步将宽度和高度偏移 100 像素,以确保我们的窗口标题、滚动条等也有空间。
另请注意,WindowStartupLocation="CenterOwner"将不起作用,因为我们在启动后手动调整窗口大小。因此,我将 WindowSTartupLocation 更改为“Manual”,然后在当前工作区内手动将窗口居中。
最后,通过使用一些 Win32 互操作调用来获取浏览器的滚动信息,人们可能会使用此技术来处理 WPF 的 WebBrowser 控件。不过,仅使用 WinForms 控件要容易得多。
希望对您有所帮助!
关于c# - 将 Dialog/WebBrowser 调整为网页宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21170316/
运行这一行: webbrowser.open(page.path, new=0, autoraise=True) 我总是在新标签页中打开页面。 我怎样才能将它放在与它应该使用 new=0 相同的选项卡
基于 MSDN 上的这篇文章:How To Determine When a Page Is Done Loading in WebBrowser Control ,并且从过去关于 StackOver
我不明白为什么这行简单的代码行不通: // Bulding tree var declaration = new XDeclaration("1.0", "UTF-8", "yes"); var ro
这些是等价的吗? WebBrowser.Navigate('http://google.com'); 和 WebBrowser.Stop(); WebBrowser.Navigate('http://
我需要获取有关 Webbrowser 的滚动条(位置、大小、可见性)的信息控制外部应用程序,我尝试使用 GetScrollBarInfo来 self 之前的功能 question ,但该函数总是返回
目前在带有 WebBrowser 的 vb6 项目中,我遇到了一个问题。我想在我的应用程序中显示 adsense 广告,所以我创建了一个 html 页面并将文件放入服务器(adsense 批准),我尝
Windows Phone 8 应用程序,WebBrowser 控件。我通过 NavigateToString 加载了一大块 HTML (设置后 IsScriptEnabled=true )。一段时间
我有一个嵌入 WebBrowser 的 native Windows 应用程序, IE。 CLSID_WebBrowser 8856F961-340A-11D0-A96B-00C04FD705A2 S
提前致谢。 在我的应用程序中,我将 WebBrowser 控件替换为 Microsoft 的 WebView2 . 我想知道 WebView2 中 WebBrowser 控件的以下成员的等价物。 We
我怎样才能让 Web 浏览器调用自定义方案来启动 Activity,然后我想按下后退按钮但不返回 Web 浏览器。 我只想在调用网络浏览器然后调用我的方案启动另一个 Activity 时实现转发。当我
我们的目标是:嵌入在 .net winform 中的支持 Watin 的浏览器测试。 目前,我们正在使用 .net WebBrowser 控件将浏览器行为嵌入到 winform 中。我们使用如下代码将
我正在使用将 WebBrowser 控件嵌入到 Windows 窗体中的应用程序,我注意到一个严重的内存泄漏问题。在阅读了互联网上的其他类似案例后,问题似乎出在 IE 上。 我问过其他人是如何解决这个
我使用了“LoadCompleted”事件,基本上一旦收到该事件,我就需要截取屏幕截图(我已经构建)。问题是一旦这个事件结束,屏幕截图就只是空白(页面还没有加载),只有当我延迟 5-10 秒时,屏幕截
我试图自动打开多个用户配置文件,给定几个不同网站上的名称列表,但我找不到在新窗口中打开链接的方法,这意味着我无法对我打开的不同网站进行排序自己的橱窗收藏。 这是我的代码: import webbrow
我使用 vb.net + wpf+ System.Windows.Forms.WebBrowser 在 wpf 表单上显示网页内容。 此网站“http://manuim.com/laisha/”在控件
有没有办法检查用户是否安装了 PDF 阅读器? 也许使用 ActiveX 组件? 请你帮助我好吗? 最佳答案 不,我不这么认为 - 但默认情况下你总是可以通过谷歌的 PDF 阅读器直接链接你的链接 -
尝试在 WPF(C#/XAML、.NET 4.0)WebBrowser 应用程序中测试基本浏览器概念。到目前为止,唯一的问题是以编程方式缩放。有没有人有这方面的经验? MSDN 没有列出任何内容:ht
首先是一些定义,以保持清楚。 User: A live person, using the software Client: A company that is paying for a custom
尝试使用 webBrowser.Navigate("https://home.nest.com/") 加载页面时,我不断收到脚本错误.它会从我的普通互联网浏览器中正常显示,但不会在我的程序中显示。 谁
显然,WPF WebBrowser control 存在一些严重的键盘和焦点问题。 .我已经组装了一个简单的 WPF 应用程序,只有一个 WebBrowser 和两个按钮。该应用程序加载一个非常基本的
我是一名优秀的程序员,十分优秀!