Event To Comand in Xamarin Forms project using .NET Standards

Thierry.8340Thierry.8340 ✭✭USMember ✭✭

Hi,

I was wondering if there was an equivalent to the MVVMLight EventToCommand available when using a Xamarin Forms project that's using .NET Standards?

I found the following article: h t t p : / / blog.xamarin.com / turn-events-into-commands-with-behaviors, which basically shows you how to create a ListView behaviour to handle such a requirement but I don't like the idea as I would have to create a behaviour for every control type that require the "EventToCommand".

In the past, I would have used something similar to the below with the Microsoft Interactivity and MVVMLight libraries:

<i:Interaction.Triggers>



</i:Interaction.Triggers>

or with arguments

<i:Interaction.Triggers>



</i:Interaction.Triggers>

That's just 2 examples but I use this mechanism on many controls such as ListViews, Grids, TreeViews, etc... and while I know many of these won't be used in a Xamarin Forms project, I'd still prefer this scenario over the one where I'd have to create a Behaviour for each control and their corresponding events.

I'd like to know if there is an equivalent in Xamarin forms to this feature or if you know how to use this with the new mvvmlight .net standard library

Many thanks.

Posts

  • RCarvalhoRCarvalho ✭✭ PTMember ✭✭

    Were you able to use Service Locator with the new MvvmLight library?

  • Thierry.8340Thierry.8340 ✭✭ USMember ✭✭

    This is a little bit off topic as I was looking for info on how to deal with EventToCommand but here's what I know and did.

    The ServiceLocator is no longer part of the .net Standard MvvmLight Library.

    Check
    h t t p ://www.mvvmlight.net/std10 for more details
    or
    h t t p s://forums.xamarin.com/discussion/105733/servicelocator-netstandard-library-2-0-0-simpleioc-default-not-valid.

    I simply created a ViewModelLocator class where I'm defining whatever model I need to access

        static ViewModelLocator()
        {
            // register services
            if (UseMockDataStore)
                SimpleIoc.Default.Register<IDataStore<Item>>(() => new MockDataStore());
            else
                SimpleIoc.Default.Register<IDataStore<Item>>(() => new AzureDataStore());
    
            if (UseMockDataStore)
                SimpleIoc.Default.Register<IDataService>(() => new MockDataService());
            else
                SimpleIoc.Default.Register<IDataService>(() => new AzureDataService());
    
            // register view's viewmodels
            SimpleIoc.Default.Register<MainPageViewModel>();
            SimpleIoc.Default.Register<ItemsPageViewModel>();
            SimpleIoc.Default.Register<BuildingListPageViewModel>();
        }
    

    and in my app.xaml, I simply declared

            <vm:ViewModelLocator x:Key="Locator" />
    

    and I can then access any of my ViewModels or I can inject them into my views as normal.

    Hope this helps.

  • Thierry.8340Thierry.8340 ✭✭ USMember ✭✭

    Anyone has any ideas on my EventToCommand question?

    I really would like to know how to best handle this as this is a crucial part of handling a lot various scenarios when using MVVM with specific controls.

    Any help would be greatly appreciated.

  • abraabra ✭✭✭ ADMember ✭✭✭

    @Thierry.8340 : take a look here

  • Thierry.8340Thierry.8340 ✭✭ USMember ✭✭

    @abra, Thanks for that.

    I've read through the article but don't see anything that mentions EventToCommand equivalent. I'm currently looking at the GitHub repository to see if there is any help/documentation regarding this feature as I don't know Codon.

    Do you know if it supports it? If I find anything, I'll update this post.

    Thanks.

    Thierry

  • abraabra ✭✭✭ ADMember ✭✭✭

    @Thierry: I guess ActionCommand would be the equivalent, did you take a look at it ? Maybe you can find a usage of it in the sample

Sign In or Register to comment.