Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maxumka/nob
Not boilerplate in WPF MVVM
https://github.com/maxumka/nob
boilerplate csharp dotnet mvvm sourcegenerator wpf
Last synced: 13 days ago
JSON representation
Not boilerplate in WPF MVVM
- Host: GitHub
- URL: https://github.com/maxumka/nob
- Owner: Maxumka
- License: gpl-3.0
- Created: 2021-06-29T06:47:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-07-12T09:51:35.000Z (over 3 years ago)
- Last Synced: 2024-10-24T12:12:40.021Z (3 months ago)
- Topics: boilerplate, csharp, dotnet, mvvm, sourcegenerator, wpf
- Language: C#
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# NOB
NOB - its a source generator that allows you not to write boilerplate code in the WPF.
## Features
1. Generate property
2. Generate INotifyPropertyChanged
3. Generate ICommand
4. Generate Command (only without parameters)
## Example
Make your viewmodel a partial class, add the viewmodel attribute to the class, the property attribute to the fields, the command attribute to the methods.
```CSharp
[ViewModel]
public partial class MainWindowViewModel
{
[Property]
private string text1;[Command]
private void ClearText1() => PropertyText1 = "";public MainWindowViewModel() { }
}
```
And this is what your viewmodel will become
```CSharp
using System;
using System.ComponentModel;
using System.Windows.Input;
using GeneratedCommand;
namespace GeneratedAttribute
{
[AttributeUsage(AttributeTargets.Class)]
public class ViewModelAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Field)]
public class PropertyAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Method)]
public class CommandAttribute : Attribute { }
}namespace GeneratedCommand
{
public class Command : ICommand
{
private readonly Action _execute;
private readonly Func _canExecute;
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public Command(Action execute, Func canExecute = null)
=> (_execute, _canExecute) = (execute, canExecute);
public bool CanExecute(object parameter) => true;
public void Execute(object parameter) => _execute();
}
}
namespace NOB.SampleProject.ViewModels
{
public partial class MainWindowViewModel : INotifyPropertyChanged
{
public string PropertyText1
{
get => text1;
set
{
text1 = value; OnPropertyChanged(nameof(PropertyText1));
}
}
private Command commandClearText1;
public Command CommandClearText1
{
get => new(ClearText1);
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new(propertyName));
}
}
}
```