Encoding two ints into a long to be used as navigation parameter
When navigating in a Windows Store app, you can specify a navigation parameter. This navigation parameter can be any object but there is a problem. When the application is suspended, this navigation parameter is saved by the LayoutAwarePage. This works only with primitive data types, unless you change the implementation of the class which instances you pass as the navigation parameter.
I use mainly ints (article id, gallery id …) as navigation parameters but sometimes just on int is not enough. If I want to pass two int parameters, I “encode” them into an int and then “decode” back on the target page. I have a simple Utils class for this purpose.
public static class Utils
{
public static long MakeLong(int left, int right)
{
long res = left;
res = (res << 32);
res = res | (long)right;
return res;
}
public static Tuple<int, int> MakeTwoInts(long x)
{
int a = (int)(x & 0xffffffffL);
int b = (int)(x >> 32);
return Tuple.Create(b, a);
}
}
See also
- The death of the WinRT developer?
- Leveraging ETag caching in Windows Phone and Windows apps
- Changing the PivotItem header color in Windows Phone 8.1 XAML
- Dialog helper for Universal Apps the easy way
- Creating a fake splashscreen for your Universal App