.Net Maui ViewModel and MVVM

Tom Meier 140 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 !

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,960 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,356 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,871 Reputation points Microsoft Vendor
    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