Home > OS >  Accessing properties from different class MAUI Android application
Accessing properties from different class MAUI Android application

Time:12-30

Testing the MAUI app dev by creating a simple Android application.

I've defined an collection and UI elements in MainPage.xaml/cs.

public ObservableCollection<Person> persons = new();

Also defined an broadcast Intent receiver.

internal class TestReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            // handle intent
        }
}

I want to access persons collection defined in MainPage and add items to it. There is so many pages defined what is what. App.xaml/Mainpage.xaml/MainActivity/AppShell. I'm a bit lost.

CodePudding user response:

Why broadcast receivers?

I suggest a different approach of your problem. Make a class. Lets call it PersonService. Put the collection there, add method to add/remove persons. Inject it as singleton, and use it across your app.

Tomorrow when you decide not to use some static list, but database (or web-api), all you have to do is edit it at one place.

Another alternative. Use messaging. CommunityToolkit.MAUI/MVVM have messaging system.

When you decide to go from Android to IOS? It will be much easier.

If you still want to do Android Broadcasts, please go ahead. But google search some of my alternatives. It saves me a lot of work every day, and works stable.

  • Related