Just a quick follow up on the post that I published where I took some baby steps in using lighting and shadows with the Visual Layer on Windows 10 1607.
Windows 10, UWP and Composition–Light and Shade
In that post, I’d failed to get a simple XAML scene lit with a PointLight and then James pinged me on Twitter saying;

and I’d actually seen the sample in question which shimmers some text;

and I hadn’t realised that it was a XAML based sample so I stared at it and it took me maybe 2-3 minutes to figure out what’s different about it from my attempt and I thought it was worth sharing what it taught me.
My attempt to do lighting with XAML involved a piece of ‘UI’ like this;
<Grid
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
x:Name="grid">
<Image
x:Name="image"
Source="assets\img102.jpg"
Stretch="UniformToFill" />
</Grid>
and then some code-behind which looks like this;
namespace App23
{
using System.Numerics;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded += OnLoaded;
}
void OnLoaded(object sender, RoutedEventArgs e)
{
var gridVisual = ElementCompositionPreview.GetElementVisual(this.grid);
var compositor = gridVisual.Compositor;
var pointLight = compositor.CreatePointLight();
pointLight.CoordinateSpace = gridVisual;
pointLight.Color = Colors.Yellow;
pointLight.Offset = new Vector3(
(float)this.grid.ActualWidth / 2,
(float)this.grid.ActualHeight / 2,
50);
pointLight.Targets.Add(gridVisual);
}
}
}
Now, this doesn’t work for me and the fact that it didn’t work caused me to take the classic programmer’s thinking path of “Somebody Else’s Problem”;
“ah, this hasn’t worked so I have to assume that there must be some limitation in the composition layer which means that you can’t apply lighting to XAML elements”
and it was pretty easy for me to put it to one side and go and do something else instead.
However, if I’d looked at the ‘Text Shimmer’ sample beforehand I’d have been confident that the composition layer worked fine and it was my own code that was broken.
What was broken about it? Object lifetimes. Here’s code that works for me. Hopefully it’s not a ‘red-herring’ 
namespace App23
{
using System.Numerics;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded += OnLoaded;
}
void OnLoaded(object sender, RoutedEventArgs e)
{
var gridVisual = ElementCompositionPreview.GetElementVisual(this.grid);
var compositor = gridVisual.Compositor;
this.pointLight = compositor.CreatePointLight();
this.pointLight.CoordinateSpace = gridVisual;
this.pointLight.Color = Colors.Yellow;
this.pointLight.Offset = new Vector3(
(float)this.grid.ActualWidth / 2,
(float)this.grid.ActualHeight / 2,
50);
this.pointLight.Targets.Add(gridVisual);
}
PointLight pointLight;
}
}
and all I had to do was promote the PointLight variable to be a member of my Page and life is good and the light does what I expect it to.
Now, to be honest, I’ve hit this type of issue working with the composition APIs before and so I should have expected it and fixed my code but I think my intuitive expectation was that having created a light and handed it ‘to the system’ I’d expect the system to keep hold of it rather than asking me to keep hold of it.
Maybe that expectation comes from me applying XAML-like ‘retained graphics’ thinking that doesn’t necessarily apply here and this is a slight odd scenario because;
- I ask the Compositor to create the PointLight
- I ask the light to target a Visual
- I don’t have to ‘insert’ the light into any kind of scene or anything like that
and so I hadn’t assumed that I was the owner of the PointLight and needed to keep it around although it’s worth saying that the PointLight is disposable so maybe that should have made it clear that the ownership lives with me as a consumer of the light.
That’s my learning for this post 
In looking around this area of ownership, I did spot that others are asking questions around object lifetimes and disposing of some of these pieces so it’s something that docs/guides could perhaps do more to help users of the APIs here understand how the lifetimes are meant to work;

but at least on this simplest of samples I got resolution as to why my code didn’t work when the sample code did!