GitHub - mrousavy/Jellyfish: 🐟 An incredibly lightweight and type safe MVVM library for .NET WPF, Silverlight, Xamarin and UWP (original) (raw)
Jellyfish
🐟
An incredibly light and type safe MVVM library for .NET WPF, Silverlight, Xamarin and UWP
Jellyfish is on NuGet:
PM> Install-Package Jellyfish
Make sure to also check out the Jellyfish Visual Studio Extension 📦!
Compared to other MVVM Frameworks like MVVM Light, Prism or Caliburn.Micro, this framework is
- as light as possible
- using modern best-practices
- using modern code style
- using little to no runtime reflection to be as fast as possible
- exactly fitting my needs
Usage
For description, documentation and usage, please view the Jellyfish wiki 📖 or the Getting Started guide 📖. For usage-example projects, please see Jellyfish.Demo or GameFinder.
📝 View Models
Every ViewModel needs to implement the ViewModel class:
public class LoginViewModel : ViewModel { private User _user; public User User { get => _user; set => Set(ref _user, value); } }
See View Models 📖
⚡ Commands
The RelayCommand is an ICommand implementation.
<Window ...>
Initialize the ICommand
with a non-generic RelayCommand
instance and the given action/callback:
ICommand LoginCommand = new RelayCommand(LoginAction, CanLogin); // ... void LoginAction(object parameter) { ... } bool CanLogin(object parameter) { ... }
See Commands 📖
💉 Dependency Injection
Provide dependencies for types using the IInjector
At app startup:
Injector.Register(() => new User("John", "Smith")); Injector.Register(() => OpenDatabaseService(username, password));
Some ViewModel:
class LoginViewModel : ViewModel { IUser User { get; set; } IDatabaseService _service;
LoginViewModel()
{
this.Inject();
}
}
💾 Enums
The enum binding source extension allows for better binding support on enums.
Just use the EnumBindingSource extension to bind an enum to any ItemsSource
:
See Enums 📖
⚙️ Preferences
An abstract class definition for any application Preferences.
public class DemoPreferences : Preferences { public int SomeInt { get; set; } = 400; public string SomeString { get; set; } = "test string"; public bool SomeBool { get; set; } = false;
public object SomeObject { get; set; } = new
{
Name = "Marc",
IsValid = true
};
}
See Preferences 📖
🔔 Feeds
The IFeed allows notifying any subscribers in this feed about sudden changes within the application domain in realtime.
class CustomViewModel : INode { public CustomViewModel { this.Subscribe(); }
public void MessageReceived(NotifyReason reason)
{ ... }
}
Feed.Notify(NotifyReason.RefreshView);
See Feeds 📖
Results
With Jellyfish
public class LoginViewModel : ViewModel { private User _user; public User User { get => _user; set => Set(ref _user, value); } }
Without Jellyfish
public class LoginViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _username;
public string Username
{
get
{
return _username;
}
set
{
_username = value;
OnPropertyChanged("Username");
}
}
}