Use class object instance everywhere in the app.

Noob question probably, trying to get the best way to handle this type of problem.
Trying to authenticate the user and then fetching his data with RestSharp such as the token, username, email etc..
What I want is to create an instance object of my class User which has the properties of token, username, email etc. and then that same object can be used everywhere, since there will only be one User object at once (only one user can log in via the app at the same time).
So in another ViewController I would want to be able to easily get the token that is saved in the Token property of the User object that I have created earlier.

Is the way I am approaching this even the right way to do it? Does xamarin give a better solution?
Thank you in advance.

Tagged:

Best Answers

  • QasimAlbaqaliQasimAlbaqali US
    Accepted Answer

    I actually went and made my class a Singleton and created the instance inside the class and in that way I can directly update the properties and execute the methods.

  • KMullinsKMullins US Xamurai
    Accepted Answer

    Hi @QasimAlbaqali,

    Sure, I'm going to take an example from a cross-platform game that I'm hoping to release to the community (if I ever get the time to finish it).

    So in my app delegate (AppDelegate.cs), I'm defining a public computed property:

    [Register ("AppDelegate")]
    public class AppDelegate : UIApplicationDelegate
    {
        #region Computed Properties
        public override UIWindow Window { get; set; }
        public ONCardGame CurrentGame { get; set; } = new ONCardGame();
        #endregion
        ...
    }
    

    Later, in any part of the app that I want to access the CurrentGame property, I add the following "shortcut" to my class:

    public partial class GameBoardView : UIView
    {
        #region Application Access
        public static AppDelegate App {
            get { return (AppDelegate)UIApplication.SharedApplication.Delegate; }
        }
        #endregion
        ...
    }
    

    And within the class I can call App.CurrentGame to access any property of the ONCardGame instance.

    Please let me know if you still have any questions.

    Kevin

Answers

  • DenisGordinDenisGordin RUMember ✭✭

    Hi!

    You can put your object of User into AppDelegate and save/restore/etc object when app will change its state (WillEnterForeground/OnActivated/etc).
    For getting User object you can use something like this:

    (AppDelegate)UIApplication.SharedApplication.Delegate.User;

  • QasimAlbaqaliQasimAlbaqali USMember
    Accepted Answer

    I actually went and made my class a Singleton and created the instance inside the class and in that way I can directly update the properties and execute the methods.

  • KMullinsKMullins USMember, Xamarin Team Xamurai

    I usually end up doing what Denis suggests and add it to the AppDelegate.

  • QasimAlbaqaliQasimAlbaqali USMember

    @KMullins said:
    I usually end up doing what Denis suggests and add it to the AppDelegate.

    Could you please show me an example on how to do that? Noob here :smile:

  • KMullinsKMullins USMember, Xamarin Team Xamurai
    Accepted Answer

    Hi @QasimAlbaqali,

    Sure, I'm going to take an example from a cross-platform game that I'm hoping to release to the community (if I ever get the time to finish it).

    So in my app delegate (AppDelegate.cs), I'm defining a public computed property:

    [Register ("AppDelegate")]
    public class AppDelegate : UIApplicationDelegate
    {
        #region Computed Properties
        public override UIWindow Window { get; set; }
        public ONCardGame CurrentGame { get; set; } = new ONCardGame();
        #endregion
        ...
    }
    

    Later, in any part of the app that I want to access the CurrentGame property, I add the following "shortcut" to my class:

    public partial class GameBoardView : UIView
    {
        #region Application Access
        public static AppDelegate App {
            get { return (AppDelegate)UIApplication.SharedApplication.Delegate; }
        }
        #endregion
        ...
    }
    

    And within the class I can call App.CurrentGame to access any property of the ONCardGame instance.

    Please let me know if you still have any questions.

    Kevin

  • QasimAlbaqaliQasimAlbaqali USMember

    @KMullins said:
    Hi @QasimAlbaqali,

    Sure, I'm going to take an example from a cross-platform game that I'm hoping to release to the community (if I ever get the time to finish it).

    So in my app delegate (AppDelegate.cs), I'm defining a public computed property:

    [Register ("AppDelegate")]
    public class AppDelegate : UIApplicationDelegate
    {
    #region Computed Properties
    public override UIWindow Window { get; set; }
    public ONCardGame CurrentGame { get; set; } = new ONCardGame();
    #endregion
    ...
    }

    Later, in any part of the app that I want to access the CurrentGame property, I add the following "shortcut" to my class:

    public partial class GameBoardView : UIView
    {
    #region Application Access
    public static AppDelegate App {
    get { return (AppDelegate)UIApplication.SharedApplication.Delegate; }
    }
    #endregion
    ...
    }

    And within the class I can call App.CurrentGame to access any property of the ONCardGame instance.

    Please let me know if you still have any questions.

    Kevin

    Thank you! Your solution worked for me and did what I exactly wanted!

Sign In or Register to comment.