Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
wi7a1ian / Converters.cs
Last active May 20, 2019 10:04
Set of useful converters that can be used in XAML #wpf #csharp
using MaterialDesignThemes.Wpf;
using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
@wi7a1ian
wi7a1ian / DeferredAction.cs
Last active March 18, 2019 10:04
Defers performing the action until after time elapses. Repeated calls will reschedule the action if it has not already been performed. Good for handling UI actions when someone has *Parkinson's disease* #wpf #csharp
sealed class DeferredAction : IDisposable
{
private Timer timer;
private bool disposed = false;
/// <summary>
/// Creates a new DeferredAction.
/// </summary>
/// <param name="action">
/// The action that will be deferred. It is not performed until after <see cref="Defer"/> is called.
@wi7a1ian
wi7a1ian / RelayCommand.cs
Last active August 6, 2019 08:34
RelayCommand or DelegateCommand #wpf #csharp
namespace Foo
{
public class RelayCommand<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Predicate<T> _canExecute;
public RelayCommand(Action<T> execute)
: this(execute, null)
{
@wi7a1ian
wi7a1ian / RoutedUICommandExample.cs
Created March 7, 2019 11:56
RoutedUICommand usage example #wpf #csharp
private static RoutedUICommand _pressMeCommand =
new RoutedUICommand("Press Me", "PressMe", typeof(MainWindow));
public static RoutedUICommand PressMeCommand
{
get { return _pressMeCommand; }
}
private void PressMe_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = CowboyCanTalk;
@wi7a1ian
wi7a1ian / DependencyPropertyExample.cs
Last active October 7, 2022 16:53
Example of DependencyProperty #wpf #csharp
public class DependencyPropertyExample
{
// Dependency Property
public static readonly DependencyProperty CurrentTimeProperty =
DependencyProperty.Register( "CurrentTime", typeof(DateTime),
typeof(MyClockControl),
new FrameworkPropertyMetadata( DateTime.Now,
OnCurrentTimePropertyChanged, // optional
OnCoerceCurrentTimeProperty ), // optional
OnValidateCurrentTimeProperty ); // optional
@wi7a1ian
wi7a1ian / RoutedEventExample.cs
Last active March 18, 2019 09:25
Example of RoutedEvent #wpf #csharp
// Register the routed event
public static readonly RoutedEvent SelectedEvent =
EventManager.RegisterRoutedEvent( "Selected", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(MyCustomControl));
// .NET wrapper
public event RoutedEventHandler Selected
{
add { AddHandler(SelectedEvent, value); }
remove { RemoveHandler(SelectedEvent, value); }
@wi7a1ian
wi7a1ian / VisualTreeHelperExtensions.cs
Last active March 18, 2019 09:25
Find a parent element of the requested type #wpv #csharp
public static class VisualTreeHelperExtensions
{
public static T FindAncestor<T>(DependencyObject dependencyObject)
where T : class
{
DependencyObject target = dependencyObject;
do
{
target = VisualTreeHelper.GetParent(target);
}
@wi7a1ian
wi7a1ian / Result.h
Last active March 18, 2019 10:30
Basic return codes defined for C++ API #cpp
enum class Result
{
// failure codes are here and are negative numbers
ERROR_UNEXPECTED = -100000,
ERROR_MEMORY, // i.e: not enough memory to continue execution
ERROR_MEMORY_BAD_CAST, // dynamic cast has failed
ERROR_MEMORY_BAD_ALLOC, // could not allocate memory on selected heap
ERROR_STACKOVERFLOW, // a stack has overflowed
ERROR_ARGUMENT_INVALID, // an argument to a method was invalid
ERROR_ARGUMENT_OUT_OF_RANGE, // argument value is out of range
@wi7a1ian
wi7a1ian / IString.h
Last active March 18, 2019 10:51
String DTO for C++ API #cpp
template<typename T>
class IData : public IBase {
public:
using Ptr = std::unique_ptr<typename IData<T>, SDeleter>;
public:
virtual const T* Get(uint32_t& size) const noexcept = 0;
};
using IString = IData<uchar>;
@wi7a1ian
wi7a1ian / FooAPI.h
Last active March 18, 2019 10:52
Base for C++ API #cpp
#include "FooAPIDefs.h"
#define FOOINTERFACES_VERSION 7
extern "C" FOOINTERFACES_API IFooFactory* GetFooFactoryPtr(const uchar* options = nullptr, uint32_t version = FOOINTERFACES_VERSION) noexcept;
// API entry point
inline IFooFactory::Ptr GetFooFactory(const uchar* options = nullptr, uint32_t version = FOOINTERFACES_VERSION) {
return IFooFactory::Ptr(GetFooFactoryPtr(options, version));
}