Having done limited rollout yesterday I noticed a strange pattern. A huge number of crashes with “System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw”
The most interesting bit was that the failures were only mostly on phones and not on desktop devices.
What the heck!! looking inside didn’t help much. The crash group on HockeyApp also mentioned ‘invalidoperation_novalue‘ Okay this was strange.. In another instance I had a similar invalidoperation_novalue but somewhat different location ‘System.Nullable$1.get_Value ‘
In a few places I was using nullable int. that translates to System.UInt32
int? pos = null;
// elsewhere read it
var item = list[pos.HasValue ? pos.Value : 0];
// in other places I encountered some exceptions - can't remember what and I used null check rather than HasValue
var item2 = list[pos != null ? pos.Value : 0];
//or in newer iteration of csharp
var item3 = list[pos?.Value??0];
Somehow I was directly using instance of nullable directly. A few posts helped. I was sure that was the problem. Thanks to Carsten Schuette for showing the use of GetValueOrDefault
In November 2015, I joined MailOnline and created their first Windows Phone and Windows 10 app. It was a standard XAML app following MVVM pattern etc etc. The design was tried and tested first on iOS and then on Android. There were discussion on a clean new design and being the smallest platform, new design Windows 10 app were approved in March 2016. Fast forward 9 months and Windows 10 app rework has been pushed to store today with a limited rollout 5% users
I mentioned that the design is different – it follows the curated style on the website closely. Not only that I have followed a new style myself – I created this app as an SPA (Single Page Application). With WinRT API page caching is disabled by default and maintaining scroll positions etc is PITA. In past I have seen ListView behaving oddly when page caching is turned on. To get away from this, I created this app as an SPA. No forward backward navigation – its more like show / hide correct content. Its a big experiment on my side. Above and beyond all else two libraries played key role in creating this app
ReactiveUI My first go at using RX ever. It has made many things so simple. I still use event wiring in a few places but I will try to remove those over coming versions. UWP Community Toolkit In previous version trying to tune the caching was a pain. In this version thanks to the toolkit I have multiple caches (image, video and json) and image ready for offline usage.
In the first version of UWP Community Toolkit, we only had ImageCache which had its origin in Windows App Studio. A few issues were raised to optimise it and one mentioned extensible cache that can be used to create any case.
FileCache, ImageCache, VideoCache, JsonCache.. you name it.. Yesterday I mentioned CacheBase. FileCache and ImageCache that ship with UWP Community Toolkit are implementations of CacheBase by giving it a specific type.
Today I had to implement ability to pull configuration settings from our server. I tried using current prod version of FileCache but my implementation was somewhat wrong there. It would try to create File from Stream and return null and then fail internally (fixed in current dev branch) however I needed something today. Enter ConfigCache.. well JsonCache really
public class ConfigCache : CacheBase<ConfigurationSetting>
{
JsonSerializer jsonSerializer = new JsonSerializer();
/// <summary>
/// Private singleton field.
/// </summary>
private static ConfigCache _instance;
/// <summary>
/// Gets public singleton property.
/// </summary>
public static ConfigCache Instance => _instance ?? (_instance = new ConfigCache() { MaintainContext = false });
protected override async Task<ConfigurationSetting> InitializeTypeAsync(StorageFile baseFile)
{
using (var stream = await baseFile.OpenStreamForReadAsync())
{
return InitializeTypeAsync(stream);
}
}
protected override Task<ConfigurationSetting> InitializeTypeAsync(IRandomAccessStream stream)
{
var config = InitializeTypeAsync(stream.AsStream());
return Task.FromResult<ConfigurationSetting>(config);
}
private ConfigurationSetting InitializeTypeAsync(Stream stream)
{
var reader = new StreamReader(stream);
using (var jsonReader = new JsonTextReader(reader))
{
return jsonSerializer.Deserialize<ConfigurationSetting>(jsonReader);
}
}
}
In this case I am using a specific type to deserialise json to. How do I use it ?
Earlier today (10 mins ago), we were discussing looping animated previews (rolling video frames) for articles and that maybe we should create a cache to prevent it being downloaded over and over again.
Back in UWP Community Toolkit, we created a CacheBase – a generic base implementation of Cache. This can be used to create just about any type of data cache.
This CacheBase was then used to implement ImageCache and finally FileCache just to show it can be done with other types too. So this is what I did.
Windows 10 WinRT API introduced ability to query whether a certain class, method, property etc were supported during run-time or not. This would allow applications to conditionally do things rather than crashing out due to unsupported API.
Since the initial release, many APIs have been introduced, Composition layer has seen many enhancements. Controls like MediaElementPlayer which return the load on UI thread by directly targeting GPU have been introduced. Prior to that developers used MediaElement control that was introduced in Windows 8 / Windows Phone 8.1 and was included in UWP API 10.0.10240.0. To take advantage of better performing control on updated devices, we need to use newer control.
public sealed partial class VideoPlayerUC : UserControl
{
FrameworkElement mediaControl = null;
private bool UseMediaPlayerElement =>
ApiInformation.IsTypePresent("Windows.UI.Xaml.Controls.MediaPlayerElement");
public VideoPlayerUC()
{
this.InitializeComponent();
if (this.UseMediaPlayerElement)
{
mediaControl = new MediaPlayerElement();
}
else
{
mediaControl = new MediaElement();
}
this.Root.Children.Add(mediaControl);
}
public Video Video
{
get { return (Video)GetValue(VideoProperty); }
set { SetValue(VideoProperty, value); }
}
// Using a DependencyProperty as the backing store for Video. This enables animation, styling, binding, etc...
public static readonly DependencyProperty VideoProperty =
DependencyProperty.Register("Video",
typeof(Video),
typeof(VideoPlayerUC),
new PropertyMetadata(null, OnVideoChanged));
private static void OnVideoChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as VideoPlayerUC;
control?.HandleVideoChange();
}
private void HandleVideoChange()
{
if (this.Video == null)
{
return;
}
MediaSource mediaSource = MediaSource.CreateFromUri(this.Video.Uri);
MediaPlaybackItem playbackItem = new MediaPlaybackItem(mediaSource);
if (this.UseMediaPlayerElement)
{
var mpe = mediaControl as MediaPlayerElement;
mpe.Stretch = Stretch.Uniform;
mpe.AreTransportControlsEnabled = this.Video.ShowControls;
mpe.Source = playbackItem;
mpe.PosterSource = new BitmapImage() { UriSource = this.Video.Images.Still.Uri };
mpe.AutoPlay = Video.AutoPlay;
if (mpe.MediaPlayer != null)
{
mpe.MediaPlayer.IsLoopingEnabled = Video.IsLooping;
}
}
else
{
var me = mediaControl as MediaElement;
me.Stretch = Stretch.Uniform;
me.AreTransportControlsEnabled = this.Video.ShowControls;
me.SetPlaybackSource(playbackItem);
me.PosterSource = new BitmapImage() { UriSource = this.Video.Images.Still.Uri };
me.AutoPlay = Video.AutoPlay;
me.IsLooping = Video.IsLooping;
}
}
}
what I however noticed was that the playback controls (aptly called Transport controls) were very flaky.. Here’s an example.. they’d work some times and not at other times (read most of the times)
I was really baffled by this behaviour.. I reworked the user control a few times. I moved to using PlayerFramework but its styling is Windows 8 and that was rather annoying trying to style.
public sealed class VideoControls : MediaTransportControls
{
public VideoControls()
{
this.DefaultStyleKey = typeof(VideoControls);
}
}
Set the XAML to default MediaTransport Style.
Now instantiate this and add that to the MediaPlayerElement / MediaElement and all was good except it was looking a bit rough. So time to style it like my design team wanted it to look. Here’s the final style
VideoControls controls = new VideoControls();
mpe.TransportControls = controls;
On 1st of Jan 2017 I got a mail from Microsoft stating that I have been awarded an MVP award for my community contributions for Windows Development.
I am honoured to have been nominated and to have been awarded this. For a developer in Microsoft technologies this is the highest honour (sort of). Thank you @WindowsDev and @MVPAward
I think 2016 was a mixed bag and technically I did little to have deserved this honour. I have been a contributor to the UWP Community Toolkit and that was the primary driver. I am sure I did substantially more from 2011 – 2015 but hey maybe that’s what it took 🙂 still feels surreal
Going ahead I will try to blog more about #UWP and #UWPDev in 2017. Happy new year all
The only reason I install Chrome now and again is to use extensions like Json Formatter extension. Recently Microsoft released Windows 10 Anniversary update.
Edge browser on this update supports extensions. This list of extensions on store is still limited. The team is analysing the performance and publishing them accordingly.
Can’t wait any longer. I started by cloning Json-Formatter repo and used Microsoft Edge Extension Toolkit to port the extension to work on Edge.
You can download the repo and install the extension after enabling Extension developer mode from ‘about:flags’
Controls like ListView / GridView contain a ScrollViewer which hosts scrollable content. The ScrollViewer contains two ScrollBar controls one for each scroll type (horizontal and vertical).
You need to go and Edit the template for ScrollViewer next.
You can right click on the ListView, tap Esc key which then moves focus on ScrollViewer. Click control’s Border and right click > Edit Template > Edit a copy
In one instance I needed the ScrollBar to not render over the content. You can see that the ScrollViewer ScrollViewerPresenter has both ColSpan and RowSpan set. Just get rid of it so there is no overlay.
In Another instance I needed to customise it further. I needed Horizontal ScrollBar (to be of same height as the ListView) and I needed only the Left and Right buttons. So start by Setting ColSpan / RowSpan on ScrollBar as needed
Now lets get to ScrollBar template to customise it further.
In my case I only needed the two RepeatButton named HorizontalSmallDecrease and HorizontalSmallIncrease. Comment out the remaining controls (VerticalTrackRect, VerticalLargeDecrease, VerticalThumb, VerticalLargeIncrease)
For smoother scroll, modify RepeatButton’s Interval property, I set it to 5 rather than 50.
For further customisation I even created Attached Property that resizes the RepeatButton (the visibility is controlled by the ScrollBar so setting width while a hack works well.
public static class ScrollBarHelper
{
static double InitialWidth = Double.MinValue;
public static readonly DependencyProperty DCustomiseScrollBehaviourProperty =
DependencyProperty.RegisterAttached("CustomiseScrollBehaviour", typeof(bool),
typeof(ScrollBarHelper), new PropertyMetadata(false, OnCustomiseScrollBehaviourPropertyChanged));
public static void SetCustomiseScrollBehaviour(DependencyObject d, bool value)
{
d.SetValue(DCustomiseScrollBehaviourProperty, value);
}
public static bool GetCustomiseScrollBehaviour(DependencyObject d)
{
return (bool)d.GetValue(DCustomiseScrollBehaviourProperty);
}
private static void OnCustomiseScrollBehaviourPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
if (!(bool)e.NewValue)
return;
var control = d as ScrollBar;
if (control != null)
{
Observable.FromEventPattern<RoutedEventArgs>(control, "Loaded")
.SubscribeOn(TaskPoolScheduler.Default)
.ObserveOn(CoreDispatcherScheduler.Current)
.Subscribe(args =>
{
ProcessPosition(args.Sender);
});
Observable.FromEventPattern<ScrollEventArgs>(control, "Scroll")
.Throttle(TimeSpan.FromMilliseconds(50))
.SubscribeOn(TaskPoolScheduler.Default)
.ObserveOn(CoreDispatcherScheduler.Current)
.Subscribe(args =>
{
ProcessPosition(args.Sender);
});
}
}
private static void ProcessPosition(object sender)
{
ScrollBar sb = (sender as ScrollBar);
RepeatButton rbLeft = (RepeatButton)sb.FindDescendantByName("HorizontalSmallDecrease");
RepeatButton rbRight = (RepeatButton)sb.FindDescendantByName("HorizontalSmallIncrease");
if (rbLeft == null || rbRight == null)
return;
double pos = sb.Value;
if (pos == 0 && InitialWidth == Double.MinValue)
{
InitialWidth = rbLeft.Width;
}
if (Math.Abs(pos - sb.Minimum) <= 15)
{
// hide the left scroll button
rbLeft.Width = 0;
}
else if (Math.Abs(pos - sb.Maximum) <= 15)
{
// hide right scroll button
rbRight.Width = 0;
}
else
{
rbLeft.Width = rbRight.Width = InitialWidth;
}
}
}
You could directly attached to ScrollBar’s Loaded / Scroll events but I used Reactive Extensions to throttle scroll changes as I wanted scroll experience to not degrade. The result.. not perfect but close to what I needed
For some reason I find myself doing some Windows Phone 8.1 development in Silverlight.. I know its a dead end and all the rest but I am working on a small app and Silverlight API was more mature at least for Windows Phone 8.1. One of the things I was adding was GeoFencing. GeoFencing was introduced to Windows Phone 8.1 WinRT API and some of it was exposed to Silverlight API (whoever made the decision on what APIs were available in WinRT and what in Silverlight needs to be shot.. at least made to feel real pain.
The way you set it up is you create GeoFence instances and add them.. System managed associated with your app. The app can access it from foreground or background.. nice right ? The constructor for GeoFence states that it requires IGeoshape along with an identifier at the very least.. IGeoshape is an enum which has possible values of Point, Circle, BoundingBox and Path. The constructor for GeoFence explicitly states now and again that the IGeoshape has to be a valid GeoCircle.. I know documentation can be wrong.. I mean why would you go through the hassle of create an Interface and passing it again if you enforce explicit type ?
I was wrong.. I tried it both the Silverlight and through WinRT API.. nope documentation is correct.. the API though designed well has shite implementation 😐 So what next ?
Well DIY. Here is my implementation.. Create a Rect instance with NorthWest X1, Y1 and SouthEast and X2 Y2 and monitor the change in geo position. Check if the new point is within Rect or not.. easy peasy.. well it works too.. Here is my implementation.
public class GeoMonitoringService
{
Geolocator _locator = null;
Dictionary<GeoSite, Rect> _locationRectangles = null;
DataService _dataService = null;
public event RoutedPropertyChangedEventHandler<GeomonitorStateChangedEventArgs> GeomonitorStateChanged;
public bool lastState = false;
public void Initialise(DataService dataService)
{
_locator = new Geolocator();
_locator.DesiredAccuracy = PositionAccuracy.High;
_locator.MovementThreshold = 1;
_locator.ReportInterval = Convert.ToUInt32(TimeSpan.FromSeconds(5).TotalMilliseconds);
this._locationRectangles = new Dictionary<GeoSite, Rect>();
this._dataService = dataService;
if (this._dataService?.SiteDictionary?.Count > 0)
{
foreach (var site in this._dataService.SiteDictionary.Values)
{
Rect r = new Rect(new Point(site.Latitude1, site.Longitude1), new Point(site.Latitude2, site.Longitude2));
this._locationRectangles.Add(site, r);
}
}
_locator.PositionChanged += GPSPositionChanged;
}
private void GPSPositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
var geoPoint = args.Position.Coordinate.Point;
Point p = new Point(geoPoint.Position.Latitude, geoPoint.Position.Longitude);
KeyValuePair<GeoSite, Rect>? matchKVP = null;
foreach (var entry in this._locationRectangles)
{
if (entry.Value.Contains(p))
{
matchKVP = entry;
break;
}
}
if (matchKVP.HasValue)
{
if (!lastState)
{
lastState = true;
this.GeomonitorStateChanged?.Invoke(this, new RoutedPropertyChangedEventArgs<GeomonitorStateChangedEventArgs>(null, new GeomonitorStateChangedEventArgs(true, matchKVP.Value.Key)));
}
}
else
{
if (lastState)
{
lastState = false;
this.GeomonitorStateChanged?.Invoke(this, new RoutedPropertyChangedEventArgs<GeomonitorStateChangedEventArgs>(null, new GeomonitorStateChangedEventArgs(false, null)));
}
}
}
}