Skip to content

Instantly share code, notes, and snippets.

View dgkanatsios's full-sized avatar

Dimitris-Ilias Gkanatsios dgkanatsios

View GitHub Profile
@dgkanatsios
dgkanatsios / TapAction
Created July 25, 2014 18:46
A XAML Behavior for a small tap animation for Windows Phone, allows for an event to run after the Storyboard is completed
public class TapAction : Behavior<FrameworkElement>
{
public string EventAfterStoryboard
{
get { return (string)GetValue(EventAfterStoryboardProperty); }
set { SetValue(EventAfterStoryboardProperty, value); }
}
@dgkanatsios
dgkanatsios / BinaryListCheck.cs
Last active January 6, 2016 21:18
Code to insert/check existence of an item using binary operations
static void Main(string[] args)
{
int storage = 19; //10011
int a = 1; //1
int b = 2; //10
int c = 4; //100
int d = 8;//1000
int e = 16;//10000
Console.WriteLine("--------------------------------");
Console.WriteLine("Check for the value 19 - 10011");
@dgkanatsios
dgkanatsios / StoryboardEmulatesThreadSleep
Created July 25, 2014 18:51
A snippet to emulate Thread.Sleep in Windows Phone apps via XAML Storyboards
public async static Task CreateSleepStoryboard(int ms)
{
Storyboard sb = new Storyboard();
sb.Duration = TimeSpan.FromMilliseconds(ms);
await sb.BeginAsync();
}
@dgkanatsios
dgkanatsios / WPDeviceInfo
Created July 25, 2014 18:52
A snippet to obtain device info on Windows Phone
public static string GetDeviceInfo()
{
string returnvalue = string.Empty;
try
{
returnvalue = string.Format("manufacturer {0}, name {1}, firmware {2}, hardware {3}",
DeviceStatus.DeviceManufacturer, DeviceStatus.DeviceName, DeviceStatus.DeviceFirmwareVersion, DeviceStatus.DeviceHardwareVersion);
}
catch
@dgkanatsios
dgkanatsios / WPResolutionHelper
Created July 25, 2014 18:53
Resolution helpers for Windows Phone
public static class ResolutionHelper
{
public static Uri GetScaledImageUri(String imageName)
{
int scaleFactor = (int)Application.Current.Host.Content.ScaleFactor;
switch (scaleFactor)
{
case 100: return new Uri(imageName + "_wvga.png", UriKind.RelativeOrAbsolute);
case 150: return new Uri(imageName + "_720p.png", UriKind.RelativeOrAbsolute);
case 160: return new Uri(imageName + "_wxga.png", UriKind.RelativeOrAbsolute);
@dgkanatsios
dgkanatsios / CreateOpacityStoryboard
Created July 25, 2014 18:54
A snippet to create a 0 to 1 opacity Storyboard for Windows Phone
public static Storyboard CreateOpacityStoryboard(FrameworkElement element)
{
Storyboard sb = new Storyboard();
DoubleAnimation opacityAnimation = new DoubleAnimation();
opacityAnimation.From = 0;
opacityAnimation.To = 1;
opacityAnimation.Duration = TimeSpan.FromMilliseconds(300);
Storyboard.SetTarget(opacityAnimation, element);
@dgkanatsios
dgkanatsios / PlaySoundXNA
Created July 25, 2014 18:56
Snippet to play sound in Windows Phone via XNA Libraries
static SoundEffectInstance sei;
public async static Task PlaySound(string soundPath, bool delay = true)
{
Stream waveFileStream = TitleContainer.OpenStream(soundPath);
SoundEffect se = SoundEffect.FromStream(waveFileStream);
sei = se.CreateInstance();
FrameworkDispatcher.Update();
sei.Play();
if (delay)
await Task.Delay(se.Duration);
@dgkanatsios
dgkanatsios / GetRandomNumbersAroundCorrectOne
Created July 25, 2014 19:04
Get random numbers given a margin around a particular one
public static List<int> GetRandomNumbersAroundCorrectOne(int correctOne, int margin, int count)
{
List<int> results = new List<int>();
for (int i = 0; i < count; i++)
{
//get the prosimo
int prosimo = 1;// ((NextRandom(1, 3) % 2) == 0) ? 1 : -1;
int number;
if (prosimo == 1)
{
@dgkanatsios
dgkanatsios / indexOnArrayLINQ
Created July 25, 2014 22:47
Find index on array via LINQ
myObjects.Select((v, i) => new {myObject = v, index = i}).First(predicate).index;
var api = {
"post": function (req, res, next) {
var sqlstring = 'UPDATE Highscore SET score=@score,playername=@playername WHERE id=@id';
var query= {sql:sqlstring,
parameters:[
{name:'score',value:req.body.score},
{name:'playername',value:req.body.playername},
{name:'id',value:req.body.id}]};