.Net Maui ViewModel and MVVM

Tom Meier 260 Reputation points
2024-05-07T20:27:49.38+00:00

Currently I have a shell application with a Tabbed Page with several Content Page Views. Instead of using one big Model (Populated with Data from a Database) in my ViewModel for all these Content Page Views, what is the best practice to separate everything out so every view has its own model. I am trying to follow the MVVM approach. If anyone one has an example project or tutorial it would be greatly appreciated. Thanks !

Developer technologies | .NET | .NET MAUI
Developer technologies | C#
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-05-08T05:33:40.72+00:00

    Hello,

    The official documentation provides best practices for setting up MVVM for individual pages. For your application scenario, there may be overlapping properties and methods between different ViewModels since you are involved in data manipulation.

    Therefore, you can build on the official documentation by first implementing a BaseViewModel in which the properties and methods needed for multiple pages are implemented. After implementing the BaseViewModel, you can then inherit the BaseViewModel for different pages to implement their corresponding ViewModels.

    There are two main benefits of doing this:

    1. Consolidate redundant properties and save code.
    2. Since the common properties are contained in a class, it is also easy to maintain and modify them.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.