我正在嘗試將Web瀏覽器添加到現有的C#應用程序中,但是,在大約6年未使用C#的情況下,我對它的工作原理並不熟悉。C#應用程序 - 如何在xaml表單中顯示網站?
已經使用XAML創建了應用程序的GUI,並且我想用它來在應用程序中添加/嵌入Web瀏覽器,但我不知道如何執行此操作。
我有我的WebBrowser.xaml
類中的以下腳本:
<Window x:Class="RiviamAgent.WebBrowser"
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:local="clr-namespace:RiviamAgent"
mc:Ignorable="d"
Title="WebBrowser" Height="500" Width="500">
<Window.CommandBindings>
<CommandBinding Command="New" CanExecute="CommonCommandBinding_CanExecute" />
<CommandBinding Command="Open" CanExecute="CommonCommandBinding_CanExecute" />
<CommandBinding Command="Save" CanExecute="CommonCommandBinding_CanExecute" />
</Window.CommandBindings>
<DockPanel>
<ToolBarTray DockPanel.Dock="Top">
<ToolBar Header="File">
<Button Command="New" Content="New" ToolBar.OverflowMode="Always" />
<Button Command="Open" Content="Open" ToolBar.OverflowMode="Always" />
<Button Command="Save" Content="Save" ToolBar.OverflowMode="Always" />
</ToolBar>
<ToolBar Header="Edit" Margin="5.4,0,-5.4,0">
<Button Command="Cut" Content="Cut" ToolBar.OverflowMode="Always" />
<Button Command="Copy" Content="Copy" ToolBar.OverflowMode="Always" />
<Button Command="Paste" Content="Paste" ToolBar.OverflowMode="Always" />
</ToolBar>
<ToolBar Margin="9.2,0,-8.2,0">
<Button Command="Back" ToolTip="Return to the previous page"/>
<Image Source="C:\Users\elgan\workspace\browser\riviam_windows\Images\navigateBack.png" Width="20" Height="20" Margin="0,0,0,2.4" />
</ToolBar>
<ToolBar Margin="16.4,0,-16.2,0" >
<Button Command="Forward" ToolTip="Proceed to the next page" />
<Image Source="C:\Users\elgan\workspace\browser\riviam_windows\Images\navigateForward.png" Width="20" Height="20" />
</ToolBar>
</ToolBarTray>
<TextBox AcceptsReturn="True" />
<WebBrowser Name="browser" Navigating="www.google.com"/>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="12">
Web page
</TextBlock>
</DockPanel>
,這是目前呈現出空白頁,用工具欄在包含「文件」 &「編輯」菜單上,以及「後退」和「前進」導航按鈕。
我現在試圖在頁面的其餘部分顯示一個網站的內容,但我不知道如何做到這一點。
在我寫的最後幾行中,我試圖讓Google顯示在GUI的主要區域(即菜單和導航按鈕下方),但目前沒有任何內容顯示在那裏,除了文本,「網頁」。
誰能告訴我這是爲什麼?我怎樣才能讓表單顯示Google或任何其他網站?提前致謝。
我很驚訝,實際上編譯。你有一個事件處理程序的網址... – Xiaoy312