尝试在应用程序中获取可从各种页面访问的 StatusBar

Trying to get a StatusBar in app that could be accessible from various pages

我已经实现了两个带导航的页面。这是我的主窗口和两个页面的声明方式。

<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());
        }

当我运行应用程序时,我看到了第 1 页。到目前为止,生活还不错。我现在需要带一个 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));
        }

而第 1 页 class 如下所示。

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 根本没有显示。我只是得到一个没有任何错误的空白页。知道我在这里做错了什么吗?

问题出在页面的构造函数中。当您调用基本构造函数时,InitializeComponent() 不会调用并且 UI 无法呈现。所以你应该调用 'this' 构造函数来执行你的逻辑和 InitializationComponent()。

    public Page1(ISBView sb) : this() 
    {
        this.statusBar = sb;
        sb.UpdateMessage("now on page1");
    }