Writing Behaviors in PCL for Windows Phone 8.1 and Windows 8.1
Join the DZone community and get the full member experience.
Join For Freei was happily adding windows phone 8.1 support to wpwinnl – or actually support for universal windows apps - when i noticed that basically all i was doing was linking files from the windows 8.1 project – without any changes. convergence ftw! although that made adding the support as such pretty simple, i also envisioned a maintenance nightmare coming towards me as would not only need to maintain links between windows 8.1 and windows phone 8.0, but also between windows 8.1 and windows phone 8.1. maybe it was time to go pcl. that went pretty well, until i hit a major roadblock.
this roadblock, my friends, is very simple: the behaviors sdk, and thus everything in microsoft.xaml.interactivity , is not available to pcl. this has probably to do with the fact that the behaviors sdk for windows 8 is a separate entity, not part of the core framework, that was added later. this same characteristic has moved over to windows phone 8.1. it basically boils down to this: every behavior must implement microsoft.xaml.interactivity.ibehavior and this is sitting in two separate assemblies – one for windows, one for windows phone. on my surface pro they are sitting in c:\program files (x86)\microsoft sdks\windows\v8.1\extensionsdks\behaviorsxamlsdkmanaged\12.0\ for windows 8.1 and in c:\program files (x86)\microsoft sdks\windowsphoneapp\v8.1\extensionsdks\behaviorsxamlsdkmanaged\12.0\ for windows phone 8.1
now what?
of course, like i wrote before, you can moan and complain about this and send venomous tweets to joe belfiore, cliff simpkins or just the first windows phone mvp you can find – or you can pause and think if there’s a way around it. this is the thing i love to do – explore, thinker, invent – and come to a solution. because as it turns out, my friends, there is one.
the key to the solution is
this pretty odd article
that, believe it or not, was sent to a closed mailing list of windows phone mvps for to get opinions by none other than
matthijs hoekstra
, just the day before i wrote this. then i thought it was a crazy idea - today i find it an invaluable clue. talking about coincidences. it’s like basic research – you never know where the next life saving idea will be coming from ;-) .
it’s called the “bait and switch pcl trick” and it basically makes use of a very odd nuget trick – it will always prefer a native platform library over a pcl. if you make a nuget package that contains both a pcl and a native version , the pcl is then only used at compile time . so what i did was create this crazy nuget package called crossplatformbehaviorbase (i think i am going to change that later) that basically has three projects in it: once pcl, one windows 8.1, and one windows 8.1.
the key thing is that the implementation for windows phone and windows are the same (in fact, the windows phone 8.1 implementation is linked from the windows implementation, but the pcl is slightly different. the pcl version is:
namespace ibehaviorbase { public interface ibehaviorbase { } }
indeed, an interface that does
totally
nothing. whereas the windows phone and windows implementations look like this:
using microsoft.xaml.interactivity; namespace ibehaviorbase { public interface ibehaviorbase : ibehavior { } }
if you build for release this will create three projects neatly conforming to nuget naming specifications and finally run a little command file that creates a nuget package in the output folder directly under the solution folder.
to be able to install the package, you need to specify that folder as a nuget source:
mind you, in the future this will all be on nuget so you won’t have to put up with this. for now, am just explaining how it’s done.
anyway, i went ahead and created an universal windows app demoapp, and added a windows phone 8.1/windows 8.1 pcl “wpwinnl” to the solution.
the newly created nuget package needs to be installed in all three projects:
and then i started in copying stuff from wpwinnl,starting with the
behavior base class that i created to maintain compatibility with ‘silverlight style’ behaviors
:
using windows.ui.xaml; namespace wpwinnl.behaviors { public abstract class behavior : dependencyobject, ibehaviorbase.ibehaviorbase { public dependencyobject associatedobject { get; set; } public virtual void attach(dependencyobject associatedobject) { associatedobject = associatedobject; } public virtual void detach() { } } }
the key part is highlighted in red and underlined – in stead of directly implementing ibehavior, the class implements my go-between
ibehaviorbase.ibehaviorbase.
the name is a bit of a kludge, i’ll be the first one to admit that, but it does the trick. and you won’t see it ever anymore, because it’s only used a a base class for behavior<t>:
namespace wpwinnl.behaviors { public abstract class behavior<t> : behavior where t: dependencyobject { [system.componentmodel.editorbrowsable( system.componentmodel.editorbrowsablestate.never)] public new t associatedobject { get; set; } public override void attach(dependencyobject associatedobject) { base.attach(associatedobject); this.associatedobject = (t)associatedobject; onattached(); } public override void detach() { base.detach(); ondetaching(); } protected virtual void onattached() { } protected virtual void ondetaching() { } } }
which basically comes over from the existing
wpwinnl
implementation
unchanged
. for kickers, i then put my infamous
dragflickbehavior
, and
all my animation code
that has been featured on this blog multiple times on top of it and lo and behold – with a minor addition it just works. and i was able to delete all #ifdef statements for windows phone out, leaving only the former ‘just windows’ code.
all in pcl
. and all usable both on windows phone and windows.
as coupe de grace i put the whole of mainpage.xaml in the shared portion, opened blend and sure enough – there was my dragflickbehavior:
so i dropped a date picker, a text box and a button on the screen and dragged my pcl-dragflickbehavior on top on all three:
<page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:interactivity="using:microsoft.xaml.interactivity" xmlns:behaviors="using:wpwinnl.behaviors" x:class="demoapp.mainpage" mc:ignorable="d"> <grid background="{themeresource applicationpagebackgroundthemebrush}"> <button content="button" horizontalalignment="left" height="37" margin="101,114,0,0" verticalalignment="top" width="108"> <interactivity:interaction.behaviors> <behaviors:dragflickbehavior/> </interactivity:interaction.behaviors> </button> <datepicker horizontalalignment="left" margin="48,230,0,0" verticalalignment="top"> <interactivity:interaction.behaviors> <behaviors:dragflickbehavior/> </interactivity:interaction.behaviors> </datepicker> <textblock horizontalalignment="left" height="42" margin="132,310,0,0" textwrapping="wrap" text="blabla" verticalalignment="top" width="121"> <interactivity:interaction.behaviors> <behaviors:dragflickbehavior/> </interactivity:interaction.behaviors> </textblock > </grid> </page>
the screen shows up and you can happily drag the items along, although dragging a windows phone date picker is quite a challenge.
so there you have it – behaviors completely defined in pcl, courtesy of a little nuget trick. could the windows phone and/or windows team have done this themselves? possibly – but don’t forget i just sailed past anything that is not managed, like c++. and if there ain’t any challenges for mvps to meet, fat lot of use we would be, eh? ;-)
a zip file with both the nuget package and the universal windows app can be found here . for now it’s a kind of proof of concept, soon i will be putting this nuget package out on nuget itself and use it for the new version of wpwinnl. after some rigorous testing ;).
Published at DZone with permission of Joost van Schaik, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java
-
Mastering Time Series Analysis: Techniques, Models, and Strategies
-
What Is Test Pyramid: Getting Started With Test Automation Pyramid
-
Explainable AI: Making the Black Box Transparent
Comments