Foxan
Foxan

Reputation: 21

Share data between compose custom native control and MAUI App

I have a MAUI app in which I'm creating and using custom native controls, using MAUI viewhandler. With it I'm able to map properties, and commands. But I need to share data like lists and dictionaries.

I managed to transmit it one way, and display a dictionary from my native view, but I can't figure out how I can observe the state of this dictionary. I want to be able to modify it from my MAUI code, like adding and removing elements from it, with the native view composables being recomposed.

I know that if I re-assign a new reference to the property, like creating a new dictionary from my dictionary in MAUI, removing the things I want to remove, and re-assign it again to my view property, it changes in my native view. But I'd like to do it in a cleaner way and more "compose" way. With for example a mutable state of something or a snapshot.

I tried to explore some ways but with no results.

Here are some snippets of my code:

the shared dictionary in my viewhandler

// **********************************dictionnary*****************************
public static readonly BindableProperty DictProperty =
    BindableProperty.Create(
        nameof(Dict),
        typeof(Dictionary<string, int>),
        typeof(CustomView),
        default(Dictionary<string, int>)
        );

public Dictionary<string, int> Dict
{
    get => (Dictionary<string, int>)GetValue(DictProperty);
    set => SetValue(DictProperty, value);
}
// **************************************************************************

the converter to kotlin compatible dictionnary:

public static IDictionary<string, int> ToJavaHashmap(IDictionary<string, int> dict)
{
    if (dict is null) return new Dictionary<string, Pair>();
    Dictionary<string, int> hashmap = [];
    foreach (KeyValuePair<string, int> elt in dict)
    {
        hashmap.Add(elt);
    }
    return hashmap;
}

Is there a way I could share a dictionary from my MAUI app with my native view, and also being able to observe changes in my native view so it recompose automatically ?

Upvotes: 1

Views: 34

Answers (0)

Related Questions