FoggyFinder
Here is one of the common version of `ViewModelBase` class written in C#
```
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```
How to translate it to F#? Or, if it is impossible, what would be a good alternative?
Top Answer
FoggyFinder
Here's one way to achieve it:
```
open System.ComponentModel
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
[<AbstractClass>]
type ViewModelBase() =
let propertyChanged =
Event<PropertyChangedEventHandler, PropertyChangedEventArgs>()
interface INotifyPropertyChanged with
[<CLIEvent>]
member this.PropertyChanged = propertyChanged.Publish
member this.NotifyPropertyChanged([<CallerMemberName; Optional; DefaultParameterValue(null:string)>] propertyName) =
propertyChanged.Trigger(this, PropertyChangedEventArgs(propertyName))
```