Reputation: 1980
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.
Upvotes: 0
Views: 13005
Reputation: 11
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
Private mainwin_frame As Frame
Public Sub New(localframe As Frame)
InitializeComponent()
mainwin_frame = localframe
End Sub
Private Sub Page1Button_Click(sender As Object, e As RoutedEventArgs)
mainwin_frame.Content = Nothing
End Sub
End Class
The key to all of this working is passing the mainframe object to the Page1 constructor so the page can access it. I'm sure there are other ways to do this but this is a common sense approach imo.
Upvotes: 1
Reputation: 215
You're not telling an object to close.
If you're just wanting a button to act on the event, I'd recommend click event or OnClick
. If you are in VS you can just double click the button in the designer, it will auto populate everything for you.
If you want a form to close you can use similar methods from your WPF window.
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
On the other hand, if you wanted to create your own method (like in this case) you can do so like:
private void closeMethod()
{
// your code here
}
Then you call it with:
closeMethod();
Depending on your method you can tell it to do what you want on the page. Such as close the page or window.
You can also pass references in your methods
Refer to: Microsoft for methods.
Not sure if it fits here, but you may want to use Application.Exit();
in some cases, if so refer to: MSDN to find the difference.
Upvotes: 2