Sending Files to a Xamarin.Forms App – Part 2: Android

Sending Files to a Xamarin.Forms App – Part 2: Android

In the first part of this series – I looked at how to register a Xamarin.Forms app on iOS to receive and display a PDF. In this post I’ll do the same thing – but this time on Android. The Scenario Again the overall scenario we’re talking about here is: We wrote a Xamarin.Forms application. This application is capable of special handling of a specific file type (in the demo app I’m using in this blog post, that’s displaying a PDF file). We want the operating system to know that our app is capable of performing operations on that file type. Thus our app should appear as an option in a list of target apps to send the file to, if the user should so decide. So let’s get to it. Android Registration Much like you use Intents to send data between Activities in Android – you can use Intents to do the same between apps. Explicit vs Implicit Intents When you know the exact Activity you want to open within you app – you create what’s called an Explicit Intent. When instantiating an Explicit Intent – you specify the class type of the Activity that should be started, and then the operating system will start that when the StartActivity() is encountered. Easy enough – but what happens when the developer of the “source app” doesn’t know which Activity should be started, because that Activity is going to reside in a separate application? All the app wants to do is “send” out a notification that it has something it wants to deliver to another app, so that app can...
Sending Files to a Xamarin.Forms App – Part 1: iOS

Sending Files to a Xamarin.Forms App – Part 1: iOS

It’s a common occurrence in mobile applications to have a file be open in one app – but need another app to use it. For example, you browsed to a PDF and find you’d like to perform some modifications to it in another app. By tapping on that PDF, a “Open In” bar will appear across the top of the screen, allowing you to import the file in your desired app. Sending files between apps sounds like a pretty operating system specific feature – something that would best be not attempted in Xamarin.Forms, right? Actually, while it is platform-specific, it’s pretty straight-forward, and the rest of this post will demonstrate how to get it up and running on iOS. (A future post will tackle Android … and if there’s any demand, we’ll cover UWP too.) It should be noted that for this post I am NOT going to speak about creating an app extension to the iOS portion of your Forms solution, and then having your app appear when the user taps the share button (the little rectangle with an arrow coming out of it). For now this is purely about bringing a file into our Forms app – not “sharing” content between apps. Our Scenario The example that we’ll build out in this post is having our Xamarin.Forms app register as capable of opening PDF files and then displaying the PDF file in a WebView. That will demonstrate the basics of registering as being able to open for a file with iOS and a means to open them with Xamarin.Forms (that will be applicable to Android as well)....
Attached Properties – What Are They Good For?

Attached Properties – What Are They Good For?

For the longest time I knew what Attached Properties in Xamarin.Forms were – the most famous example being setting a control’s position within a Grid … <Label Text="I'm attached!" Grid.Row="1" Grid.Column="2" /> 123  <Label Text="I'm attached!" Grid.Row="1" Grid.Column="2" />  … but to be honest, I just didn’t care how the Grid.Row and Grid.Column or any attached properties worked behind the scenes. They were one of those things that as long as they worked – I didn’t care if I knew anything else about them. I even knew I could use them to bind Commands to events that don’t work with Commands. And I heard rumors I could do cool things with Effects with them. But still, I didn’t care. But I finally took the time to sit down and understand them … and wow am I glad I did! Attached properties are extremely powerful and they let you do some seriously cool stuff from XAML (or C# if you’re still into that). What Are They? Attached Properties allow properties from one object to be “attached” and set from another object. So in the ubiquitous Grid example – a Label can set the Grid’s properties – within the Label’s XAML declaration. Or as the official Xamarin documentation refers to it: An attached property is a special type of bindable property, defined in one class but attached to other objects… There’s a key word in that definition above that gives Attached Properties all of their power … “bindable”. Because they are bindable – they essentially provide a means to let one view to communicate with something else. Like another a view,...
Making It Snow! Xamarin.Forms and CocosSharp and Particles

Making It Snow! Xamarin.Forms and CocosSharp and Particles

It goes without saying that I’m a big fan of Xamarin.Forms. It’s the first framework I evaluate when taking on a new project. I love all of the out of the box goodness it provides – easy MVVM (especially the binding), layout engine, and now it’s very easy to get down to the platform – without necessarily having to resort to renderers. But … I also like the look of full screen apps that are, how should I say … a little more pizazz-y. You know the ones – they’re full screen, lots of cool graphics, maybe some “things” animating about the screen. Something that looks a bit like the apps below. Those apps are cool … but surely one couldn’t do that with Xamarin.Forms … right? Right? Wrong! Those apps are both done with Forms – and both are running pretty much with the normal Forms elements – Button, Grid, some data binding. But to give it a bit of pizazz – there’s some CocosSharp running in it as well. And it’s pretty easy to get setup and running. So let’s take a look at how to do so! (Full source can be found on GitHub here.) Hello CocosSharp The key to getting that cool background and those animations is CocosSharp. My inspiration for all of this was the weather app Carrot. If it’s snowing, it shows some snow falling – if it’s raining, it shows some rain… etc. I wanted to see if I could reproduce that with Xamarin.Forms. CocosSharp is a game development framework. It’s fully featured (I’m talking physics engine and everything). If you desire,...
Bindable Native Views in XAML – With Commands!?!

Bindable Native Views in XAML – With Commands!?!

Back when I wrote the post on the awesomeness that is binding platform specific, aka native, views directly in Xamarin.Forms XAML one of the things missing were Commands. For example, no tying button clicks to a Command in a view model. Using the UpdateSourceEventName attribute, you can introduce two way binding by having Forms listen for the event specified and then update the bindings when said event is fired. However, that doesn’t help us when our app needs to react and take an action when an event occurs on the native view embedded within the XAML. So what’s a developer to do? One way around this is to embed the native control in the code behind file and then all the events will be available for use. Handle that event, then invoke whatever command you want in your view-model. That works – but it isn’t optimal. You have to mix code-behind with XAML … AND … either use Shared Projects or make creative use of partial classes. There has to be a better way … and there is … Commanding From the Native View! Or Subclassing to the Rescue! The solution to the problem really is rather simple and it evolves subclassing. All the demo code and a working solution can be found on GitHub here. In the previous post on native XAML embedding with data binding, I mentioned there was a new extension method called SetBinding on UIView and Android.View objects. It’s this method the Xamarin.Forms XAML parser calls when it comes across a native property definition specified in the XAML to provide data binding. So … what...
Xamarin Forms – Bindable Picker(!!)

Xamarin Forms – Bindable Picker(!!)

I’m rather starting to like doing these look aheads to features coming out in future versions of Xamarin.Forms. It’s really cool tracing through the source code to see how a new feature is being implemented and coming together. And today we’re going to talk about a feature that I keep trying to use, thinking it’s already implemented in Forms… but every time I try, I slap my forehead and remember it’s not there yet … binding a data source to a Picker !! Everything I talk about in this article is dependent upon Xamarin.Forms 2.3.4 – which at the time of this writing (November 29, 2016) is not on NuGet yet. You will need to build from source in order to use it right now. When it does show up on NuGet if there are any changes, I will keep this blog updated. tl;dr; All the code from this post can be found on GitHub here. If you’re only here for the goodness of how to data bind to a Picker … first bind an IList collection to the ItemsSource property. Then bind whatever you want receiving updates to the SelectedItem. Finally, ItemDisplayBinding can receive another binding (from a property within the class’s that make up the IList bound to the ItemsSource), that will determine what gets displayed. Read on to get the full run down of the new data binding! New Properties There are 3 new properties introduced to the Picker as part of the new data binding functionality (more if you count the backing static BindableProperty properties, but we don’t need to directly address those). ItemsSource, SelectedItem,...