I am writing a WPF Appl. in C#. I got a MainWindow.xaml
which contains an empty frame called MainFrame. I got four other pages called Welcome.xaml
, Login.xaml
, ObjectSelection.xaml
and Exec.xaml
. To navigate from MainWindow
through the pages via the navigation buttons that are on the MainWindow
, I simply can do this and call it on my buttons:
private void PageNav1(object sender, RoutedEventArgs e)
{
MainFrame.Content = new Welcome();
}
I have buttons in that Welcome Page that need to bring you to another page, let's say to the Login.xaml
. My approach was this:
private void SelectionExport(object sender, RoutedEventArgs e)
{
MainWindow.MainFrame.Content = new Login();
}
But as usual I got an error: CS0120 An object reference is required for the non-static field, method, or property 'MainWindow.MainFrame' INTEGR8
Any idea is greatly appreciated.
You need to get a reference to the instance of the MainWindow
class and access the Frame
of this one:
private void SelectionExport(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
mainWindow.MainFrame.Content = new Login();
}
As the Documentation says
In order to use a non-static field, method, or property, you must first create an object instance.
For example,
public class MyClass
{
public void MyMethod() { }
public static void MyStaticMethod() { }
}
MyClass.MyStaticMethod(); // Works
MyClass.MyMethod(); // CS0120
// You've got to do it like this
MyClass mc = new MyClass();
mc.MyMethod();
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.