Git Product home page Git Product logo

mvvm.interactions's Introduction

Common MVVM Level Interactions(like open/save file) for WPF/UWP/WinUI/Uno/Avalonia/Maui platforms. Features:

  • Easy interactions with files, message boxes, web from MVVM level.
  • Enables drag-and-drop support at the ViewModel level.
  • Wide choice of platforms
  • Dependency injection friendly

NuGet

NuGet NuGet NuGet NuGet NuGet NuGet NuGet

Install-Package Mvvm.Interactions.Core
Install-Package Mvvm.Interactions.Wpf
Install-Package Mvvm.Interactions.Uno
Install-Package Mvvm.Interactions.Uwp
Install-Package Mvvm.Interactions.WinUI
Install-Package Mvvm.Interactions.Avalonia
Install-Package Mvvm.Interactions.Maui

Usage

Add to your App constructors:

public sealed partial class App
{
    private IHost AppHost { get; }

    public App()
    {
        AppHost = Host
            .CreateDefaultBuilder()
            .ConfigureServices(static services =>
            {
                // Add all available interactions
                services.AddMvvmInteractions();
                
                // or add only what you need
                services.AddSingleton<IFileInteractions, FileInteractions>();
                services.AddSingleton<IMessageInteractions, MessageInteractions>();
                services.AddSingleton<IWebInteractions, WebInteractions>();
            })
            .Build();

        // Optional. Displays unhandled exceptions using MessageInteractions.Exception.
        AppHost.Services.GetRequiredService<IMessageInteractions>().CatchUnhandledExceptions(this);

        // your code
    }
}

FileInteractions

// Open
var file = await FileInteractions.OpenFileAsync(new OpenFileArguments
{
    SuggestedFileName = "my.txt",
    Extensions = new[] { ".txt" },
    FilterName = "My txt files",
});
if (file == null)
{
    return;
}
var text = await file.ReadTextAsync().ConfigureAwait(true);

// Save (if you need to save file from previuos step)
await file.WriteTextAsync(text).ConfigureAwait(false);

// Save As
var file = await FileInteractions.SaveFileAsync(new SaveFileArguments(".txt")
{
    SuggestedFileName = "my.txt",
    FilterName = "My txt files",
});
if (file == null)
{
    return;
}
await file.WriteTextAsync(text).ConfigureAwait(false);

MessageInteractions

await MessageInteractions.ShowMessageAsync("Message");
await MessageInteractions.ShowWarningAsync("Warning");
await MessageInteractions.ShowExceptionAsync(new InvalidOperationException("Exception"));
bool question = await MessageInteractions.ShowQuestionAsync(new QuestionData("Are you sure?"));

WinUI requires a window to display the ContentDialog, so you'll need to set it explicitly in your App.OnLaunched:

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
#if HAS_WINUI
    var window = new Window();
    MessageInteractions.Window = window;
#endif
}

WebInteractions

await WebInteractions.OpenUrlAsync("https://www.google.com/");

DragAndDropExtensions

// WPF
xmlns:dragAndDrop="clr-namespace:Mvvm.Interactions;assembly=Mvvm.Interactions.Wpf" 
// UWP/Uno
xmlns:dragAndDrop="using:Mvvm.Interactions"
<Element
    AllowDrop="True"
    dragAndDrop:DragAndDropExtensions.DragFilesEnterCommand="{Binding DragFilesEnter}"
    dragAndDrop:DragAndDropExtensions.DragTextEnterCommand="{Binding DragTextEnter}"
    dragAndDrop:DragAndDropExtensions.DragLeaveCommand="{Binding DragLeave}"
    dragAndDrop:DragAndDropExtensions.DropFilesCommand="{Binding DropFiles}"
    dragAndDrop:DragAndDropExtensions.DropTextCommand="{Binding DropText}"
    >

Command arguments: DragFilesEnterCommand - FileData[] - Array of files.
DragTextEnterCommand - string - Text.
DragLeaveCommand - null.
DropFilesCommand - FileData[] - Array of files.
DropTextCommand - string - Text.

Contacts

mvvm.interactions's People

Contributors

dependabot[bot] avatar havendv avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

Forkers

0xdo

mvvm.interactions's Issues

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.