我已经实现了两个导航页面。以下是我的MainWindow和两个页面的声明方式。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sb="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Frame x:Name="frame" Grid.Row="0" />
</Grid>
</Window>
这是我的Page1
<Page x:Class="WpfApplication1.Page1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="This is Page 1" FontSize="20" />
<Button Content="Next" Grid.Row="1" Click="Button_Click"/>
</Grid>
</Page>
这是Page2
<Page x:Class="WpfApplication1.Page2"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="This is Page 2" FontSize="20" />
<Button Content="Exit" Grid.Row="1"/>
</Grid>
</Page>
在MainWindow构造函数后面的代码中,我导航到Page1。
public MainWindow()
{
InitializeComponent();
this.frame.Navigate(new Page1());
}
当我运行应用程序时,我会看到Page1。到目前为止生活很美好。我现在需要带一个StatusBar,其中文本将从Page1和Page2更新。我开始使用以下界面。
public interface ISBView
{
void UpdateMessage(string message);
}
然后我创建了一个用以下代码实现此接口的用户控件。
<UserControl x:Class="WpfApplication1.MyStatusBar"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<StackPanel Orientation="Horizontal">
<TextBlock Name="statusMessage" />
</StackPanel>
</Grid>
</UserControl>
public partial class MyStatusBar : UserControl, ISBView
{
public MyStatusBar()
{
InitializeComponent();
}
public void UpdateMessage(string message)
{
this.statusMessage.Text = message;
}
}
然后我在MainWindow中使用了这个用户控件,如下所示。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sb="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Frame x:Name="frame" Grid.Row="0" />
<StatusBar VerticalAlignment="Bottom" Grid.Row="1" >
<StatusBarItem>
<sb:MyStatusBar x:Name="myStatusBar" Content="Hi there!!!" />
</StatusBarItem>
</StatusBar>
</Grid>
</Window>
现在MainWindow的构造函数改为:
public MainWindow()
{
InitializeComponent();
this.frame.Navigate(new Page1(this.myStatusBar));
}
而Page1类如下所示。
public partial class Page1 : Page
{
private ISBView statusBar;
public Page1()
{
InitializeComponent();
}
public Page1(ISBView sb):base()
{
this.statusBar = sb;
sb.UpdateMessage("now on page1");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Page2());
}
}
现在问题是当我运行应用程序时,根本没有显示Page1。我只是得到一个没有任何错误的空白页面。知道我在这里做错了吗?
答案 0 :(得分:1)
问题出在你的Page的构造函数中。当您调用基础构造函数时,InitializeComponent()将不会调用,UI无法呈现。所以你应该打电话给这个&#39;构造函数执行你的逻辑和InitializationComponent()。
public Page1(ISBView sb) : this()
{
this.statusBar = sb;
sb.UpdateMessage("now on page1");
}