Skip to content

Instantly share code, notes, and snippets.

View SchreinerK's full-sized avatar

Kay-Uwe Schreiner (kux) SchreinerK

View GitHub Profile
@SchreinerK
SchreinerK / XyzVM.cs
Last active February 26, 2018 22:50
Simple ViewModel (KsWare Presentation Framework)
using KsWare.Presentation.ViewModelFramework;
namespace MyProject {
public class XyzVM : ObjectVM {
public XxxVM () {
RegisterChildren(()=>this);
}
}
@SchreinerK
SchreinerK / XyzDataVM.cs
Last active February 26, 2018 23:08
Data ViewModel (KsWare Presentation Framework)
using KsWare.Presentation.ViewModelFramework;
namespace MyProject {
public class XyzDataVM : DataVM<XyzData> {
public XxxVM () {
RegisterChildren(()=>this);
Fields[nameof(Name)].ValueChangedEvent.add(() => Data.Name = Name); // save changes immediately to data model
@SchreinerK
SchreinerK / WPF-Read-BAML.cs
Last active April 5, 2018 08:40
Read BAML (create object from BAML)
public static T ReadBaml<T>(Stream stream) {
var bamlReader = new Baml2006Reader(stream);
var writer = new XamlObjectWriter(bamlReader.SchemaContext);
while (bamlReader.Read()) writer.WriteNode(bamlReader);
return (T)writer.Result;
}
@SchreinerK
SchreinerK / WPF-DesignTime-Style-DataContext.xaml
Last active April 5, 2018 08:38
Specify a designer datacontext for a style
<Style>
<d:Style.DataContext>
<x:Type Type="local:MyTreeItem" />
</d:Style.DataContext>
<!--usual setters, triggers, etc.-->
</Style>
@SchreinerK
SchreinerK / DependencyObjectExtensions.cs
Last active March 6, 2019 07:05
Get all dependency and attached proprties
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Markup.Primitives;
using System.Windows.Media;
public static class DependencyObjectExtensions
{
public static List<DependencyProperty> GetDependencyProperties(this DependencyObject element)
@SchreinerK
SchreinerK / GuidSemaphoreSlim.cs
Last active July 3, 2019 08:08
Guid Ressource Lock
public sealed class GuidSemaphoreSlim : IDisposable
{
private static readonly Dictionary<Guid,SemaphoreSlim> Handles=new Dictionary<Guid, SemaphoreSlim>();
private readonly Guid _guid;
private readonly SemaphoreSlim _semaphore;
/// <summary>
/// Usage: using(await GuidSemaphoreSlim.WaitAsync(myGuid)) {...}
/// </summary>
public static async Task<IDisposable> WaitAsync(Guid guid)
@SchreinerK
SchreinerK / Result.cs
Created July 5, 2019 04:43
Result<Value>
public struct Result<TValue>
{
internal Result(TValue value, bool success, object failureInfo)
{
Value = value;
Success = success;
FailureInfo = failureInfo;
}
public TValue Value { get; private set; }
@SchreinerK
SchreinerK / WaitHandleAsyncExtension.cs
Created July 10, 2019 05:37
WaitHandle.WaitOneAsync(...)
public static class WaitHandleAsyncExtension
{
public static async Task<bool> WaitOneAsync(this WaitHandle handle, int millisecondsTimeout, CancellationToken cancellationToken)
{
RegisteredWaitHandle registeredHandle = null;
var tokenRegistration = new CancellationTokenRegistration();
try
{
var tcs = new TaskCompletionSource<bool>();
registeredHandle = ThreadPool.RegisterWaitForSingleObject(
@SchreinerK
SchreinerK / AsyncHelpers.cs
Created July 21, 2019 03:59
Run Task synchonously
public static class AsyncHelpers
{
/// <summary>
/// Execute's an async Task<T> method which has a void return value synchronously
/// </summary>
/// <param name="task">Task<T> method to execute</param>
public static void RunSync(Func<Task> task)
{
var oldContext = SynchronizationContext.Current;
var synch = new ExclusiveSynchronizationContext();
@SchreinerK
SchreinerK / GenerateFullTypeName.cs
Created September 1, 2019 06:06
Generates the (C#) code for the specified type name.
public static string GenerateFullTypeName(Type type)
{
var retType = new StringBuilder();
if (type.IsGenericType)
{
var parentType = type.FullName.Split('`');
var arguments = type.GetGenericArguments();
var argList = new StringBuilder();