Skip to content

Instantly share code, notes, and snippets.

View EntityFrameworkConcurrency.cs
private async Task SaveChangesAsync()
{
try
{
await _dbContext.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
//adapted from: https://docs.microsoft.com/en-us/ef/core/saving/concurrency
foreach (var entry in ex.Entries)
View LoginAsync.cs
public async Task<bool> LoginAsync()
{
var authenticator = new Windows.Security.Authentication.OnlineId.OnlineIdAuthenticator();
var mobileServicesTicket = new Windows.Security.Authentication.OnlineId.OnlineIdServiceTicketRequest("https://yourendpoint.azure-mobile.net/", "JWT");
var ticketRequests = new List<OnlineIdServiceTicketRequest>() { mobileServicesTicket };
var authResult = await authenticator.AuthenticateUserAsync(ticketRequests, CredentialPromptType.PromptIfNeeded);
if ((authResult.Tickets.Count == 1) && (authResult.Tickets[0].ErrorCode == 0))
View GetFilesInCameraRoll.cs
public async Task<IEnumerable<StorageFile>> GetFilesInCameraRoll()
{
var cameraRoll = await KnownFolders.PicturesLibrary.GetFolderAsync("Camera Roll");
return await cameraRoll.GetFilesAsync();
}
View StringExtensions.cs
using System;
namespace MadWorldApps.PCL.Helpers.Extensions
{
public static class StringExtensions
{
/// <summary>
/// Adds spaces to words based on capital characters.
/// </summary>
/// <param name="s">The CamelCase string you want spaces added to.</param>
View EnumExtensions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace MadWorldApps.PCL.Helpers.Extensions
{
public static class EnumExtensions
{
/// <summary>
View EnumExtensions_Partial1.cs
public static class EnumExtensions
{
/// <summary>
/// Converts the enum member names into an enumberable of strings.
/// </summary>
/// <typeparam name="TEnum">A valid enum type.</typeparam>
/// <returns>An enumerable of strings based on the enum members.</returns>
public static IEnumerable<string> GetNames<TEnum>() where TEnum : struct
{
var typeInfo = ValidateEnum<TEnum>();
@zoint
zoint / SettingsService.cs
Last active Jan 1, 2016
Localization of App Settings for Windows Phone 8.
View SettingsService.cs
using System;
using System.Globalization;
using MadWorldApps.Phone.Helpers.Interfaces;
namespace Places.BLL.Services
{
public enum DistanceType { Miles, Kilometers };
public class SettingsService : ISettingsService
@zoint
zoint / PositionChanged.cs
Last active Dec 30, 2015
Basic position changed event handler
View PositionChanged.cs
private async void PositionChangedHandler(Geolocator sender, PositionChangedEventArgs args)
{
if (args.Position == null || args.Position.Coordinate == null) return;
//save updated coordinates to a property on the class
//can do whatever we want with position, all I need are the coordinates
Position = args.Position.Coordinate;
//stop the tracking once we have our desired accuracy in meters
if (args.Position.Coordinate.Accuracy < 500)
@zoint
zoint / gist:7867765
Created Dec 9, 2013
Start position tracking Windows Phone 8
View gist:7867765
public void StartTracking()
{
_geolocator.ReportInterval = (uint)TimeSpan.FromSeconds(30).TotalMilliseconds;
_geolocator.MovementThreshold = 100;
// this implicitly starts the tracking operation
_geolocator.PositionChanged += PositionChangedHandler;
}