执行应用程序时无法显示我的 WebBrowser 和文本框。我可以在我的设计中看到它们 window。
I can't get my WebBrowser and Textbox to display when I execute the app. I can see them in my design window.
这是我的 XAML 和 C# 代码。我是 xaml 的新手,请多多包涵。就像我上面提到的,当我执行应用程序时,我根本看不到 WebBrowser window 或 TextBox。
XAML:
<Window x:Class="HTML_Viewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="HTML Viewer" Height="350" Width="525">
<Grid Name="grid_Main">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Menu Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="0">
<MenuItem Header="File">
<MenuItem Header="Open HTML File"/>
<Separator/>
<MenuItem Header="Save"/>
<Separator/>
</MenuItem>
<ToolBar Width="Auto">
<Button x:Name="btnLocal" BorderBrush="DarkGray" Content="Load Local" Click="btnLocal_Click" Width="67"
HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand" Panel.ZIndex="1"
Margin="0,0,0,-1" Height="22"/>
<Button x:Name="btnCodeView" BorderBrush="DarkGray" Content="Code View" Click="btnCodeView_Click" Width="67"
HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand" Panel.ZIndex="1"
Height="Auto" Margin="0,0,0,-1"/>
<Button x:Name="btnPageView" BorderBrush="DarkGray" Content="Page View" Click="btnPageView_Click" Width="67"
HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand" Panel.ZIndex="1"
Height="Auto" Margin="0,0,0,-1"/>
<Button x:Name="btnSplitView" BorderBrush="DarkGray" Content="Split View" Click="btnSplitView_Click" Width="67"
HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand" Panel.ZIndex="1"
Height="Auto" Margin="0,0,0,-1"/>
</ToolBar>
</Menu>
<Grid Name="innerGrid" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Name="col1" Width="*"/>
<ColumnDefinition Name="col2" Width="*"/>
</Grid.ColumnDefinitions>
<WebBrowser x:Name="webOutput" Width="{Binding ElementName=col1,Path=ActualWidth}"
Height="{Binding ElementName=col1, Path=ActualHeight}"/>
<TextBox x:Name="txtInput" Background="Bisque" Grid.Column="1" Width="{Binding ElementName=col2,Path=ActualWidth}"
Height="{Binding ElementName=col2, Path=ActualHeight}" />
</Grid>
</Grid>
</Window>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using Microsoft.Win32;
using System.IO;
namespace HTML_Viewer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnCodeView_Click(object sender, RoutedEventArgs e)
{
}
private void btnLocal_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
webOutput.Navigate(new Uri(openFileDialog.FileName));
txtInput.Text = File.ReadAllText(openFileDialog.FileName);
}
}
private void btnPageView_Click(object sender, RoutedEventArgs e)
{
}
private void btnSplitView_Click(object sender, RoutedEventArgs e)
{
}
}
}
我可以通过删除指定的高度和宽度来查看您的控件,
<WebBrowser x:Name="webOutput" />
<TextBox x:Name="txtInput" Background="Bisque" Grid.Column="1"/>
这是我的 XAML 和 C# 代码。我是 xaml 的新手,请多多包涵。就像我上面提到的,当我执行应用程序时,我根本看不到 WebBrowser window 或 TextBox。
XAML:
<Window x:Class="HTML_Viewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="HTML Viewer" Height="350" Width="525">
<Grid Name="grid_Main">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Menu Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="0">
<MenuItem Header="File">
<MenuItem Header="Open HTML File"/>
<Separator/>
<MenuItem Header="Save"/>
<Separator/>
</MenuItem>
<ToolBar Width="Auto">
<Button x:Name="btnLocal" BorderBrush="DarkGray" Content="Load Local" Click="btnLocal_Click" Width="67"
HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand" Panel.ZIndex="1"
Margin="0,0,0,-1" Height="22"/>
<Button x:Name="btnCodeView" BorderBrush="DarkGray" Content="Code View" Click="btnCodeView_Click" Width="67"
HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand" Panel.ZIndex="1"
Height="Auto" Margin="0,0,0,-1"/>
<Button x:Name="btnPageView" BorderBrush="DarkGray" Content="Page View" Click="btnPageView_Click" Width="67"
HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand" Panel.ZIndex="1"
Height="Auto" Margin="0,0,0,-1"/>
<Button x:Name="btnSplitView" BorderBrush="DarkGray" Content="Split View" Click="btnSplitView_Click" Width="67"
HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand" Panel.ZIndex="1"
Height="Auto" Margin="0,0,0,-1"/>
</ToolBar>
</Menu>
<Grid Name="innerGrid" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Name="col1" Width="*"/>
<ColumnDefinition Name="col2" Width="*"/>
</Grid.ColumnDefinitions>
<WebBrowser x:Name="webOutput" Width="{Binding ElementName=col1,Path=ActualWidth}"
Height="{Binding ElementName=col1, Path=ActualHeight}"/>
<TextBox x:Name="txtInput" Background="Bisque" Grid.Column="1" Width="{Binding ElementName=col2,Path=ActualWidth}"
Height="{Binding ElementName=col2, Path=ActualHeight}" />
</Grid>
</Grid>
</Window>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using Microsoft.Win32;
using System.IO;
namespace HTML_Viewer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnCodeView_Click(object sender, RoutedEventArgs e)
{
}
private void btnLocal_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
webOutput.Navigate(new Uri(openFileDialog.FileName));
txtInput.Text = File.ReadAllText(openFileDialog.FileName);
}
}
private void btnPageView_Click(object sender, RoutedEventArgs e)
{
}
private void btnSplitView_Click(object sender, RoutedEventArgs e)
{
}
}
}
我可以通过删除指定的高度和宽度来查看您的控件,
<WebBrowser x:Name="webOutput" />
<TextBox x:Name="txtInput" Background="Bisque" Grid.Column="1"/>