migration
This commit is contained in:
91
SimpleToolkit.UI.WinUI.Behaviors/EventToCommandBehavior.cs
Normal file
91
SimpleToolkit.UI.WinUI.Behaviors/EventToCommandBehavior.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.Xaml.Interactivity;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace SimpleToolkit.UI.WinUI.Behaviors
|
||||
{
|
||||
public class EventToCommandBehavior : Behavior<FrameworkElement>
|
||||
{
|
||||
#region FIELDS
|
||||
|
||||
private Delegate _handler;
|
||||
private EventInfo _oldEvent;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PROPERTIES
|
||||
|
||||
public string Event { get { return (string)GetValue(EventProperty); } set { SetValue(EventProperty, value); } }
|
||||
public static readonly DependencyProperty EventProperty = DependencyProperty.Register("Event", typeof(string), typeof(EventToCommandBehavior), new PropertyMetadata(null, OnEventChanged));
|
||||
|
||||
public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } }
|
||||
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(EventToCommandBehavior), new PropertyMetadata(null));
|
||||
|
||||
public bool PassArguments { get { return (bool)GetValue(PassArgumentsProperty); } set { SetValue(PassArgumentsProperty, value); } }
|
||||
public static readonly DependencyProperty PassArgumentsProperty = DependencyProperty.Register("PassArguments", typeof(bool), typeof(EventToCommandBehavior), new PropertyMetadata(false));
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private static void OnEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
EventToCommandBehavior beh = (EventToCommandBehavior)d;
|
||||
|
||||
if (beh.AssociatedObject != null)
|
||||
{
|
||||
beh.AttachHandler((string)e.NewValue);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
AttachHandler(this.Event);
|
||||
}
|
||||
|
||||
private void AttachHandler(string eventName)
|
||||
{
|
||||
if (_oldEvent != null)
|
||||
{
|
||||
_oldEvent.RemoveEventHandler(this.AssociatedObject, _handler);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(eventName))
|
||||
{
|
||||
EventInfo ei = this.AssociatedObject.GetType().GetEvent(eventName);
|
||||
if (ei != null)
|
||||
{
|
||||
MethodInfo mi = this.GetType().GetMethod("ExecuteCommand", BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
_handler = Delegate.CreateDelegate(ei.EventHandlerType, this, mi);
|
||||
ei.AddEventHandler(this.AssociatedObject, _handler);
|
||||
_oldEvent = ei;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException(string.Format("The event '{0}' was not found on type '{1}'", eventName, this.AssociatedObject.GetType().Name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteCommand(object sender, object e)
|
||||
{
|
||||
object parameter = this.PassArguments ? e : null;
|
||||
if (this.Command != null && this.Command.CanExecute(parameter))
|
||||
{
|
||||
this.Command.Execute(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
56
SimpleToolkit.UI.WinUI.Behaviors/README.md
Normal file
56
SimpleToolkit.UI.WinUI.Behaviors/README.md
Normal file
@@ -0,0 +1,56 @@
|
||||
# SimpleToolkit.UI.WinUI.Behaviors
|
||||
|
||||
### Set of helpers, class extensions, UI controls used in my other C# projects
|
||||
|
||||
SimpleToolkit is package of useful classes, helpers, extensions and UI controls, I use in my C# projects. UI.WinUI.Behaviors subpackage contains XAML action triggers for WinUI applications.
|
||||
|
||||
---
|
||||
|
||||
## NuGet registry status
|
||||
|
||||
| Subpackage | Status |
|
||||
|--------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| **SimpleToolkit.UI.WinUI.Behaviors** | [](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Behaviors/) |
|
||||
| SimpleToolkit.Extensions | [](https://www.nuget.org/packages/SimpleToolkit.Extensions/) |
|
||||
| SimpleToolkit.Attributes | [](https://www.nuget.org/packages/SimpleToolkit.Attributes/) |
|
||||
| SimpleToolkit.MVVM | [](https://www.nuget.org/packages/SimpleToolkit.MVVM/) |
|
||||
| SimpleToolkit.UI.Models | [](https://www.nuget.org/packages/SimpleToolkit.UI.Models/) |
|
||||
| SimpleToolkit.UI.WinUI.Converters | [](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Converters/) |
|
||||
| SimpleToolkit.UI.WinUI.Controls | [](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Controls/) |
|
||||
| SimpleToolkit.UI.WinUI.Helpers | [](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Helpers/) |
|
||||
|
||||
## Features
|
||||
|
||||
- **EventToCommandBehavior** - allows to bind parametrized command to any event
|
||||
|
||||
## Dependencies
|
||||
|
||||
Dependencies should be installed automatically with this package
|
||||
|
||||
- Microsoft.Xaml.Behaviors.WinUI.Managed 2.0.9
|
||||
|
||||
## Installation and usage
|
||||
|
||||
You can download package from official NuGet registry or .nupkg file itself from Releases tab.
|
||||
|
||||
**CLI:**
|
||||
|
||||
```
|
||||
dotnet add package SimpleToolkit.UI.WinUI.Behaviors
|
||||
```
|
||||
|
||||
**Package reference in .csproj file:**
|
||||
|
||||
```
|
||||
<PackageReference Include="SimpleToolkit.UI.WinUI.Behaviors" Version="<version>" />
|
||||
```
|
||||
|
||||
## Attribution and contribution
|
||||
|
||||
This project is open source on MIT License, so you can just copy and upload again to your repository. But according to the license, you must include information about the original author. You can find license [here](https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.UI.WinUI.Behaviors/src/branch/main/LICENSE).
|
||||
|
||||
However, the preferred way to contribute would be to propose improvements in a pull request, through issues, or through other means of communication.
|
||||
|
||||
**Other sources:**
|
||||
|
||||
- Icon by [Icons8](icons8.com)
|
||||
@@ -0,0 +1,37 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
|
||||
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
|
||||
<RootNamespace>SimpleToolkit.UI.WinUI.Behaviors</RootNamespace>
|
||||
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
|
||||
<UseWinUI>true</UseWinUI>
|
||||
<UseRidGraph>true</UseRidGraph>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<EnableCoreMrtTooling Condition=" '$(BuildingInsideVisualStudio)' != 'true' ">false</EnableCoreMrtTooling>
|
||||
<Nullable>enable</Nullable>
|
||||
<Title>SimpleToolkit.UI.WinUI.Behaviors</Title>
|
||||
<Authors>Mateusz Skoczek</Authors>
|
||||
<Copyright>Mateusz Skoczek</Copyright>
|
||||
<PackageProjectUrl>https://repos.mateuszskoczek.com/SimpleToolkit/</PackageProjectUrl>
|
||||
<PackageLicenseUrl>https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.UI.WinUI.Behaviors/src/branch/main/LICENSE</PackageLicenseUrl>
|
||||
<PackageIcon>icon.png</PackageIcon>
|
||||
<RepositoryUrl>https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.UI.WinUI.Behaviors</RepositoryUrl>
|
||||
<Description>Set of helpers, class extensions, UI controls used in my other C# projects - XAML action triggers for WinUI applications</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="icon.png">
|
||||
<Pack>True</Pack>
|
||||
<PackagePath></PackagePath>
|
||||
</None>
|
||||
<None Update="README.md">
|
||||
<Pack>True</Pack>
|
||||
<PackagePath></PackagePath>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
BIN
SimpleToolkit.UI.WinUI.Behaviors/icon.png
Normal file
BIN
SimpleToolkit.UI.WinUI.Behaviors/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.7 KiB |
Reference in New Issue
Block a user