How to get Content in MAUI Class Library?

Jassim Al Rahma 1,521 Reputation points
2022-08-26T15:02:58.237+00:00

Hi,

I have below code in my MAUI Class Library but the name Content does not exists

Kindly help..

namespace TempMAUIClass;  
  
using Microsoft.Maui.Controls;  
  
// All the code in this file is included in all platforms.  
public class Class1  
{  
    public static void GoTo()  
    {  
        var label = new Label { ... };  
        var button = new Button { ... };  
  
        var layout = new StackLayout();  
  
        layout.Children.Add(label);  
        layout.Children.Add(button);  
  
        Content = layout;  
          
        Application.Current.MainPage = content;  
    }  
}  

Thanks,
Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,861 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,221 Reputation points Microsoft Vendor
    2022-08-29T06:50:05.553+00:00

    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.