在WPF C#中关闭已打开的页面

时间:2013-10-30 05:22:12

标签: c# wpf xaml

我在我的应用程序中打开了一个新页面。

XAML

        <MenuItem Header="Admin" IsTabStop="False">
            <MenuItem x:Name="mi_ManageUsers" Header="Manage Users" Click="mi_ManageUsers_Click"/>
        </MenuItem>

C#

    private void mi_ManageUsers_Click(object sender, RoutedEventArgs e)
    {
        ManageUsers newPage = new ManageUsers();
        this.Content = newPage;
    }

现在我的新页面中有一个按钮

XAML

<Page x:Class="Billing.ManageUsers"
      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="ManageUsers">

    <Grid>
        <Button x:Name="btnClose" Content="Close" HorizontalAlignment="Left" Margin="185,219,0,0" VerticalAlignment="Top" Width="75" Click="btnClose_Click"/>

    </Grid>
</Page>

C#

    private void btnClose_Click(object sender, RoutedEventArgs e)
    {
        //OnReturn(new ReturnEventArgs<string>(this.dataItem1TextBox.Text));
        this.NavigationService.GoBack();
    }

但代码无效。我需要关闭这个新页面或点击按钮

返回previos窗口

2 个答案:

答案 0 :(得分:1)

正如我在评论中提到的那样,当您this.Content = newPage;时, 实际导航到该页面,而不是更改当前页面的Content。当我尝试使用您的代码时,我在第InvalidOperationException行上获得了this.NavigationService.GoBack();

要使其正常运行,请将mi_ManageUsers_Click方法中的代码更改为:

NavigationService.Navigate(new ManageUsers());

我已经测试了这段代码并且有效。希望这会有所帮助。

答案 1 :(得分:-1)

我为此感到挣扎,但最终找到了答案...要从打开的页面中关闭页面,请使用:

<table>
    <tr>
        <th class="day-name">Wed</th>
        <th class="day-name">Thu</th>
        <th class="day-name">Fri</th>
        <th class="day-name">Sat</th>
    </tr>
    <tr>
        <td class="day day-3">
            <span class="number">3</span>
            <span class="event event-multiday-start event-1"></span>
        </td>
        <td class="day day-4">
            <span class="number">4</span>
            <span class="event event-multiday event-1"></span>
            <span class="event event-multiday-start eventclass event-2" style="background-color:#5a9ab2;"></span>
            <span class="event event-3"></td>
        <td class="day day-5">
            <span class="number">5</span>
            <span class="event event-multiday-finish event-1"></span>
            <span class="event event-multiday eventclass event-2" style="background-color:#5a9ab2;"></span></td>
        <td class="day day-6">
            <span class="number">6</span>
            <span class="event event-ghost event-1"></span>
            <span class="event event-multiday-finish eventclass event-2" style="background-color:#5a9ab2;"></span>
        </td>
    </tr>
</table>