Navigation errors between MainWindows and pages (WPF Application)

W

Watlah

Guest
I am currently coding a wpf application that allows users to firstly select a process currently running on the local system, after clicking on a button, what the user has selected will be passed into a variable, and into the next few pages where they will need to answer and type in information into textboxes which needs to be kept into variables as well.

I need to store all of these information to be access in my final compiled page where i will display all of these information in a textbox. However i have ran into a few exceptions when trying to navigate through these pages/windows.

Mainwindow.xaml.cs (Window)

private void startTroubleshootButton_Clicknew(object sender, RoutedEventArgs e)
{

//get the selected process at column/index 0 and store it in a variable,
var selectedProcess = listViewProcesses.SelectedItems[0] as myProcess;
//var selectedProcess1 = listViewProcesses.SelectedItems[2] as myProcess;
if (selectedProcess == null)
{
MessageBox.Show("no selection made");
return;
}

//Store the associated process name and PID in proc1 and proc2 variable
proc1 = selectedProcess.processName;
proc2 = selectedProcess.processId;


MessageBox.Show($"you have selected the {proc1} application {proc2} ");

//instantiate the Pageone page

Q1 pg1 = new Q1();
this.Content = pg1;
}


(Q1.xaml.xs (Page)) Error receive after clicking compileButtonClick

private void compileButtonClick(object sender, RoutedEventArgs e)
{
var window = (MainWindow)Application.Current.MainWindow;
string procName = window.proc1;
int subprocPid = window.proc2;
string procPID = subprocPid.ToString();
MessageBox.Show(procName + procPID);

Compiledpage pg = new Compiledpage(); //this may not be required/efficient

//Show Pageone in the current window
this.Content = pg;

}


System.InvalidOperationException: 'Page can have only Window or Frame as parent.'



(Q1.xaml.xs (Page)) Error received after click homeButtonClick

private void homeButtonClick(object sender, RoutedEventArgs e)
{
MainWindow main = new MainWindow();
this.Content = main;

}


System.InvalidOperationException: 'Window must be the root of the tree. Cannot add Window as a child of Visual.'


compiledpage.xaml.cs

public partial class Compiledpage : Page
{
public Compiledpage()
{
InitializeComponent();
}

private string remoteIp;

public void Window_Loaded(object sender, RoutedEventArgs e)
{



//instantiate the MainWindow and assign it to the 'window' variable

var window = (MainWindow)Application.Current.MainWindow;
string procName = window.proc1;
int subprocPid = window.proc2;
string procPID = subprocPid.ToString();
//MessageBox.Show(procPID);

...



Is there a better way i can code for this "navigation and storing values across windows/pages" approach? I've heard that it is better to use UserControl but im not sure why thats the case.

Continue reading...
 
Top