Hello @Jassim Al Rahma ,
Content
is the property of ContentPage. If you want to call Content
in Class1
, the Class1
should be derived from ContentPage
.
public class Class1: ContentPage{...}
In addition, you use the Content
in the static GoTo()
method. But the Content
is a property 'member' of ContentPage
, only a ContentPage
object can reference it. This will result in compiler error CS0120.
public void GoTo()
{ .......
Content = layout;
......
}
Further more, you set Application.Current.MainPage = content
, Application.Current.MainPage
is of type Microsoft.Maui.Controls.Page
, but content
is of type Microsoft.Maui.Controls.View
. You can set Application.Current.MainPage = this;
, or set any other pages.
------------Update---------------
You got the blank page because you created a new Signin
page but you haven't call the GoTo()
method, you can try
Signin myPage = new Signin();
myPage.GoTo();
how can I just do:
MyClass.Signin();
to set the Page?
You would like to make a static class and call the static method, please refer to the following code :
First, you can create a static class named MyClass
, then add the static method named Sigin()
for it:
public static class MyClass
{
public static void Signin()
{
Signin myPage = new Signin();
//myPage.GoTo(); if you don't want to call the GoTo() method here, you can move it to the Constructor of Signin page
Application.Current.MainPage = myPage;
}
}
The Signin
page:
public class Signin : ContentPage
{
public Signin()// Constructor method
{
this.GoTo(); //if you have called myPage.GoTo() above, you don't have to call this method again
}
public void GoTo()
{ .......
Content = layout;
// Application.Current.MainPage = this; the MainPage has been set in the static Signin() method above
}
}
For more information, please see:
Instance constructors - C# programming guide | Microsoft Learn
Static Classes and Static Class Members - C# Programming Guide | Microsoft Learn
Compiler Error CS0120 | Microsoft Learn
ContentPage - .NET MAUI | Microsoft Learn
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.