question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Making Properties Available to Entire App

I have several properties that I want to make available to all classes in my Xamarin.Android app (including RecyclerView Adapters & ViewHolders, all Activity(s), and custom classes). Many of the sites I have found talk about global variables, but I want the variable only for the specific instance of the app. How do I do this in Xamarin.Android?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered JessieZhang-2116 commented

Hello,


Welcome to our Microsoft Q&A platform!

I want to make available to all classes in my Xamarin.Android app (including RecyclerView Adapters & ViewHolders, all Activity(s), and custom classes)

Yes, you can define a global static variable.For example:

  public class MyConstants
 {
     public static string TestStr = "My test string";
 }

In RecyclerView Adapters & ViewHolders, all Activity(s), and custom classes, you can access and change this variable like this:

          // access this variable
         System.Diagnostics.Debug.WriteLine("MyConstants.TestStr = " + MyConstants.TestStr);
         //modify the value of this variable
         MyConstants.TestStr = "test2...";

And this variable could been accessed for your app.

Update:

If I declare the static variable in one of my Activity(s) (probably the MainLauncher), will it still be available if I navigate to another Activity? If I declare it in a custom class (like MyConstants in your example), do I need to instantiate that class at some point?

Yes, if we declare the static variable in one of our Activity(s) , it still will be available if we navigate to another Activity.

For example:

       Debug.WriteLine("MyConstants.TestStr = " + MyConstants.TestStr);

And if we declare it in a custom class (like MyConstants in above example), we don't need to instantiate that class. A static member can't be referenced through an instance. Instead, it's referenced through the type name.

For more about this, you can check document : https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static .

For example:

 public class MyBaseC
 {
     public struct MyStruct
     {
         public static int x = 100;
     }
 }

And access this variable by code:

 Console.WriteLine(MyBaseC.MyStruct.x);

Hope it can help you.


Best Regards,

Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.


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.



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If I declare the static variable in one of my Activity(s) (probably the MainLauncher), will it still be available if I navigate to another Activity? If I declare it in a custom class (like MyConstants in your example), do I need to instantiate that class at some point?

0 Votes 0 ·

Hi @njsokalski , I have updated my answer. You can check the updated part at the bottom of my answer. Hope it can help you.

0 Votes 0 ·