Prevent user entered data from clearing on new window load - c#

I have a small desktop app that loads numerous windows and pages into a main screen into a grid on a button click. The relevant page loads into the grid and users then enter data in text boxes, selected radio questions and so on. They can then copy the data entered and paste it into where they need to paste it. Simple macro type desktop app.
My main issue is when the users click from one loaded window and goes to another macro that then deletes all previously entered data. So if the user then goes back to a previous macro it reloads as new.
Each window/macro has a reset button and I would ultimately like to have all data saved until that is used or app is closed.
Thanks for any help!
Update: added code below. Simple app that has a main window/landing page and a grid. On button click grid loads a page (page2 or page3) dependent on which button is clicked. If A user enters text in text boxes, goes to a different page by clicking the other button and then goes to page with said button previously entered data is gone.
code breakdown:
main screen xaml
<Page
x:Class="test.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Width="1001">
<Grid x:Name="loadgrid" Height="500" Background="White" Margin="423,110,61,110">
<Button Content="Button" Margin="-298,29,0,0" VerticalAlignment="Top" Width="113" Click="Button_Click"/>
<Button Content="Button" Margin="-298,97,0,0" VerticalAlignment="Top" Width="113" Click="Button_Click_1"/>
</Grid>
</Page>
main screen CS
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace test
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
loadgrid.Children.Add(new test.BlankPage1());
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
loadgrid.Children.Add(new test.page3());
}
}
}
page1 xaml
<Page x:Name="page2"
x:Class="test.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="#FFF57A7A" Width="400" Height="400">
<Grid>
<TextBox HorizontalAlignment="Center" Margin="0,53,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="178" AllowFocusWhenDisabled="True"/>
<TextBox HorizontalAlignment="Center" Margin="0,116,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="178"/>
<TextBox HorizontalAlignment="Center" Margin="0,168,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="178"/>
</Grid>
</Page>
page1 cs
namespace test
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
this.InitializeComponent();
}
}
}
page3 xaml
<Page x:Name="page1"
x:Class="test.page3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="#FF5C838F" Width="400" Height="400">
<Grid>
<TextBox HorizontalAlignment="Left" Margin="123,105,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="173"/>
<TextBox HorizontalAlignment="Left" Margin="123,168,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="173"/>
<TextBox HorizontalAlignment="Left" Margin="123,227,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="173"/>
</Grid>
</Page>
page3 cs
namespace test
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class page3 : Page
{
public page3()
{
this.InitializeComponent();
}
}
}

hi you can enable the page cache mode at the start of the page it store the entire page cache data check boxes data n all.
this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;

As #Ed_Plunkett said, if you want to save the state of user input, you can use MVVM. Simply put, the user's input is saved to a variable for the next page load.
But as far as your current code is concerned, Page is best to use Frame for navigation. If you need to add some controls directly to the Page, you can create a UserControl.
Here is the document about UserControl.
Here is the document about Navigation.
Best regards.

Related

How to create child window by the context menu of page in wpf?

I have a context menu what is triggered by each list item of listbox.
And, I want to create child window when I select a context menu as below:
xaml
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="150" Orientation="Vertical" Margin="15, 5, 15, 5">
<StackPanel.ContextMenu>
<ContextMenu FontSize="16">
<MenuItem Header="{x:Static localRes:Resources.ID_STRING_SETTING}" Margin="5" Command="Setting_Click"/>
</ContextMenu>
It is already a sub page of main window.
So, I can't find a way how to set MainWindow instance as the owner of new window.
Behind Code
private void Setting_Click(object sender, RoutedEventArgs e)
{
SettingWindow SettingWindow = new SettingWindow();
SettingWindow.Owner = /* I do not know how to do */
SettingWindow.Show();
}
If your click command handler is in the code behind for your main window, then you need to set
deviceSettingWindow.Owner = this;
https://learn.microsoft.com/en-us/dotnet/api/system.windows.window.owner?view=netframework-4.8
Here's a small example. It includes a button with a handler whose code is in the code behind -
<Window x:Class="MainWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ChildWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="114,137,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
CodeBehind:
using System.Windows;
namespace MainWindow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var childWindow = new ChildWindow.Window1();
childWindow.Owner = this;
childWindow.Show();
}
}
}
Child Window - just an empty window
<Window x:Class="ChildWindow.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ChildWindow"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Grid>
</Grid>
</Window>
Child Window Code Behind
using System.Windows;
namespace ChildWindow
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}
In my example, Since you are in the code behind for the MainWindow, this is a reference to the MainWindow, and setting 'childWindow.Owner = this' is setting the childWindow's owner to the MainWindow which is what I believe you want.
One thing that is a little confusing to me is that you are using a Command and a reference to an event handler in the code behind. I'm pretty sure that's not going to work. Commands need to Bind to an ICommand reference - you'll have to implement your own ICommand class, or use one from MVVM Light or another WPF MVVM Framework. Once you've got that you can pass a reference from the parent window through the Command as a CommandParameter. For an example of how to do this, see passing the current Window as a CommandParameter
If you are using an event handler on a control, then that can bind to the event handler implementation in the code behind like in my example. You need to choose one or the other.
If you're able to provide more details on how you're setup, it would make it easier for me to provide input on which way you need to go.

WPF WebView doesn't show

I'm learning WPF WebView control, I have the below MainWindow.xaml file:
<Window x:Class="MyWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWPF"
xmlns:WPF="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls.WebView"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid x:Name="Grid1" Grid.Row="1" Grid.Column="1">
<WPF:WebView x:Name="webView1"
Grid.Row="0"
Grid.Column="0"
Loaded="WebView_Loaded" />
</Grid>
</Window>
and the below corresponding MainWindow.xaml.cs file:
namespace MyWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
webView1.Navigate(new Uri("https://www.google.com"));
}
private void WebView_Loaded(object sender, RoutedEventArgs e)
{
}
}
}
But I don't get any expecting popup window. If I remove
webView1.Navigate(new Uri("https://www.google.com"));
I can get a blank popup window. What is wrong with my code and how to solve it?
UPDATE1
I added Window_Loaded event handler and move the below two in it, now I can see a blank popup window but without any content as well as message box.
Uri uri = new Uri("https://www.google.com");
WebView1.Navigate(uri);
The whole updated code-behind:
namespace MyWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
Uri uri = new Uri("https://www.google.com");
WebView1.Navigate(uri);
}
}
}
UPDATE2
I added one HyperLink and one Button into xaml file, the below is the updated xaml file:
<Window x:Class="MyWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWPF"
xmlns:WPF="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls.WebView"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" AllowsTransparency="False" >
<Grid x:Name="Grid1" Visibility="Visible" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="71*"/>
</Grid.ColumnDefinitions>
<WPF:WebView x:Name="WebView1" Visibility="Visible" Grid.Column="1" Margin="50,50,50,50">
</WPF:WebView>
<Button Name="BTN1" Click="BTN1_Click" Margin="285,380,285,10" Grid.Column="1" >Click Me</Button>
<TextBlock Margin="0,0,747,390" Grid.ColumnSpan="2">
<Hyperlink
NavigateUri="http://www.google.com">
Google
</Hyperlink>
</TextBlock>
</Grid>
</Window>
and adding Button click event handler into the code-behind with webview initialization inside it:
namespace MyWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
//Uri uri = ;
WebView1.Navigate(new Uri("https://www.google.com"));
}
private void BTN1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
WebView1.Navigate(new Uri("https://www.google.com"));
}
}
}
Nothing happens after clicking the HyperLink. I see popup Messagebox after clicking button but still google doesn't show up.
UPDATE3
I followed the link:
https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/webview#high-dpi
I set Source property Source="http://www.google.com" inside the xaml file, see the below code:
<Window x:Class="MyWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWPF"
xmlns:WPF="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls.WebView"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" AllowsTransparency="False" >
<Grid x:Name="Grid1" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="71*"/>
</Grid.ColumnDefinitions>
<WPF:WebView x:Name="WebView1" Visibility="Visible" Grid.Column="1" Margin="50,50,50,50" Source="http://www.google.com" >
</WPF:WebView>
<Button Name="BTN1" Click="BTN1_Click" Margin="285,380,285,10" Grid.Column="1" >Click Me</Button>
<TextBlock Margin="0,0,747,390" Grid.ColumnSpan="2">
<Hyperlink
NavigateUri="http://www.google.com">
Google
</Hyperlink>
</TextBlock>
</Grid>
</Window>
But still not working.
UPDATE4
I added((ISupportInitialize)webView).BeginInit() and ((ISupportInitialize)webView).EndInit() into the button click event handler, see the below:
using Microsoft.Toolkit.Wpf.UI.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
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 MyWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
}
private void BTN1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
((ISupportInitialize)WebView1).BeginInit();
((ISupportInitialize)WebView1).EndInit();
WebView1.Navigate(new Uri("https://localhost:3000"));
Console.ReadLine();
}
}
}
But get the below error message at this line:
((ISupportInitialize)WebView1).BeginInit();
:
$exception {"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure
\"Microsoft.Toolkit.Win32.UI.Controls.DesignerUI.resources\" was
correctly embedded or linked into assembly
\"Microsoft.Toolkit.Wpf.UI.Controls.WebView\" at compile time, or that
all the satellite assemblies required are loadable and fully
signed."} System.Resources.MissingManifestResourceException
Then I went back and added more properties based on this link:
https://github.com/rjmurillo/webview-samples/blob/master/Toolkit/dotnet/Microsoft.Toolkit.Win32.Samples.WPF.WebView/MainWindow.xaml
<WPF:WebView x:Name="WebView1" Visibility="Visible" Grid.Column="1" Margin="50,50,50,50" Source="http://www.google.com" IsIndexedDBEnabled="True"
IsJavaScriptEnabled="True"
IsPrivateNetworkClientServerCapabilityEnabled="True"
IsScriptNotifyAllowed="True" >
</WPF:WebView>
But still WebView not working.
I was having the same issue upgrading from the first preview Toolkit, and it was all because the Navigate function causing to start window hidden.
Change from:
WebView1.Navigate(new Uri("https://localhost:3000"));
To:
WebView1.Source = new Uri("https://localhost:3000");
I was also having this issue and even using the myWebBrowser.Source = new Uri("https://www.google.com"); it was not showing the page.
I added the following to the code and now it loads fine.
myWebBrowser.BringIntoView();
I am using this with WPF and displaying it in a popup window, I'm not sure if that has anything to do with the issue I was having or not.
Did you run your program as an administrator (with elevated privileges), either directly or through Visual Studio?
According to this discussion this is not possible, the Win32 WebView does not support running under an elevated token.
If you check your Windows event log, you should also find an application error like the one mentioned in the discussion.
I also tried to use WebView WPF, but it can not show when I use webview.Source = url as accepted answer.
Finally, I changed to use CefSharp.Wpf library following this link https://www.technical-recipes.com/2016/using-the-cefsharp-chromium-web-browser-in-wpf-xaml/
And it worked perfectly at my site, i shared for whom concerned.
Create a new WPF project
Obtain the CefSharp packages using NuGet
Select Tool > NuGet Package manager > Package Manager Console.
Use the PM> install-package CefSharp.Wpf
Use in WPF as
<Window x:Class="CefSharpBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
Title="MainWindow"
Height="350" Width="525">
<Grid>
<wpf:ChromiumWebBrowser
x:Name="Browser"
Address="http://www.myaddress.com" />
</Grid>
</Window>

how to show auth0 client loginasync in webview

I am trying to build an app with login page that uses auth0, now when I call LoginAsync the sdk will popup the login page in a different frame, I want to show the login page in a webview that is defined in xaml.
I want to show login page that popup in this [ in a webview defined in this is xaml:
<Page
x:Class="Auth0App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Auth0App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Margin="10,0">
<StackPanel>
<TextBlock Style="{ThemeResource SubheaderTextBlockStyle}">Log in using widget</TextBlock>
<Grid HorizontalAlignment="Center" Margin="0,10">
<Button Content="Login" x:Name="LoginButton" Click="LoginButton_OnClick"/>
</Grid>
</StackPanel>
<StackPanel>
<WebView Name="loginWebView"/>
</StackPanel>
</StackPanel>
</Page>
and the code behind is:
using Auth0.SDK;
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Auth0App
{
public sealed partial class MainPage : Page
{
private Auth0Client auth0Client;
public MainPage()
{
this.InitializeComponent();
auth0Client = new Auth0Client("mydomain", "**************************");
}
private async void LoginButton_OnClick(object sender, RoutedEventArgs e)
{
try
{
var user = await auth0Client.LoginAsync();
}
catch (Exception ex)
{
}
}
}
}
How would I wire auth0 client with my webview?
I would start by looking at the Auth0 quickstart for this kind of project. I think you're about halfway there, and the quickstart would get you the rest of the way.
Feel free to reach out if there's anything missing or you need further assistance!
--Kassandra Perch, Developer Evangelist

WinRT FlipView binding fails possibly

I have a xaml page:
<Page x:Class="DailyStyleW8.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DailyStyleW8"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:DataTypes"
mc:Ignorable="d">
<Page.Resources>
<converters:PortableImageConverter x:Key="ImageConverter" />
</Page.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<ProgressBar x:Name="loadingViewer"
IsIndeterminate="True"
Height="20" />
<FlipView x:Name="displayViewer"
ItemsSource="{Binding}"
Visibility="Collapsed">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<Image Source="{Binding Image,Converter={StaticResource ImageConverter}}" />
<TextBlock Text="{Binding Name}" />
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
</Grid>
</Grid>
</Page>
and in the code behind file:
using DailyStyleApp;
using PortableAPI;
using System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace DailyStyleW8
{
/// <summary>
/// Display a list of recent updates to the user
/// </summary>
public sealed partial class MainPage : Page
{
Controller controller = new Controller();
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
LoadContent();
}
private async void LoadContent()
{
var viewModel = await controller.GetMultiDayAsync(DateTime.Now, PortableAPIProvider.Storage.ReadFromSettings<int>("CacheDuration", 7));
displayViewer.ItemsSource = viewModel.Items;
displayViewer.Visibility = Windows.UI.Xaml.Visibility.Visible;
loadingViewer.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
}
}
Now when I run the code the LoadContent function is called correctly and the viewModel object is formed correctly. If I comment out the line displayViewer.ItemsSource = viewModel.Items; the ProgressBar visibility is changed as you would expect.
When that line is left in and stepped through all 4 lines inside the LoadContent method are run, however the FlipView is not updated with the new items and the ProgressBar visibility is not changed. viewModel.Items is of type List<T>.
I am even sure really what to be searching for here. I am guessing it's something wrong with the XAML and my binding?
The issue related to this question was actually to do with another section of code in the application. Elsewhere I had a series of async / await calls that were locking up the UI thread.
This prevented the scheduler from ever triggering the callbacks for the async. In short solution to the problem: Never call await on something that is called from the UI thread (and not via another async call).

How do i navigate from one Xaml file to another?

I am very new to XAML and WPF.I have a problem.
I have two files. first.xaml and second.Xaml. There is a button in first.xaml, on click of which it should navigate to second.xaml.How do i achieve this.
This is one way of organising the code:
Your second.xaml should contain your window definiton e.g.:
<Window x:Class="MediaCheckerWPF.AboutBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="About Media Checker" Height="300" Width="400" ResizeMode="NoResize"
Icon="/MediaCheckerWPF;component/Resources/checker.ico"
ShowInTaskbar="False">
<Grid>
...
</Grid>
</Window>
Your first.xaml has the button e.g.:
<Button Height="23" HorizontalAlignment="Right" Name="aboutButton"
VerticalAlignment="Top" Width="23" Click="AboutButton_Click"
Content="{DynamicResource TInformationButton}"
ToolTip="{DynamicResource TInformationButtonTooltip}" Margin="0,0,8,0"/>
Then in the code behind:
private void AboutButton_Click(object sender, RoutedEventArgs e)
{
var about = new AboutBox { Owner = this };
about.Initialise();
about.Show();
}
ChrisF's answer is a good way of popping up a new window in a Windows-style application, except you should not call Initialize that way (it should be called from the constructor).
If you want web-style navigation instead, you should use the Page class instead of Window, along with NavigationService. This also allows your WPF application to run inside an actual web browser.

Categories