c# change page

C# Page Switching: Master the Code

By Brian Rodriguez
AD

switching between pages in a C# application is a common task, often achieved by using Navigation or Routing services. This process, also known as navigation, allows users to effortlessly move between different views or screens within your software, much like turning the pages of a book.

The .NET framework, along with WPF (Windows Presentation Foundation) and ASP.NET, provides several ways to perform page changes. We'll explore these methods, along with best practices and common pitfalls.

Page Navigation in WPF Applications

In WPF, navigation typically involves switching between different pages within a Frame control. The Frame allows hosting of child elements, including other Frames, panes, and pages.

AD
C23 Logo
C23 Logo
an app that says your account is being closed, and the text below it reads
an app that says your account is being closed, and the text below it reads
🤎
🤎

To navigate to a new page, you use the Navigate() method. Here's a simple example:

```csharp myFrame.Navigate(new MyNewPage()); ```

Using UserControl for Navigable Pages

Another way is to create a UserControl for each navigable page. UserControls in WPF provide reusability, allowing code and style definitions to operate across multiple forms.

23 pages gone. Conversion rate doubled.
23 pages gone. Conversion rate doubled.
Change
Change
a piece of paper with the words change can be scary but so is staying the same
a piece of paper with the words change can be scary but so is staying the same

To use UserControl for navigation, place your UserControl in the Frame:

```csharp MyNewPage myNewPage = new MyNewPage(); myFrame.Content = myNewPage; ```

Navigating with Parameters

Sometimes, you may need to pass data to the target page. This can be achieved by using the NavigationService and QueryString.

AD

In the source page:

Change
Change
10 Lessons Learned by 23 — HALEY IVERS
10 Lessons Learned by 23 — HALEY IVERS
the only constant in life is change text on white background with black font and an orange flower
the only constant in life is change text on white background with black font and an orange flower
It’s your time to embrace change!
It’s your time to embrace change!
a book sitting on top of a wooden table next to a window
a book sitting on top of a wooden table next to a window
Revenge In Her Eyes Live Wallpaper
Revenge In Her Eyes Live Wallpaper
```csharp NavigationService.Navigate(new Uri("/MyNewPage.xaml?param=value", UriKind.Relative)); ```

In the target page, you can retrieve parameters using:

```csharp string param = NavigationContext.QueryString["param"]; ```

Routing in ASP.NET MVC Applications

In web applications using ASP.NET MVC, the navigation process is handled through routing. A route is defined for each URL, linking to a specific controller and action.

AD

To define routes in your application, you use the RouteConfig.cs file:

a black background with the words change on it
a black background with the words change on it
```csharp routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); ```

Programmatic Navigation

Besides defining route patterns, you can also perform navigation programmatically using the RedirectToRouteResult or RedirectToAction methods.

For example, to redirect to a new page, you can use:

AD
```csharp return RedirectToAction("NewAction", "NewController"); ```

Navigation with Parameters

Like WPF, you can pass parameters during the navigation process. Simply include them in the route URL:

```csharp return RedirectToAction("Details", new { id = model.Id }); ```

In the target action, you can retrieve parameters using:

```csharp int id = Convert.ToInt32(RouteData.Values["id"]) ?? -1; ```

In conclusion, whether you're working in WPF or ASP.NET MVC, changing pages in a C# application can be achieved seamlessly using built-in navigation services with a bit of familiarization with their nuances. Happy coding!

AD