简体   繁体   中英

Closing a page in WPF

I am new to C#, I just created a new page and want to close it in the XAML side I have:

<Grid>
    <Button Content="Back" Click="Button_Click_Exit"
        HorizontalAlignment="Left" Margin="220,263,0,0"
        VerticalAlignment="Top" Width="75"/>
</Grid>

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;
namespace UI
{
    /// <summary>
    /// Interaction logic for calibrationPage.xaml
    /// </summary>
    public partial class calibrationPage : Page
    {
        public calibrationPage()
        {
            InitializeComponent();
        }

        private void Button_Click_Exit(object sender,
            RoutedEventArgs e)
        {
            Close();
        }
    }
}

I think this supposed to be really simple, somehow I get this error, when I try to build it:

error CS1061: 'UI.calibrationPage' does not contain a definition for 'Close'
and no extension method 'Close' accepting a first argument
of type 'UI.calibrationPage' could be found 

Edit 1: I understand that close() does not exit, then rephrasing my question, how can I simply close the page using a button click?

Edit 2: For the benefit of others: I ended up using PageNavigator.NavigateTo function to navigate back and forth between pages, I do not think that there is a concept of closing one page in WPF. Thanks for everyone's participation.

I am new to c#, I just created a new page and want to close it in the xaml side I have:

<Grid>
        <Button Content="Back" Click="Button_Click_Exit" HorizontalAlignment="Left" Margin="220,263,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>

and on the .xaml.cs side:

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;
    namespace UI
    {
        /// <summary>
        /// Interaction logic for calibrationPage.xaml
        /// </summary>
        public partial class calibrationPage : Page
        {
            public calibrationPage()
            {
                InitializeComponent();
            }

            private void Button_Click_Exit(object sender, RoutedEventArgs e)
            {
                Close();
            }
        }
    }

I think this supposed to be really simple, somehow I get this error, when I try to build it:

error CS1061: 'UI.calibrationPage' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'UI.calibrationPage' could be found 

EDIT: I understand that close() does not exit, then rephrasing my question, how can I simply close the page using a button click?

EDIT2: For the benefit of others: I ended up using PageNavigator.NavigateTo function to navigate back and forth between pages, I do not think that there is a concept of closing one page in wpf . Thanks for everyone's participation.

You would think closing a page would be a trivial task, but there's no page.Close property so what is one to do? I don't think pages were designed to be opened and closed like Windows, but here's a solution that should solve the problem for most.

To start, create a new WPF application and add a page to it called Page1.

In MainWindow.xaml, create a grid with a button and frame to display your page. (The frame is a key element because you can close the page by setting the frame.content = Nothing)

<Window x:Class="MainWindow"
...
    <Grid>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top">
            <Button Content="Page1" Click="Page1_Click" Height="40"/>
        </StackPanel>
        <Frame x:Name="mainframe" Margin="0,50,0,0" NavigationUIVisibility="Hidden"/>
    </Grid>
</Window>

In MainWindow.xaml.vb, add this code to display the page within the frame...

Class MainWindow
    Private Sub Page1_Click(sender As Object, e As RoutedEventArgs)
        Dim page1 As New Page1(mainframe)
        mainframe.Content = page1
    End Sub
End Class

In Page1.xaml, create a grid with a button to close the page...

<Page x:Class="Page1">
    <Grid>
        <Button Click="Page1Button_Click" Content="Close Page 1" Height="140" Width="140"/>
    </Grid>
</Page>

And in your Page1.xaml.vb, close the page like this...

Class Page1
    Dim mainwin_frame As Frame

    Public Sub New(frame As Frame)
        InitializeComponent()
        mainwin_frame = frame
    End Sub

    Private Sub Page1Button_Click(sender As Object, e As RoutedEventArgs)
        mainwin_frame.Content = Nothing
    End Sub
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM