Corrado's Blog 2.0

Online thoughts of a technology funatic

Save Win2D CanvasControl content

Win2D is an easy-to-use Windows Runtime API for immediate mode 2D graphics rendering with GPU acceleration that is very helpful in some situations like real time or layered rendering.
This post is about how to save dynamically rendered content to a file for further external processing.

To give you an idea, let’s build a quick Win2D sample.

Create a blank new UWP project and add a NuGet reference to Win2D package

image

on MainPage.Xaml add following XAML

and this code on relative codebehind

if you now run the sample and change the value of the slider you will see the rectangle moving this way

cb

From the code is evident that each time the value of the slider changes we invalidate the CanvasControl so that OnDraw method gets called and content is redrawn with the new rectangle position.

To save CanvasControl content we basically need to draw actual content into an offscreen render target and use the SaveAsync method to get the stream content.
Here’s the code:

code just draws the same content into a CanvasRenderTarget representing the offscreen renderer and then gets that stream, the remaining code just saves it into a StorageFile.
Off course what’s drawn into offscreen renderer is up to you (as example you might add a watermark) and output format can be changed via CanvasFormatBitmap parameter.

HTH

Fix Xamarin Form compilation errors

I’ve recentily repaved my development machine and installed Visual Studio 2017 adding nearly all features including, of course, Xamarin and all subsequent updates.
Today I’ve tried to create a blank new Xamarin Forms project targeting .Net Standard for the common part but as soon as I tried to compile it I got a strange sequence of errors on Android project:

error APT0000: 1: error: Error retrieving parent for item: No resource found that matches the given name ‘Theme.AppCompat.Light.Dark
error APT0000: 2: error: Error: No resource found that matches the given name: attr ‘colorAccent’.
error APT0000: 1: error: Error: No resource found that matches the given name: attr ‘colorPrimary’.
error APT0000: 1: error: Error: No resource found that matches the given name: attr ‘colorPrimaryDark’.
error APT0000: 1: error: Error: No resource found that matches the given name: attr ‘windowActionBar’.
error APT0000: 3: error: Error: No resource found that matches the given name: attr ‘windowActionModeOverlay’.
error APT0000: 1: error: Error: No resource found that matches the given name: attr ‘windowNoTitle’.
error APT0000: 3: error: Error retrieving parent for item: No resource found that matches the given name ‘Theme.AppCompat.Light.Dial
error APT0000: 3: error: Error: No resource found that matches the given name: attr ‘colorAccent’.

Totally nonsense for me.
After a chat with my friend Laurent we’ve discovered that problem depends on missing installation of Android SDK 24 (Nugat 7.0) after installing it using the new SDK Manager (via Tools->Android->Android SDK manager) problem was gone.

Cause probably depends on referenced packaged used in default templates that no longer support Android Marshmallow (6.0) the version that gets installed by default by Visual Studio.

Fix “No Internet Connection” issue

A couple of days ago, some applications like Skype, Skype for Business, Outlook stopped working due to a “can’t connect” problem, the only way to make them work was to enable corporate VPN, since I was busy didn’t take care of it and just used the VPN.
This morning, same problem, and so I went to the “VPN trick” to get over it but this time it wasn’t working and after a few minutes I lost the entire interner connectivity on my main machine.
Think you know that no internet connectivity at these days is more or less like a boot disk failure so i started trying nearly everything from reboot, to driver updates, going through wired connection, using tethering etc but got no luck.
I then decided to compare what ipconfig was telling me on two different machines and i discovered a “little difference”, my wifi card was having the Autoconfiguration Ipv4 address set to a weird “169.254.X,Y” no idea of where that address was coming from but I thought that it could be the origin of my problem.

I the started looking for a solution and, voilà, two magic commands solved my headache…

1) Open command prompt as admin
2) type netsh int ip reset +enter
3) type netsh winsock reset + enter
4) reboot your machine

In my case it worked, hope it will be the same for you that you’ve reached this post probably because you have the same problem as me.

Good luck!

Change x:Bind default binding mode

Recent release of Windows 10 introduced Complied Bindings, an type safe alternative to {Binding} syntax that add compile time checking and better performances, it can also be used in place of ElementName to bind to a UI element property.
In order to squeeze performances the default binding mode of compiled bindings is OneTime, this means that any further change of bound property after initial one won’t get propagated to bound UI property, and this, personally, is one of the most annoying things because i often use properties whose value changes and everytime i spend time figuring why they don’t before remembering of this different behavior.

To give you an example, let’s take this simple ViewModel

Page code behind

and page markup

if you try this code you will see that value doesn’t change when you click the button, this is because of default OneTime binding.

While you can change the behavior by adding Mode=OneWay to the Count binding, if you have many bindings and you’re targeting Fall Creators Update you can use the new attibute DefaultBindingMode

The plus of using this attribute is that it changes the default binding mode to all compiled bindings contained in the element where it is applied (and all childrens) saving a lot of time.

Nice! waited a long for this Smile