Skip to content

Instantly share code, notes, and snippets.

@emoacht
emoacht / DialogHelper.cs
Created May 20, 2023 09:47
Use WinRT's FileOpenPicker and FolderPicker from WPF.
using System;
using System.Threading.Tasks;
using System.Windows.Interop;
using Windows.Storage.Pickers;
public static async Task<string?> OpenFileDialog(System.Windows.Window window)
{
var picker = new FileOpenPicker()
{
ViewMode = PickerViewMode.Thumbnail
@emoacht
emoacht / StoreSample.cs
Last active May 14, 2023 06:22
Sample method for Microsoft Store API to check if a subscription add-on is in trial period and if automatic renewal is on
// using Windows.Services.Store;
public enum LicenseStatus
{
Unknown = 0,
Trial,
Full
}
private static StoreContext _context;
@emoacht
emoacht / UnityWebRequestUsage.cs
Created December 31, 2015 22:57
Sample usage of UnityWebRequest
using System.Collections;
using UnityEngine;
using UnityEngine.Experimental.Networking;
public class UnityWebRequestUsage : MonoBehaviour
{
void Start()
{
StartCoroutine(GetText());
}
@emoacht
emoacht / MainWindow1.xaml
Created April 29, 2023 21:31
Sample Styles of Round Button
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="400" Height="200">
<Window.Resources>
<Style x:Key="RoundStyle" TargetType="{x:Type Button}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="100"/>
<Setter Property="Foreground">
@emoacht
emoacht / DependencyObjectExtensions.cs
Created April 15, 2023 02:30
Scroll ScrollViewer of ListBox by mouse wheel.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Media;
public static class DependencyObjectExtensions
{
public static bool TryFindDescendant<T>(this DependencyObject reference, [NotNullWhen(true)] out T? descendant) where T : DependencyObject
{
var queue = new Queue<DependencyObject>();
@emoacht
emoacht / TaskbarHelper.cs
Created April 14, 2023 02:25
Switch taskbar aute hide state.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
internal static class TaskbarHelper
{
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
extern static IntPtr FindWindow(
[MarshalAs(UnmanagedType.LPWStr), In] string lpClassName,
[MarshalAs(UnmanagedType.LPWStr), In] string? lpWindowName);
@emoacht
emoacht / MainWindow.xaml.cs
Last active March 25, 2023 03:51
Generate and consume IAsyncEnumerable<T>.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private async void OnLoaded(object sender, RoutedEventArgs e)
{
@emoacht
emoacht / DeviceNotificationListener.cs
Created February 15, 2015 09:02
Listen add/remove events of devices including USB devices in WPF
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Interop;
@emoacht
emoacht / MonitorHelper.cs
Created January 17, 2023 12:44
Enumerate monitor connections by WMI.
using System.Collections.Generic;
using System.Linq;
using System.Management;
public static class MonitorHelper
{
public static IEnumerable<MonitorConnection> EnumerateMonitorConnections()
{
var @class = new ManagementClass(@"root\wmi:WmiMonitorConnectionParams");
@emoacht
emoacht / CollectionToBooleanConverter.cs
Created January 5, 2023 01:18
Converts the state whether the source object is a collection and it has any item to boolean.
using System;
using System.Collections;
using System.Globalization;
using System.Windows.Data;
public class CollectionToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is IEnumerable collection)