How to access to user defined class in .NET MAUI contentpage

Mun Foong Woo 1 Reputation point
2022-12-08T17:52:59.867+00:00

Hi All,

I have defined a class ContactInfo.cs and would like the class to be accessible by all the contentpage in my .NET MAUI project.

Please let me know how to do it.

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

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 44,011 Reputation points Microsoft Vendor
    2022-12-09T02:50:09.083+00:00

    Hello,

    It is recommended to use thread-safe singleton mode to achieve this functionality.

    Please refer to the following code:

       public class ContactInfo  
       {  
           private static ContactInfo instance = null;  
           private static readonly object obj = new object();  
           public static ContactInfo Instance  
           {  
               get  
               {  
                   lock (obj)  
                   {  
                       if (instance == null)  
                       {  
                           instance = new ContactInfo();  
                       }  
                       return instance;  
                   }  
               }  
           }  
       }  
    

    Then, you could use ContactInfo.Instance to use this object by all the contentpage.

    Best Regards,

    Alec Liu.


    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.