{"id":19762555,"url":"https://github.com/dotnet-ad/mvvmicro","last_synced_at":"2025-04-30T14:30:57.729Z","repository":{"id":81440670,"uuid":"88717432","full_name":"dotnet-ad/Mvvmicro","owner":"dotnet-ad","description":"Minimalist MVVM framework for .NET.","archived":false,"fork":false,"pushed_at":"2019-07-17T15:07:18.000Z","size":96,"stargazers_count":22,"open_issues_count":1,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-11T06:29:27.465Z","etag":null,"topics":["commands","lightweight","micro","mvvm","navigation","xamarin"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dotnet-ad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-19T07:57:50.000Z","updated_at":"2023-06-21T02:11:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"a3ec85e5-ecae-4a7f-b5bb-c1aa9e5bd817","html_url":"https://github.com/dotnet-ad/Mvvmicro","commit_stats":null,"previous_names":["aloisdeniel/mvvmicro"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnet-ad%2FMvvmicro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnet-ad%2FMvvmicro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnet-ad%2FMvvmicro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnet-ad%2FMvvmicro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dotnet-ad","download_url":"https://codeload.github.com/dotnet-ad/Mvvmicro/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224214146,"owners_count":17274524,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["commands","lightweight","micro","mvvm","navigation","xamarin"],"created_at":"2024-11-12T04:05:05.243Z","updated_at":"2024-11-12T04:05:07.266Z","avatar_url":"https://github.com/dotnet-ad.png","language":"C#","readme":"![Schema](./Documentation/Logo.png)\n\n[![NuGet](https://img.shields.io/nuget/v/Mvvmicro.svg?label=NuGet)](https://www.nuget.org/packages/Mvvmicro/) [![Donate](https://img.shields.io/badge/donate-paypal-yellow.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations\u0026business=ZJZKXPPGBKKAY\u0026lc=US\u0026item_name=GitHub\u0026item_number=0000001\u0026currency_code=USD\u0026bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted)\n\nMinimalist MVVM framework for .NET.\n\n## Install\n\nAvailable on [NuGet](https://www.nuget.org/packages/Mvvmicro/).\n\n## Usage\n\n### Observable\n\nA base implementation of `INotifyPropertyChanged` with helper method for raising property changes.\n\n* `Set\u003cT\u003e(ref T field, T newValue, [auto] string propertyName)` : sets the value of the referenced field with the given new value and raises `PropertyChanged` even if its different value that the current one. An `Assignment\u003cT\u003e` will be returned to indicate the change status with `HasChanged`. If the value changed, this object also allows you in to raise other linked properties or commands that depends on the current one with `ThenRaise`.\n\n```csharp\npublic class HomeViewModel : Observable\n{\n    private string firstname, lastname;\n    \n    public string Firstname\n    {\n    \tget =\u003e this.firstname;\n    \tset =\u003e this.Set(ref this.firstname, value).ThenRaise(() =\u003e this.Fullname);\n    }\n    \n    public string Lastname\n    {\n    \tget =\u003e this.lastname;\n    \tset =\u003e this.Set(ref this.lastname, value).ThenRaise(() =\u003e this.Fullname);\n    }\n    \n    public string Fullname =\u003e $\"{Firstname} {Lastname}\";\n    \n}\n```\n\n### ViewModelBase\n\nAn `Observable` object that adds navigation elements. This should be the base class for the view models associated to your application screens. A `NavigationRequested` event is available for your view to subscribe and react to.\n\n```csharp\npublic class HomeViewModel : ViewModelBase\n{\n\t//...\n\t\n    private void NavigateDetail(int id)\n    {\n    \t// \"/DayViewModel?id=467674\"\n    \tthis.Navigate\u003cDayViewModel\u003e((s) =\u003e\n\t\t{\n\t\t\ts.Query.Set(day.Identifier, \"id\");\n\t\t});\n    }\n}\n```\n\n```csharp\npublic class HomeViewController : UIViewController\n{\n\t// ...\n\n\tpublic HomeViewModel ViewModel { get; } = new HomeViewModel();\n\n\tpublic override void ViewDidAppear(bool animated)\n\t{\n\t\tbase.ViewDidAppear(animated);\n\t\tthis.ViewModel.NavigationRequested += OnNavigation;\n\t}\n\t\n\tpublic override void ViewWillDisappear(bool animated)\n\t{\n\t\tbase.ViewDidAppear(animated);\n\t\tthis.ViewModel.NavigationRequested -= OnNavigation;\n\t}\n\t\n\tprivate void OnNavigation(object sender, NavigationArgs nav)\n\t{\n\t\tthis.PerformSegue(nav.Segment.Value, this);\n\t}\n}\n```\n\n### Commands\n\nThe `(Async)RelayCommand` classes are helpers for creating `ICommand` synchronous and asynchronous implementations on the fly from methods. An alternate version `(Async)RelayCommand\u003cT\u003e` with a typed parameterer is also provided. \n\nThe advantages of using `IAsyncRelayCommand` is :\n\n* **Executing state** : `IsExecuting` property, that can be bound for updating your UI (activity indicators and so on).\n* **Managing lifecycle** : `LastSuccededExecution` property, for managing lifecycle of display last successful execution time.\n* **Failure management** : `ExecutionFailed` event, thrown whenever a\n* **Cancellation** : `Cancel()` call will cancel the tokens given as a parameter of the execution method. \n* **Single execution** : when already running, a new execution can't be triggered.\n\n**Example:**\n\n```csharp\npublic class HomeViewModel : ViewModelBase\n{\n    public HomeViewModel()\n    {\n    \tthis.UpdateCommand = new AsyncRelayCommand(ExecuteUpdateCommandAsync);\n    }\n    \n    public AsyncRelayCommand UpdateCommand { get; }\n    \n    private Task ExecuteUpdateCommandAsync(CancellationToken token)\n    {\n        // ...\n    }\n}\n```\n\n### Dependency container\n\nA deliberately basic container is provided for managing your dependencies. Declarations may seem too verbose compared to other popular IoC frameworks (*Autofac, Ninject, ...*), but **it doesn't rely on any reflection (which can has a cost in a mobile environment)** for injecting instances into newly created instances.\n\n```csharp\nContainer.Default.Register\u003cIApi\u003e((c) =\u003e new WebApi(), isInstance: true); // A unique instance for entire lifecycle\nContainer.Default.Register((c) =\u003e new HomeViewModel(c.Get\u003cIApi\u003e())); // a new instance is created each time the type is requested\n\n// ...\n\nvar homeViewModel = Container.Default.Get\u003cHomeViewModel\u003e();\n```\n\n### Observers\n\nSometimes you wish to listen to observable property changes : if you subscribe to `PropertyChanged` event, memory leaks could append since your observable will keep a reference to your observer in order to notify it. \n\nThis is often solved by subscribing to this event when your observer becomes active (*i.e. when your page become visible, like `OnResume` in an Android activity*) and unsubcribing to it when the observable becomes inactive (*i.e. when your page disappears, like `OnPause` in an Android activity*). But what happens if your observable triggers property changes while your observer is inactive : **the pending modifications are never applied to it**! This can be deactivated through `ShouldTriggerPendingChanges` property.\n\nThe observer also manages initial values by triggering a change on all registered properties the first time. This can be deactivated through `ShouldTriggerInitialValues` property.\n\nThe `NotifyPropertyObserver` solves those requirements :\n\n```csharp\npublic partial class HomeViewController : UIViewController\n{\n    public HomeViewController(IntPtr ptr) : base(ptr)\n    {\n       this.ViewModel = new HomeViewModel();\n       this.observer = this.ViewModel.CreateObserver(this);\n    }\n\n    public HomeViewModel ViewModel { get; }\n\n    private NotifyPropertyObserver\u003cHomeViewController, HomeViewModel\u003e observer;\n\n    public override void ViewWillAppear(bool animated)\n    {\n        base.ViewWillAppear(animated);\n\n        this.observer\n            .Observe(vm =\u003e vm.Fullname, (vm, value) =\u003e this.Title = value)\n            .Start();\n    }\n\n    public override void ViewDidDisappear(bool animated)\n    {\n        base.ViewDidDisappear(animated);\n        \n        this.observer.Stop();\n    }\n}\n```\n\n## Complementary tools\n\nBellow, you will find a list of tools that can be used in combination with **Mvvmicro** to build great mobile or desktop application projects.\n\n* [AutoFindViews](https://github.com/aloisdeniel/AutoFindViews) : Auto extract Xamarin.Android layout elements from declared identifiers.\n* [StaticBind](https://github.com/aloisdeniel/StaticBind) : View data bindings for Xamarin.\n* [Refit](https://github.com/paulcbetts/refit) : Rest client implementation generator.\n* [LiteDB](http://www.litedb.org/) : Small embedded NoSQL database.\n* [Xamarin Plugins](https://github.com/xamarin/XamarinComponents) : Various abstractions for platform specific behaviours\n\n## Roadmap / Ideas\n\n* Create a Fody task for generating properties.\n\n## Why ?\n\nI decided to create this small framework because other alternatives offer often too much stuff for me and include unused or duplicated parts. I also often find their navigation model to be not enough flexible. Finally, it's also a good starting point to learn MVVM to students. That's why this framework includes only the minimal bits I need for the majority of my developments.\n\n## Contributions\n\nContributions are welcome! If you find a bug please report it and if you want a feature please report it.\n\nIf you want to contribute code please file an issue and create a branch off of the current dev branch and file a pull request.\n\n### License\n\n![MIT © Aloïs](https://img.shields.io/badge/licence-MIT-blue.svg) \n\n© [Aloïs Deniel](http://aloisdeniel.github.io)\n","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_donations\u0026business=ZJZKXPPGBKKAY\u0026lc=US\u0026item_name=GitHub\u0026item_number=0000001\u0026currency_code=USD\u0026bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdotnet-ad%2Fmvvmicro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdotnet-ad%2Fmvvmicro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdotnet-ad%2Fmvvmicro/lists"}