WinUIStateBasedViewModel 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package WinUIStateBasedViewModel --version 1.0.2                
NuGet\Install-Package WinUIStateBasedViewModel -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WinUIStateBasedViewModel" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WinUIStateBasedViewModel --version 1.0.2                
#r "nuget: WinUIStateBasedViewModel, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install WinUIStateBasedViewModel as a Cake Addin
#addin nuget:?package=WinUIStateBasedViewModel&version=1.0.2

// Install WinUIStateBasedViewModel as a Cake Tool
#tool nuget:?package=WinUIStateBasedViewModel&version=1.0.2                

WinUI 3 Event-State Based ViewModel

This library simplifies the use of ViewModels in WinUI 3 projects, providing easy access to state flows and event handling.

Features

  • State Management: Manage state immutably using state flows.
  • Event Handling: Handle events effectively with a clear structure.
  • Simplified ViewModel: Provides a structured approach to ViewModel implementation in WinUI 3.

Installation

To install the library, add the following NuGet package to your project:

dotnet add package WinUIStateBasedViewModel

Usage

Define Your State

Create a class that extends ViewModelState and define the properties of your state.


using WinUIViewModel.Presentation.State;

public class TestState : ViewModelState
{
    public string Name { get; set; }
    public string Age { get; set; }

    public TestState(string name = null, string age = null)
    {
        Name = name;
        Age = age;
    }
}

Implement Your ViewModel

Create a ViewModel class that extends ViewModel<State, Event, Effect>. Implement the required methods and handle state updates and effects.

using WinUIViewModel.Presentation;
using WinUIViewModel.Presentation.State;

public class MyViewModel : ViewModel<TestState, MyEvent, MyEffect>
{
    public override TestState createInitialState()
    {
        return new TestState { Name = "DefaultName", Age = "DefaultAge" };
    }

    public override void handleEffect(MyEffect effect)
    {
        // Handle effect
    }

    public void UpdateAge(string newAge)
    {
        var newState = currentState.Copy(newValues =>
        {
            newValues.Age = newAge;
        });
        updateState(newState);
    }
}

Handling State and Events

Use the updateState method to update the state immutably. The sendEvent method can be used to handle events.

public void SomeMethod()
{
    // Update the age property of the state
    var newState = currentState.Copy(newValues =>
    {
        newValues.Age = "21";
    });
    updateState(newState);
}

Contributing

Contributions are welcome! Please fork the repository and submit a pull request with your changes.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net6.0-windows10.0.17763 is compatible.  net7.0-windows was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 114 6/25/2024
1.0.2 102 6/25/2024
1.0.1 101 6/25/2024
1.0.0 101 6/25/2024