Giter Site home page Giter Site logo

nkristek / smaragd Goto Github PK

View Code? Open in Web Editor NEW
34.0 2.0 6.0 1.09 MB

A platform-independent, lightweight library for developing .NET applications using the MVVM architecture

License: MIT License

C# 100.00%
mvvm-framework mvvm dotnet csharp nuget nuget-package dotnet-standard inotifypropertychanged viewmodel mvvm-architecture

smaragd's Introduction

Smaragd

CI Status NuGet version

This is a very lightweight library containing base classes for implementing .NET applications using the MVVM architecture. It is fully unit tested and platform independent.

Features

Smaragd offers base implementations of key .NET interfaces for building WPF / MVVM applications.

Core class diagram

In addition, it enables developers to:

For more information, please visit the documentation.

Installation

The recommended way to use this library is via NuGet.

Currently supported frameworks:

  • .NET Standard 2.0 or higher
  • .NET Framework 4.5 or higher

Quick Start

The following is a simple demonstration of some core features of Smaragd.

  1. Choose a base class for your ViewModel.

    • Inherit from ViewModel if you want to use the fill feature set (recommended)
    • Inherit from Bindable if you only want an implementation of INotifyPropertyChanged and INotifyPropertyChanging
    class AppViewModel : ViewModel
    {
        // ...
    }
  2. Add a property with a backing field that invokes PropertyChanged when set.

    class AppViewModel : ViewModel
    {
            private string _name;
            public string Name
            {
                get => _name;
                set => SetProperty(ref _name, value);
            }
    }
  3. Make the property dependent on the ViewModel's IsDirty property. IsDirty indicates whether property values have changed. The Name property then automatically updates observing views when IsDirty changes.

    class AppViewModel : ViewModel
    {
            private string _name;
    
            [PropertySource(nameof(IsDirty))]
            public string Name
            {
                get => IsDirty ? $"{_name} (unsaved changes)" : _name;
                set => SetProperty(ref _name, value);
            }
    }
  4. Add an async command to reset the IsDirty flag.

    class AppViewModel : ViewModel
    {
        private string _name;
    
        [PropertySource(nameof(IsDirty))]
        public string Name
        {
            get => IsDirty ? $"{_name} (unsaved changes)" : _name;
            set => SetProperty(ref _name, value);
        }
    
    
        private IViewModelCommand<AppViewModel> _saveCommand;
    
        [IsDirtyIgnored]
        [IsReadOnlyIgnored]
        public IViewModelCommand<AppViewModel> SaveCommand => _saveCommand ??= new SaveCommand(this)
    }
    
    
    class SaveCommand : AsyncViewModelCommand<AppViewModel>
    {
        public SaveCommand(AppViewModel context)
        {
            Context = context;
        }
    
        protected override async Task ExecuteAsync(AppViewModel viewModel, object parameter)
        {
            // SaveChanges(viewModel);
            viewModel.IsDirty = false;
        }
    }
  5. Create a view in XAML for your ViewModel and enjoy working with bindings.

    <Window Title="{Binding Name}">
        <Button Command="{Binding SaveCommand}">
    </Window>

In case you would like to see a more advanced reference application please don't hesitate to visit my other project Stein.

Why another MVVM library?

This library originated in my other project Stein and was subsequently moved to its own repository and nuget package. The goal is to provide a great yet minimal foundation which also promotes a good code style. Nearly everything is marked virtual (except events) so you can customize it to fit your needs.

And of course, this library is 🚀blazing fast🚀.

Contribution

If you find a bug feel free to open an issue. Contributions are also appreciated.

smaragd's People

Contributors

benruehl avatar nkristek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

smaragd's Issues

Having multiple INotifyCollectionChanged properties pointing to the same value will result in the PropertySourceAttribute not working correctly

private ObservableCollection<int> Values;

public ObservableCollection<int> Values1 => Values;

public ObservableCollection<int> Values2 => Values;

[PropertySource(nameof(Values1), NotifyCollectionChangedAction.Add, NotifyCollectionChangedAction.Remove, NotifyCollectionChangedAction.Replace, NotifyCollectionChangedAction.Reset)]
public int MinValue => Values1.Min();

[PropertySource(nameof(Values2), NotifyCollectionChangedAction.Add, NotifyCollectionChangedAction.Remove, NotifyCollectionChangedAction.Replace, NotifyCollectionChangedAction.Reset)]
public int MaxValue => Values2.Max();

This will result in either the PropertySourceAttribute from MinValue or MaxValue not working.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.