尝试在应用程序中获取可从各个页面访问的状态栏
本文关键字:访问 状态栏 应用程序 获取 | 更新日期: 2023-09-27 18:35:48
我已经实现了两个带有导航的页面。以下是我的主窗口和两个页面的声明方式。
<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>
这是我的页面1
<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>
这是第 2 页
<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。到目前为止,生活是美好的。我现在需要带一个状态栏,其中的文本将从第 1 页和第 2 页更新。我从以下界面开始。
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;
}
}
然后,我在主窗口中使用此用户控件,如下所示。
<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());
}
}
现在的问题是当我运行应用程序时,页面 1 根本没有显示。我只是得到一个空白页,没有任何错误。知道我在这里做错了什么吗?
问题出在主页的构造函数中。当你调用你的基构造函数时,初始化组件()将不会调用,UI也无法呈现。所以你应该调用'this'构造函数来执行你的逻辑和InitializationComponent()。
public Page1(ISBView sb) : this()
{
this.statusBar = sb;
sb.UpdateMessage("now on page1");
}