Binding to a Command and Type Safety

MREmeMREme Member ✭✭

Perhaps this is simply an uneducated question I am not sure. I use a number of Command Bindings similar to what is described here:
https://blog.xamarin.com/simplifying-events-with-commanding/

I like this approach quite a bit because it allows me to include virtually all code behind in the View Model. However, I am finding a few drawbacks that can sometimes cause problems.

1) When I bind a control in XAML the command (in this case "SquareRootCommand") does not refactor in Visual Studio with the attached view model. This causes problems when the command name gets renamed in the view model, leaving behind an orphaned control that no longer fires its intended command. Unfortunately, there are no compiler errors as I would have hoped. In fact it appears there not really any errors at run time either, Xamarin simply ignores the command.

2) Sometimes I forget to the declare the ICommand property in the view model as public. This again orphans the control because it can not reach the non public property. Again there are no compiler errors.
//Note the command is not public
ICommand SquareRootCommand { get; private set; }

Is this the intended behavior for Visual Studio or are there any plans to make the bound property/command in the XAML trigger a compiler error, similar to how an error is triggered if a XAML control EVENT is not properly found/wired up in the page code behind?

I understand the complexity that may exist since the design of the view model is not necessarily going to be defined the same way each time (it is not after all the code behind page file). However, is there an interface I should implement or a design practice that I should incorporate that may allow for more type safety in this regard?

Tagged:

Answers

  • theocangtheocang Member ✭✭

    That is a good reflexion, I have also this 'issues' and I think there don't exist better way to use Command.
    So just be careful during writing code.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    @MREme The Mfractor addin for VS has a lot of neat features which help to address this type of problem.

    I've been using it for a couple of months now, and it's improved my coding efficiency massively. From memory I can't remember if it addresses your points. I've emailed the author with a link to this post - I'm sure he will respond.

  • Matthew-RobbinsMatthew-Robbins AUInsider ✭✭

    Thanks for looping me in @JamesLavery 😊

    @MREme, as James has said we have features to address these exact two scenarios.

    MFractor includes XAML analysis that inspects for a range of data-binding issues, misspelt bindings and non-public property bindings being two of what it checks for:

    Misspelt Binding Detection:

    Non-Public Binding Detection:

    In addition to data-binding analysis, MFractor also suggests binding context properties and can generate them for you.

    The product helps a lot when working with Xamarin.Forms; I pride myself on creating incredible tools to make you more productive.

    Shoot me an email if you have any questions: [email protected]

  • MREmeMREme Member ✭✭

    This sounds like a great tool but is it only available on the Mac?

  • Matthew-RobbinsMatthew-Robbins AUInsider ✭✭

    Yes, MFractor is only available for Visual Studio Mac at the moment.

  • MREmeMREme Member ✭✭

    Ok thank you. I have to run on a Win 10 box.

Sign In or Register to comment.