Skip to content

Instantly share code, notes, and snippets.

Avatar

Atsushi Nakamura nuitsjp

View GitHub Profile
@nuitsjp
nuitsjp / pre-configuration.ps1
Created Aug 12, 2020
Scoop-Playbookのインストール前に必要だったこと
View pre-configuration.ps1
scoop install 7zip
scoop install sudo
scoop install dark
scoop install innounp
sudo Add-MpPreference -ExclusionPath 'C:\ProgramData\scoop'
sudo Add-MpPreference -ExclusionPath $home\scoop
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -Value 1
@nuitsjp
nuitsjp / GitHubCounter.cs
Last active Mar 21, 2019
Count download of GitHub release module.
View GitHubCounter.cs
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Linq;
using System.Threading.Tasks;
public static class GitHubCounter
{
public static async Task CountDownload(string owner, string repository)
{
@nuitsjp
nuitsjp / GitHubCommitCounter.cs
Created Mar 21, 2019
Count commits to a specific owner's repository for a specific time period.
View GitHubCommitCounter.cs
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Linq;
using System.Threading.Tasks;
public static class GitHubCommitCounter
{
public static async Task Count(string owner, DateTime minDateTime)
{
@nuitsjp
nuitsjp / Enumに別名?をつけて表示する.cs
Last active Mar 13, 2020
Enumに別名?をつけて表示する
View Enumに別名?をつけて表示する.cs
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Gender.Female.ToJapanese());
Console.ReadLine();
}
}
public enum Gender
@nuitsjp
nuitsjp / MockExtensions.cs
Created Dec 22, 2016
You can easily publish PropertyChanged events from the Moq.
View MockExtensions.cs
public static class MockExtensions
{
public static IReturnsResult<T> NotifyPropertyChanged<T, TResult>(this Mock<T> mock, Expression<Func<T, TResult>> expression, TResult setupValue) where T : class, INotifyPropertyChanged
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null) throw new ArgumentException("expression.Body is not MemberExpression");
var returnResult = mock.Setup(expression).Returns(setupValue);
mock.Raise(m => m.PropertyChanged += null, new PropertyChangedEventArgs(memberExpression.Member.Name));
@nuitsjp
nuitsjp / App.xaml.cs
Last active Aug 2, 2017
Xamarin.FormsでOnSleep・OnResumeをViewModelでハンドルする
View App.xaml.cs
public partial class App : Application
{
.....
protected override void OnSleep()
{
(MainPage.BindingContext as IApplicationLifecycle)?.OnSleep();
}
protected override void OnResume()
@nuitsjp
nuitsjp / sample.cs
Created Nov 17, 2016
Use embedded sqlite database file in Xamarin.Forms
View sample.cs
const string databaseFileName = "sqlite.db3";
// ルートフォルダを取得する
IFolder rootFolder = FileSystem.Current.LocalStorage;
// ファイルシステム上のDBファイルの存在チェックを行う
var result = await rootFolder.CheckExistsAsync(databaseFileName);
if (result == ExistenceCheckResult.NotFound)
{
// 存在しなかった場合、新たに空のDBファイルを作成する
var newFile = await rootFolder.CreateFileAsync(databaseFileName, CreationCollisionOption.ReplaceExisting);
// Assemblyに埋め込んだDBファイルをストリームで取得し、空ファイルにコピーする
@nuitsjp
nuitsjp / Bootstrapper.cs
Created Nov 2, 2016
PrismでViewとViewModelを別アセンブリにする場合のコードサンプル
View Bootstrapper.cs
ViewTypeToViewModelTypeResolver _resolver;
protected override void ConfigureViewModelLocator()
{
base.ConfigureViewModelLocator();
_resolver = new ViewTypeToViewModelTypeResolver(typeof(MainWindowViewModel).Assembly); // とりあえず適当なVMからAssembly取得して設定しておく
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(_resolver.Resolve);
}
public class ViewTypeToViewModelTypeResolver
{
@nuitsjp
nuitsjp / BitmapSourceExtensions.cs
Last active Oct 17, 2016
Convert System.Windows.Media.BitmapSource to System.Drawing.Bitmap
View BitmapSourceExtensions.cs
public static Bitmap ToBitmap(this BitmapSource bitmapSource, PixelFormat pixelFormat)
{
int width = bitmapSource.PixelWidth;
int height = bitmapSource.PixelHeight;
int stride = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8); // 行の長さは色深度によらず8の倍数のため
IntPtr intPtr = IntPtr.Zero;
try
{
intPtr = Marshal.AllocCoTaskMem(height * stride);
bitmapSource.CopyPixels(new Int32Rect(0, 0, width, height), intPtr, height * stride, stride);
@nuitsjp
nuitsjp / BitmapExtensions.cs
Created Oct 17, 2016
Convert System.Drawing.Bitmap to System.Windows.Media.ImageSource
View BitmapExtensions.cs
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);
public static ImageSource ToImageSource(this Bitmap bmp)
{
var handle = bmp.GetHbitmap();
try
{
return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());