Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any common pitfalls when using MvxView and MvxViewController in MvvmCross


Are there any common pitfalls when using MvxView and MvxViewController in MvvmCross


When using `MvxView` and `MvxViewController` in MvvmCross, several common pitfalls can arise. Understanding these issues can help developers avoid them and ensure smoother development.

1. Type Conversion Issues**

One common problem is type conversion. In the MVVM pattern, properties defined in the ViewModel might not be directly consumable by the UI. For example, an image property might be a byte array, but the UI expects it as an image source. MvvmCross provides the `IMvxValueConverter` interface to handle such conversions, but it requires casting, which can be cumbersome. The generic `MvxValueConverter` class helps by allowing you to specify input and output types explicitly[1].

2. Lifecycle Management**

Starting with MvvmCross 5.0, ViewModels are coupled to the lifecycle of the view, providing methods like `Appearing()`, `Appeared()`, `Disappearing()`, and `Disappeared()`. While these methods offer more control over the ViewModel's state, they are not 100% reliable and can be affected by platform-specific events or custom app logic[2][7]. Ensuring proper handling of these lifecycle events is crucial.

3. Binding Issues**

Binding issues can occur, especially when using custom views or controls. For instance, if a custom control is created before the ViewModel's data is available, the `DataContext` might be null. Using `DelayBind` can help resolve this by delaying the binding until the data is available[4].

4. Reusability of ViewModels**

ViewModels in MvvmCross, especially on iOS, can become tightly coupled to specific views, reducing their reusability. This happens when ViewModels handle view-specific logic, such as formatting labels or acting as data sources for table views. To avoid this, keep ViewModels focused on business logic and use converters or other mechanisms to handle view-specific transformations[3].

5. Platform-Specific Navigation**

MvvmCross supports cross-platform navigation, but each platform has unique presentation patterns. On iOS, for example, using storyboards for screen flow can conflict with MvvmCross's cross-platform approach. It's best to handle navigation logic in the ViewModel and use platform-specific presenters to manage how views are displayed[5][9].

6. Linker Safety**

MvvmCross is not linker-safe, meaning that parts of the framework might be stripped out by the linker during compilation. This can lead to runtime errors. To prevent this, you need to hint the linker to preserve necessary parts of the framework[6].

By being aware of these potential pitfalls, developers can better navigate the complexities of using `MvxView` and `MvxViewController` in MvvmCross applications.

Citations:
[1] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/august/windows-phone-build-mvvm-apps-with-xamarin-and-mvvmcross
[2] https://www.mvvmcross.com/mvvmcross-5-release/
[3] https://www.danielhall.io/the-problems-with-mvvm-on-ios
[4] https://stackoverflow.com/questions/56531225/problem-with-binding-to-mvxview-with-xamarin-ios-and-mvvmcross
[5] https://kerry.lothrop.de/ios-ui-with-mvvmcross/
[6] https://github.com/MvvmCross/MvvmCross/issues/4286
[7] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[8] https://stackoverflow.com/questions/45966067/mvvmcross-iphone-and-ipad-best-practices
[9] https://www.mvvmcross.com/documentation/fundamentals/view-presenters