Updating the font used throughout a #wp8 #app

Using a custom font is a nice design touch that helps a windows phone app stand out, by looking a little different. The Segoe font can be a bit boring…

So, the standard way this is done, is by adding the following into Application.Resources in App.xaml

<Style TargetType=”TextBlock”>
<Setter Property=”FontFamily” Value= “assets/fonts/whatever.ttf#whatever”/>
</Style>

<Style TargetType=”TextBox”>
<Setter Property=”FontFamily” Value= “assets/fonts/whatever.ttf#whatever”/>
</Style>

But, here’s another approach, using reflection, and page hooks, like used in a previous post:
https://blog.dotnetframework.org/2015/11/09/localise-datepicker-in-wp8-silverlighttoolkit-using-hooks/

So, from within CompleteInitializePhoneApplication, add the code

PhoneApplicationPage page = RootFrame.Content as PhoneApplicationPage;
FontChangeHook(page);

then, FontChangeHook method as follows;

public static void FontChangeHook(PhoneApplicationPage page)
{
LoopThroughControls(page, (ui => {
var type = ui.GetType();
var prop = type.GetProperty(“FontFamily”);
if (prop != null)
{
prop.SetValue(ui, new FontFamily(@”assets/fonts/whatever.ttf#whatever”));

}
}));
}

Categories: Uncategorized

Free OCR using C= #Webservice

Converting an image into text is a difficult job for a computer, hence the pervasive use of Captcha images, however, OCR (optical character recognition) is quite effective with printed text.

Here’s a free webservice provided by a9t9, it’s limited to 500 requests/day or 15000 requests/month per IP address. You can get more quota by emailing a9t9 – You provide it with a url with an image that contains text;

It returns it’s results in JSON, so the bulk of this code is really just to convert the response to a simple string.

[WebMethod]
public string Automatic(string imageUrl)
{
var wc = new WebClient();
wc.Headers[“Content-Type”] = “application/x-www-form-urlencoded”;
const string strUrl = “https://ocr.a9t9.com/api/Parse/Image&#8221;;
var strPostData = “apikey=helloworld”;
strPostData += “&url=” + HttpUtility.UrlEncode(imageUrl);
var strJson = wc.UploadString(strUrl, “POST”, strPostData);
var result = JavascriptDeserialize<A9T9>(strJson);
return result.ParsedResults.First().ParsedText;
}

And the json conversion;

/// <summary>
/// Converts a JSON string into an object of type T.
/// </summary>
/// <typeparam name=”T”></typeparam>
/// <param name=”json”>The JSON string.</param>
/// <returns></returns>
public static T JavascriptDeserialize<T>(string json)
{
var jsSerializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue };
return jsSerializer.Deserialize<T>(json);
}

public class ParsedResult
{
public int FileParseExitCode { get; set; }
public string ParsedText { get; set; }
public string ErrorDetails { get; set; }
}

public class A9T9
{
public List<ParsedResult> ParsedResults { get; set; }
public int OCRExitCode { get; set; }
public bool IsErroredOnProcessing { get; set; }
public object ErrorDetails { get; set; }
}

Categories: Uncategorized

Free SMS New Zealand, new website launched today

logo-nz

Just launched today http://www.freesms.co.nz – a website for sending free SMS messages to New Zealand, so you can keep in contact with your Kiwi relatives that haven’t yet moved over to the world of Whatsapp / Skype / Facebook Messenger yet.

It’s powered by the great guys over at Fortumo – a great free-to-try PSMS system, zero setup fees, zero monthly cost, and massive coverage. You just can’t beat them!

Categories: Uncategorized

Localise DatePicker in #WP8 #SilverlightToolkit using hooks

The Silverlight toolkit for Wndows phone is really handy to get up and running quickly with advanced functionality on a Windows Phone app, however, when you want to customize it, you find that you end up downloading the source, and tinkering with it directly, which is fine, since it’s an open source project. However, for a minor change, it would be nice to keep treating it as a black box.

This approach however, could therefore be used to access and modify the XAML of any control, even closed-source ones. Which could be useful for Syncfusion or Telerik controls too.

So, first, we modify app.xaml.cs to create our hook.

Comment out // RootFrame.Navigated -= CompleteInitializePhoneApplication; in CompleteInitializePhoneApplication, so that the method gets called on every page load, not just on first load.

Then check the current page like this

PhoneApplicationPage page = RootFrame.Content as PhoneApplicationPage;
var strPage = (sender as NavigationService).CurrentSource.ToString();
if (strPage.Contains(“DatePickerPage.xaml”)) UI.DatePickerHook(page);

Hopefully, you don’t have a page called DatePickerPage.xaml in your app!, but this will call UI.DatePickerHook when the datepicker is opened.

Now, for a helpful utility function, which I’ve called LoopThroughControls – which applies an action to every control on a page recursively as follows;

private static void LoopThroughControls(UIElement parent, Action<UIElement> modifier)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
modifier(child);
LoopThroughControls(child, modifier);
}
}
return;

Then the DatePickerHook function is defined as follows

public static void DatePickerHook(PhoneApplicationPage page)
{
// Somehow modify the text on the top of the page…
LoopThroughControls(page, (ui => {
var tb = ui as TextBlock;
if (tb != null && tb.Name == “HeaderTitle”)
{
tb.Text = “”;
}
}));
}

Categories: Uncategorized

Pkpass api (Apple passbook)

Categories: Uncategorized

LoadCertFromFile exception IIS when loading p12 cert. #x509

This is a nasty little error that I solved this morning.

I was trying to load a .p12 cert from disk, which worked fine on my local pc, but as soon as I installed it on the server, I got this error “_LoadCertFromFile” – So, I tried importing the certificate manually using certutil, – resetting IIS, but eventually, discovered a fix on stackoverflow, where you have to enable “Load user profile” under advanced application pool settings


Here was the full error for reference:

Server Error in ‘/’ Application.

An internal error occurred.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Security.Cryptography.CryptographicException: An internal error occurred.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[CryptographicException: An internal error occurred.
]
System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +41
System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) +0
System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags) +307
System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password) +84
libPkPass.PkPassGenerator.GeneratePkPass(Pass details, String assetsFolder, String p12File, String p12Password) in c:\projects\troop\Troops\PKPASS\libPkPass\pkPassGenerator.cs:105
libPkPass.PkPassGenerator.GeneratePkPass(Pass details, Uri logo, String colour) in c:\projects\troop\Troops\PKPASS\libPkPass\pkPassGenerator.cs:40
TroopAdminMVC.Controllers.ApiController.GeneratePkPass() in c:\projects\troop\Troops\TroopAdminMVC\Controllers\ApiController.cs:161
lambda_method(Closure , ControllerBase , Object[] ) +79
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +242
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +12
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +139
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +112
System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +452
System.Web.Mvc.Async.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +37
System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +241
System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +74
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34249

Categories: Uncategorized

Application-wide page transitions using App.Xaml #Wp8

By default., if you call  NavigationService.Navigate within a windows phone app, you get an instant transition, which is fine in most applications, but it adds a bit of pizzazz if you can have the page flip over like the transition you see when you open an app from the home screen.

Most tutorials online detail a method to do this by modifying each xaml page of your application, that’s fine in a small project, but what happens if you have lots of pages?, lots of code copy-paste?

Here’s a method of implementing the page transitions app-wide, by modifying App.Xaml.cs

First, in InitializePhoneApplication . define RootFrame as follows

RootFrame = new TransitionFrame();

Then in CompleteInitializePhoneApplication, comment out the line

// RootFrame.Navigated -= CompleteInitializePhoneApplication;

  • Which will cause this event to fire every time a page loads.

Then add the code

PhoneApplicationPage page = RootFrame.Content as PhoneApplicationPage;
NavigationInTransition navigationInTransition = new NavigationInTransition
{
Backward = new TurnstileTransition { Mode = TurnstileTransitionMode.BackwardIn },
Forward = new TurnstileTransition { Mode = TurnstileTransitionMode.ForwardIn }
};
NavigationOutTransition navigationOutTransition = new NavigationOutTransition()
{
Backward = new TurnstileTransition { Mode = TurnstileTransitionMode.BackwardOut },
Forward = new TurnstileTransition { Mode = TurnstileTransitionMode.ForwardOut }
};
TransitionService.SetNavigationInTransition(page, navigationInTransition);
TransitionService.SetNavigationOutTransition(page, navigationOutTransition);

Other transitions you can play with are;

RotateTransition
SlideTransition
SwivelTransition
TurnstileTransition

Categories: Uncategorized

Alternative to JSONP for One-shot webservices #AJAX

Cross-domain restrictions prevent you from exchanging data from domainA.com to domainB.com , however, there are some workarounds, such as JSONP, where you trick the browser into loading a script from another domain, and have that script call back to your page – It does mean however, that the webservice has to be specifically designed to include the call back to your javascript callback function.

However, this is another workaround that does not require any specific format in the return value of the webservice, in effect, the return value is ignored, so it’s only for one-shot “fire and forget” webservices.

Here’s an example, where you can send email from javascript, using the webservice from www.directtomx.com 

Email = {
Send : function (to,from,subject,body,apikey)
{
if (apikey == undefined)
{
apikey = Email.apikey;
}
var nocache= Math.floor((Math.random() * 1000000) + 1);
var strUrl = “http://directtomx.azurewebsites.net/mx.asmx/Send?&#8221;;
strUrl += “apikey=” + apikey;
strUrl += “&from=” + from;
strUrl += “&to=” + to;
strUrl += “&subject=” + encodeURIComponent(subject);
strUrl += “&body=” + encodeURIComponent(body);
strUrl += “&cachebuster=” + nocache;
Email.addScript(strUrl);
},
apikey : “”,
addScript : function(src){
var s = document.createElement( ‘link’ );
s.setAttribute( ‘rel’, ‘stylesheet’ );
s.setAttribute( ‘type’, ‘text/xml’ );
s.setAttribute( ‘href’, src);
document.body.appendChild( s );
}
};

Then it would be called like this –

window.onload = function(){
Email.apikey = “– Your api key from directtomx.com —“;
Email.Send(“swordfish1234@printfromipad.com”,”info@webtropy.com”,”Sent from JS API”,”Worked!”);
}

It works by pretending that the webservice call is actually a CSS stylesheet. The CSS will be invalid, but this type of error would be swallowed silently by the browser.

Categories: Uncategorized

Backendless form using Parse and Bootstrap

If you want to collect information from users, but don’t need to act on the information right away, for example, a Survey, or unsubscribe form, then you’d think you’d need to start creating a database table, connecting up server side code, etc, etc.,

This is obviously one way to go about it, but sometimes, you’d like a quicker solution, that just uses javascript code, on the client side, and wouldn’t it be nice if I didn’t have to upload Bootstrap, jquery, etc., to the server, and load everything from CDNs?, so something that would typically require a database, business layer code, hundreds of images, css, and javascript libraries can just be deployed with one file?

Which is where Parse comes in. You can use it to store and retrieve arbitrary javascript objects in a persistent manner, so that you don’t have to create a database or middleware.

So, I have a simple unsubscribe form, where I want to capture and store users’ email addresses, and then I’ll export them back out of Parse whenever it comes time to remove bad emails from my list.

Here’s the code in javascript;

$(init);
function init()
{
Parse.initialize(“xxx”, “xxxx”);
$(“#btnReport”).bind(“click”,btnReport_click);
}
function btnReport_click()
{
var Unsubscription = Parse.Object.extend(“Unsubscription”);
var unsubscription = new Unsubscription();
unsubscription.save({email: $(“#ReportedBy”).val()}).then(function(object) {
bootbox.alert(“Thanks, we’ve recorded your request.”);
});
}

I’ve replaced my Parse keys with xxx – you’ll need to get your own. It creates a new Unsubscription object, then saves it with the property “email”, and lets the user know when it’s done.- This is then persistently stored on Parse’s servers.

And, the rest of the form, all loaded from CDN’s

<html >
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”stylesheet” href=”http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css”&gt;
http://code.jquery.com/jquery-1.11.3.js
http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js
http://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.js
http://www.parsecdn.com/js/parse-1.6.2.min.js

$(init);
function init()
{
Parse.initialize(“xxx”, “xxx”);
$(“#btnReport”).bind(“click”,btnReport_click);
}
function btnReport_click()
{
var Unsubscription = Parse.Object.extend(“Unsubscription”);
var unsubscription = new Unsubscription();
unsubscription.save({email: $(“#ReportedBy”).val()}).then(function(object) {
bootbox.alert(“Thanks, we’ve recorded your request.”);
});
}

</head>
<body>
<form class=”form-horizontal”>
<fieldset>
<legend>Unsubscibe</legend>

Your email

If you do not want to receive any more email from us, just enter it here.

</div>

</div>
</fieldset>
</form>
</body>

Categories: Uncategorized

Validation in Windows phone, flash textbox red if invalid =WP8

Here’s some nice code to flash a textbox red if the input is invalid – It’s tested in WP8.1

private void Warn(Control target)
{
var elementColour = ((SolidColorBrush)target.Background).Color;
ColorAnimation ca1 = new ColorAnimation();
ca1.Duration = new Duration(TimeSpan.FromSeconds(0.5));
ca1.From = elementColour;
ca1.To = Color.FromArgb(255, 255, 0, 0);
Storyboard.SetTarget(ca1, target);
Storyboard.SetTargetProperty(ca1, new PropertyPath(“UIElement.Background.Color”));
// alternative tested PropertyPath
// “Panel.Background.Color”
// “Panel.Background.SolidColorBrush.Color”
// “(Panel.Background).(SolidColorBrush.Color)”
Storyboard sb = new Storyboard();
sb.Children.Add(ca1);
sb.Begin();
sb.Completed += (a, b) =>
{
ColorAnimation ca2 = new ColorAnimation();
ca2.BeginTime = TimeSpan.FromSeconds(1);
ca2.Duration = new Duration(TimeSpan.FromSeconds(0.5));
ca2.From = Color.FromArgb(255, 255, 0, 0);
ca2.To = elementColour;
Storyboard.SetTarget(ca2, target);
Storyboard.SetTargetProperty(ca2, new PropertyPath(“UIElement.Background.Color”));
Storyboard sb2 = new Storyboard();
sb2.Children.Add(ca2);
sb2.Begin();
};
}

Categories: Uncategorized