rid00z / FreshMvvm

FreshMvvm is a super light Mvvm Framework designed specifically for Xamarin.Forms. It's designed to be Easy, Simple and Flexible.
Apache License 2.0
597 stars 173 forks source link

Can't bind to Command. Am I doing something wrong? #173

Open SerNikola opened 7 years ago

SerNikola commented 7 years ago

Hello,

First of all, very nice framework Michael, I enjoy the simplicity of it.

However, I am very new to your framework and Xamarin in general. In fact I am just starting my first project, and for hours I can't bind a Button - or -SelectedItem event on a ListView.

Here is my page, and as you can se it's a carousel page:

`<?xml version="1.0" encoding="utf-8" ?> <CarouselPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="NovTest.Pages.ListDailyMealPlanPage" ItemsSource="{Binding DailyMealPlans}"

`

Here is a pic for a better view

And here is the PageModel:

public class ListDailyMealPlanPageModel : FreshBasePageModel

{

    public ObservableCollection<DailyMealPlan> DailyMealPlans { get; set; }

    Realm realm;

    public Command TheCommand
    {
        get
        {
            return new Command(() =>
            {

                Debug.WriteLine("Fired");
            });
        }
    }
    public Recipe SelectedItem
    {
        get
        {
            return null;
        }
        set {
            Debug.WriteLine("Value is" + value.ToString());
        }
    }
}

The app goes in break mode before the view is rendered. I was wondering, maybe I am doing something completely wrong, like with carousel page binding or I don't know.

Before trying to bind with command/event, all my binds to the properties worked as I expected. Only when I add {Binding SomeCommand} to an element, the app goes in breakmode.

Here is the stack trace for convenience:


07-20 12:16:45.329 D/Mono    ( 9707): Assembly Loader probing location: '/storage/emulated/0/Android/data/NovTest.Android/files/.__override__/Realm.dll'.
07-20 12:16:45.329 D/Mono    ( 9707): AOT: image '/storage/emulated/0/Android/data/NovTest.Android/files/.__override__/Realm.dll.so' not found: dlopen failed: library "/data/app/NovTest.Android-1/lib/x86/libaot-Realm.dll.so" not found
07-20 12:16:45.330 D/Mono    ( 9707): AOT: image '/usr/local/lib/mono/aot-cache/x86/Realm.dll.so' not found: dlopen failed: library "/data/app/NovTest.Android-1/lib/x86/libaot-Realm.dll.so" not found
07-20 12:16:45.330 D/Mono    ( 9707): Assembly Ref addref Realm.DataBinding[0xab2dd0a0] -> Realm[0xab2dcb00]: 3
07-20 12:16:45.339 D/Mono    ( 9707): Unloading image System.Linq.dll [0x9b626a00].
07-20 12:16:45.339 D/Mono    ( 9707): Image addref System.Linq[0x9cf99fe0] -> System.Linq.dll[0x9d995d00]: 8
07-20 12:16:45.339 D/Mono    ( 9707): Config attempting to parse: 'System.Linq.dll.config'.
07-20 12:16:45.339 D/Mono    ( 9707): Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Linq/System.Linq.config'.
07-20 12:16:45.339 D/Mono    ( 9707): Assembly Ref addref NovTest[0xab2ddd00] -> System.Linq[0x9d95a300]: 6
07-20 12:16:45.340 D/Mono    ( 9707): Image addref System.Linq.Queryable[0x9cf99fe0] -> System.Linq.Queryable.dll[0x9b626a00]: 2
07-20 12:16:45.340 D/Mono    ( 9707): Prepared to set up assembly 'System.Linq.Queryable' (System.Linq.Queryable.dll)
07-20 12:16:45.340 D/Mono    ( 9707): Assembly System.Linq.Queryable[0x9cf99fe0] added to domain RootDomain, ref_count=1
07-20 12:16:45.340 D/Mono    ( 9707): AOT: image 'System.Linq.Queryable.dll.so' not found: dlopen failed: library "/data/app/NovTest.Android-1/lib/x86/libaot-System.Linq.Queryable.dll.so" not found
07-20 12:16:45.341 D/Mono    ( 9707): AOT: image '/usr/local/lib/mono/aot-cache/x86/System.Linq.Queryable.dll.so' not found: dlopen failed: library "/data/app/NovTest.Android-1/lib/x86/libaot-System.Linq.Queryable.dll.so" not found
07-20 12:16:45.341 D/Mono    ( 9707): Config attempting to parse: 'System.Linq.Queryable.dll.config'.
07-20 12:16:45.341 D/Mono    ( 9707): Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Linq.Queryable/System.Linq.Queryable.config'.
07-20 12:16:45.341 D/Mono    ( 9707): Assembly Ref addref NovTest[0xab2ddd00] -> System.Linq.Queryable[0x9cf99fe0]: 2
07-20 12:16:45.341 D/Mono    ( 9707): Assembly Ref addref System.Linq.Queryable[0x9cf99fe0] -> System.Core[0x9d95a420]: 8
Loaded assembly: System.Linq.Queryable.dll [External]
07-20 12:16:45.350 D/Mono    ( 9707): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
07-20 12:16:45.350 D/Mono    ( 9707): Searching for 'object_get_backlinks'.
07-20 12:16:45.350 D/Mono    ( 9707): Probing 'object_get_backlinks'.
07-20 12:16:45.350 D/Mono    ( 9707): Found as 'object_get_backlinks'.
07-20 12:16:45.360 D/Mono    ( 9707): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
07-20 12:16:45.360 D/Mono    ( 9707): Searching for 'results_add_notification_callback'.
07-20 12:16:45.360 D/Mono    ( 9707): Probing 'results_add_notification_callback'.
07-20 12:16:45.360 D/Mono    ( 9707): Found as 'results_add_notification_callback'.
07-20 12:16:45.401 D/Mono    ( 9707): DllImport searching in: '__Internal' ('(null)').
07-20 12:16:45.401 D/Mono    ( 9707): Searching for 'java_interop_jnienv_new_local_ref'.
07-20 12:16:45.401 D/Mono    ( 9707): Probing 'java_interop_jnienv_new_local_ref'.
07-20 12:16:45.401 D/Mono    ( 9707): Found as 'java_interop_jnienv_new_local_ref'.
07-20 12:16:45.419 W/art     ( 9707): JNI RegisterNativeMethods: attempt to register 0 native methods for md5b60ffeb829f638581ab2bb9b1a7f4f3f.Platform_DefaultRenderer
07-20 12:16:45.526 D/Mono    ( 9707): DllImport searching in: '__Internal' ('(null)').
07-20 12:16:45.526 D/Mono    ( 9707): Searching for 'java_interop_jnienv_call_nonvirtual_float_method'.
07-20 12:16:45.526 D/Mono    ( 9707): Probing 'java_interop_jnienv_call_nonvirtual_float_method'.
07-20 12:16:45.526 D/Mono    ( 9707): Found as 'java_interop_jnienv_call_nonvirtual_float_method'.
07-20 12:16:45.600 D/Mono    ( 9707): DllImport searching in: '__Internal' ('(null)').
07-20 12:16:45.600 D/Mono    ( 9707): Searching for 'java_interop_jnienv_call_nonvirtual_boolean_method'.
07-20 12:16:45.601 D/Mono    ( 9707): Probing 'java_interop_jnienv_call_nonvirtual_boolean_method'.
07-20 12:16:45.601 D/Mono    ( 9707): Found as 'java_interop_jnienv_call_nonvirtual_boolean_method'.
07-20 12:16:45.611 W/FragmentManager( 9707): moveToState: Fragment state for FragmentContainer{a65f52e #1 id=0x1 android:switcher:1:713632936} not updated inline; expected state 3 found 2
07-20 12:16:45.631 D/Mono    ( 9707): Assembly Ref addref Realm[0xab2dcb00] -> System.Runtime.CompilerServices.Unsafe[0xab2dcaa0]: 2
07-20 12:16:45.684 D/Mono    ( 9707): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
07-20 12:16:45.684 D/Mono    ( 9707): Searching for 'object_get_string'.
07-20 12:16:45.684 D/Mono    ( 9707): Probing 'object_get_string'.
07-20 12:16:45.684 D/Mono    ( 9707): Found as 'object_get_string'.
07-20 12:16:45.690 D/Mono    ( 9707): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
07-20 12:16:45.690 D/Mono    ( 9707): Searching for 'object_get_double'.
07-20 12:16:45.690 D/Mono    ( 9707): Probing 'object_get_double'.
07-20 12:16:45.690 D/Mono    ( 9707): Found as 'object_get_double'.
InspectorDebugSession(43): HandleTargetEvent: UnhandledException````

I can see **Realm** is mentioned lots of times, but I promise nothing Realm related happens.
I even tried with the simple "The Command" command, initialized in the PageModel. I bound it to a button and the same stack trace occurred.

Let me know if you need more info.
Thank you for your time,

------
Edit: Grammar and formatting.
SerNikola commented 7 years ago

I discovered that for this to work, I had to move the SelectedItem Command in the Meal class. That's because the ItemsSource for the ListView is the collection of meals.

While this solves my problem, it's far from clean code, or MVVM for that matter. With this solution I am forced to have VM code (Commands) in the Model.

What's the workaround of this? Is it MVVM friendly to have a ViewModel with an ObservableCollection inside that will be used as binding property?

Like this:

ViewModels:

public class MainViewModel : FreshBasePageModel 
{
    public ObservableCollection<OtherViewModel> OtherCollection { get; set; }
}

public class OtherViewModel: FreshBasePageModel 
{
    public Command SomeCommand { get; set; }
}

And use it like this in the Views:

<ContentPage>
    <ListView ItemsSource={Binding OtherCollection} SelectedItem={Binding SomeCommand}>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>

This seems Okay to me, but this is my first ever MVVM project, and I wonder if this is how you do stuff.

Thanks a lot for your time,