Просмотр HTML-документов в WebBrowser
- В декларативный раздел XAML добавьте контейнер <TabControl> с вкладкой <TabItem Header="HTML1">
- Заполните файл Window1.xaml следующим кодом
<Window x:Class="WpfText3.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300" Background="LightGray" > <TabControl> <TabItem Header="HTML1"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <WebBrowser Grid.Row="0" Name="webBrowser1" /> <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center"> <Button Width="75" Content="Back" Click="backButton_Click" Margin="0,5,0,0" /> <Button Width="75" Content="Load" Click="loadButton_Click" Margin="5,5,5,0" /> <Button Width="75" Content="Forward" Click="forwardButton_Click" Margin="0,5,0,0" /> </StackPanel> </Grid> </TabItem> </TabControl></Window> - Обработчики события Click кнопок заполните так
using System;using System.Collections.Generic;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes; namespace WpfText3{ public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void loadButton_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog(); openFileDialog.Filter = "HTML Documents (*.htm; *.html)|*.htm;*.html"; openFileDialog.InitialDirectory = System.IO.Directory.GetCurrentDirectory(); openFileDialog.Multiselect=false; if (openFileDialog.ShowDialog() == false || openFileDialog.FileName == String.Empty) return; Uri uri = new Uri(openFileDialog.FileName); this.webBrowser1.Source = uri; } private void backButton_Click(object sender, RoutedEventArgs e) { if (this.webBrowser1.CanGoBack) { this.webBrowser1.GoBack(); } } private void forwardButton_Click(object sender, RoutedEventArgs e) { if (this.webBrowser1.CanGoForward) { this.webBrowser1.GoForward(); } } }} - Запустите проект - получится нормальный инструмент просмотра HTML -документов
Дата добавления: 2015-04-15; просмотров: 1171;