Forum Xamarin.iOS

NSTableViewDataSource vs Key-Value Coding (MVVM), which is a better approach?

GregCadmesGregCadmes USUniversity ✭✭

Quick intro... I'm writing an app for my company that supports iOS, Android, Windows, UWP, and finally getting around to the Mac platform.
App is nearly done for all other platforms except macOS. So far 80% code-reuse with iOS and Android, 60% Windows & UWP. Was hoping to leverage my ViewModel layer with macOS, but for obvious reasons, I cannot.

I came across this excellent xamarin resource for adding Data binding support to macOS apps. In that resource there is no mention of using a NSTableViewDataSource. By the way, all Xamarin Mac samples use a NSTableViewDataSource.

My question is, should I not even entertain the MVVM pattern for a macOS app, or should I utilize the NSTableViewDataSource approach? To me, it seems like the NSTableViewDataSource is akin to the classic WinForms code-behind methodology (for those of you who remember WinForms, pre-WPF days). In other words, limited extensibility and maintenance nightmares lie ahead.

I can't figure out how to databind my RelayCommand to the TableView's row double-click, but I can see how to do it using the NSTableViewDataSource, which at this point, leaning towards MVVM.

Any insight you have will be greatly appreciated.
thanks much,

Best Answer


  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    My tl;dr; personal opinion is if you are willing to spend the time burning your fingers with IB, comfortable enough with native crashes, and plan on showing non-trivial UI data binding is not a bad option. It can save you a ton of code in the long run.

    I've heard from many that once you "get it" it isn't that bad, though nowhere as nice as XAML.

    I have a honest distaste for anything related to IB so lean away from putting even more of my application there where possible. That might be a personal issue though. :)

  • GregCadmesGregCadmes USUniversity ✭✭

    Excellent insight, thank you for that.

    Personally, I loath IB.
    One of the devs on my team started the macOS project while I was finishing up the Xamarin Forms (iOS/Android) app. He knows IB very well, which explains the lack of Mvvm knowledge on that front.

    I don't mind learning how to use IB, albeit I do lose my patience trying to figure out how to do something super simple. Equally frustrating is the lack of async/await support for partial methods.

    Again, thank you kindly.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Equally frustrating is the lack of async/await support for partial methods.

    Explain? I'm not familiar with this issue.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai
    edited November 2017

    Here's a few "surviving IB tips" that I've come up with over my time with it:

    • Undo does not always work in all contexts. This is extremely frustrating when you have to recreate some visual tree or the like. Work in small chunks and git add them to your index as you go. That way, you can blow away your last 2 minutes instead of last 20.
    • Yes, you can in theory diff/merge storyboard/xib files, but be careful this is not XAML. For smaller teams / small projects consider using some form of exclusive lock (e-mail? spreadsheet?) so two people don't edit the same file extensively and bring you to merge hell.
    • There is a tradeoff between putting everything in one storyboard and having separate xib files for different parts of your UI. Fewer storyboards let you push more transition logic into it, so less code, but compound the merge/diff issue.
  • GregCadmesGregCadmes USUniversity ✭✭

    Regarding the lack of async/await support for partial methods...

    In Visual Studio for Mac, right clicking over a c# statement that appears to be in error shows the "Quick Fix" context menu. So clicking "Quick Fix" I see an option for "Make method async (stay void)".
    I chose that QF option, and it auto-fixed the method signature from this:

    partial void OnButtonClick(NSObject sender)

    To This:

    partial async void OnButtonClick(NSObject sender)

    Except this newly updated signature won't compile.
    So naturally, I assumed that the async/await pattern for partial methods was not possible with Xamarin.Mac/Visual Studio For Mac. (when in fact it's a VSFM refactoring bug)

    After digging around a little, I found that the CORRECT syntax is like so:

    async partial void OnButtonClick(NSObject sender)

    All I had to do was swap the position of the "async" and "partial" keywords. Who knew?
    I'm embarrassed to admit that, I should have known. After all, I use async/await everywhere, but never have I used a partial method. So I learned something new today. I wouldn't have if we didn't collaborate here. :-)

    Thanks again!

Sign In or Register to comment.