MVVM Pattern without XAML and Fody in Xamarin?

GuiWaltrickeGuiWaltricke BRMember ✭✭
edited September 2015 in Xamarin.Forms

Hello everyone,

i would like to know about MVVM pattern without XAML, i dont feel corfortable using XAML, so you guys have experience with MVVM without XAML?

i found only this tutorial: https://github.com/MvvmCross/MvvmCross-Forms

in MVVMCross they have an example without XAML but is like strange...

another problem is about INotifyPropertyChanged i have to do this for each property:

string familyName;
    public string FamilyName
    {
        get { return familyName; }
        set 
        {
            if (value != familyName)
            {
                familyName = value;
                OnPropertyChanged("FamilyName");
                OnPropertyChanged("FullName");
            }
        }
    }

there are another simple way and clean? someone know how to use this: https://github.com/Fody/PropertyChanged
or another easy way?

i am open to ideas and personal experience, thank you all

Posts

  • DanielLDanielL PLInsider ✭✭✭✭
    edited September 2015

    You could look at: http://daniel-luberda.github.io/20150922/Page-Factory-MVVM-library-for-Xamarin-Forms/#table-of-contents (5th chapter)
    I made it for maximum simplicity and to be dependency free. There're examples there how to use INotifyPropertyChanged implementation.

    BaseNotifyPropertyChanged.cs

    BaseViewModel.cs (adds Dictionary based SetField, GetField methods - no private fields needed)

    Just put BaseNotifyPropertyChanged.cs inside you project or download dependency free nuget from here.

    Fody is really easy. Just install nuget and don't forget to add CodyWeavers.xml to your project (Xamarin studio won't do that, Visual Studio does):

    <?xml version="1.0" encoding="utf-8" ?>
    <Weavers>
      <PropertyChanged/>  
    </Weavers>
    

    All INotifyPropertyChanged classes will be implemented automatically when you build your project. Just use auto setters, getters:

    public string Something { get; set; } - it will be converted when building

  • GuiWaltrickeGuiWaltricke BRMember ✭✭

    Thanks @DanielL

    I will check PageFactory...

    about Fody, the part that i have to add CodyWeavers.xml is that i have only to create a fila in my project and all done?

    i am using VS2013

  • DanielLDanielL PLInsider ✭✭✭✭

    @Guilherme Almost forgot! You have to set BuildAction to Content. Here's step by step guide: http://arteksoftware.com/fody-propertychanged-xamarin-studio/

    BTW: Fody is a great tool, I use it all the time!

  • GuiWaltrickeGuiWaltricke BRMember ✭✭

    @DanielL could you tell me why using Fody and using a IValueConverter in my entry the properychanged is not firing?

    myEntry.SetBinding(Entry.TextProperty, "MyProperty", BindingMode.TwoWay, converter: new GorduraCorporalConverter());
    

    using this when the BindingContext is updated but the screen is not updated, do you know why?

    thanks

  • DanielLDanielL PLInsider ✭✭✭✭
    edited November 2015

    @GuilhermeWaltricke

    Don’t forget to add “FodyWeavers.xml” file to you project (BuildAction set to Content):

    <?xml version="1.0" encoding="utf-8" ?>
    <Weavers>
      <PropertyChanged/>  
    </Weavers>
    

    It's really important.

  • GuiWaltrickeGuiWaltricke BRMember ✭✭

    Hello @DanielL in my project Fody is working good, i have FodyWeavers.xml and it is ok... the problem is using IValueConverter the event is not firing the Convert or ConvertBack

    thank you very much

  • DanielLDanielL PLInsider ✭✭✭✭

    Sorry! Didn't understand at first. Your code seems fine and should work if the property name is correct. Could you show me your converter code?

  • GuiWaltrickeGuiWaltricke BRMember ✭✭

    Hello Daniel, here is the code:

    The Entry:

    myEntry.SetBinding(Entry.TextProperty, ".", converter: new MyConverter());
    

    here is the converter:

    public class MyConverter : IValueConverter
        {
            #region Convert
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value == null) return "Indefinido";
    
                var composicao = (ComposicaoModel)value;
    
                return string.Format("{0} - {1}%", composicao.GorduraValor.ToString("N2"), composicao.GorduraPerc.ToString("N2"));
            }
            #endregion
    
            #region ConvertBack
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return "";
            }
            #endregion
        }
    

    Thanks

  • DanielLDanielL PLInsider ✭✭✭✭

    It looks fine to me. Did you try to put a breakpoint inside converter? Does it get into the converter methods?

  • GuiWaltrickeGuiWaltricke BRMember ✭✭

    Yes @DanielL when the view is created the converter is called normally and works but when the viewModelis updated the converter is not calling (firing the event). I put two breaking points inside the converter and anyone was called

    thanls

Sign In or Register to comment.