public QueryItemSelectPage(PhoneApplicationPage page)
            : base(page)
        {
            var bar = new ApplicationBar();

            string queryId, attrName;
            page.NavigationContext.QueryString.TryGetValue("query", out queryId);
            page.NavigationContext.QueryString.TryGetValue("attribute", out attrName);

            if (!string.IsNullOrEmpty(attrName))
            {
                var parent = Client.CurrentClient.GetCachedObject<PersistentObject>(page.NavigationContext.QueryString["parent"]);
                var attribute = parent.GetAttribute(attrName);

                referenceAttribute = attribute as PersistentObjectAttributeWithReference;
                if (referenceAttribute != null)
                    Lookup = referenceAttribute.Lookup;
                else if (attribute.Type == DataTypes.DateTimeOffset || attribute.Type == DataTypes.NullableDateTimeOffset)
                {
                    dateTimeOffsetAttribute = attribute;
                    Lookup = Client.CurrentClient.GetCachedObject<Query>("94b37097-6496-4d32-ae0b-99770defa828");
                    Lookup.Label = Service.Current.Messages["ChooseTimeZone"].ToUpper();
                }

                SelectionMode = SelectionMode.Single;
            }
            else if (queryId != null)
            {
                query = Client.CurrentClient.GetCachedObject<Query>(queryId);
                Lookup = Service.Current.Hooks.OnConstruct(query.Model, query.Parent, true);
                Lookup.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;

                SelectionMode = SelectionMode.Multiple;

                okButton = new ApplicationBarIconButton(new Uri("/Assets/ActionIcons/OK.png", UriKind.RelativeOrAbsolute))
                           {
                               Text = Service.Current.Messages["Select"],
                               IsEnabled = false
                           };
                okButton.Click += OkButton_Click;
                bar.Buttons.Add(okButton);
            }

            var cancelButton = new ApplicationBarIconButton(new Uri("/Assets/ActionIcons/Cancel.png", UriKind.RelativeOrAbsolute))
                               {
                                   Text = Service.Current.Messages["Cancel"],
                                   IsEnabled = true
                               };
            cancelButton.Click += delegate { Client.RootFrame.GoBack(); };
            bar.Buttons.Add(cancelButton);

            if (Lookup != null)
            {
                Lookup.OpenItem += Lookup_OnOpenItem;
                Lookup.NotificationChanged += Lookup_NotificationChanged;
#pragma warning disable 4014
                Lookup.SearchTextAsync(null);
#pragma warning restore 4014

                var filterAction = Lookup.Actions.OfType<Filter>().FirstOrDefault();
                if (filterAction != null)
                {
                    var searchButton = new ApplicationBarIconButton(new Uri("/Assets/ActionIcons/Filter.png", UriKind.RelativeOrAbsolute))
                                       {
                                           Text = filterAction.DisplayName,
                                           IsEnabled = true
                                       };
                    searchButton.Click += delegate { IsSearchOpen = true; };
                    bar.Buttons.Add(searchButton);
                }
            }

            page.ApplicationBar = bar;
        }
        internal async Task Initialize(string arg)
        {
            if (Client.CurrentClient.HasSearch)
                SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;

            var key = JObject.Parse(arg);

            previousState = (string)key["PreviousState"];
            var attrName = (string)key["Attribute"];
            if (!string.IsNullOrEmpty(attrName))
            {
                var parent = Client.CurrentClient.GetCachedObject<PersistentObject>((string)key["Parent"]);
                referenceAttribute = parent.GetAttribute(attrName) as PersistentObjectAttributeWithReference;
                if (referenceAttribute != null)
                    Lookup = (StoreQuery)referenceAttribute.Lookup;

                ListViewSelectionMode = ListViewSelectionMode.Single;
            }
            else if (!string.IsNullOrEmpty((string)key["Query"]))
            {
                query = Client.CurrentClient.GetCachedObject<Query>((string)key["Query"]);
                Lookup = (StoreQuery)Service.Current.Hooks.OnConstruct(query.Model, query.Parent, true);

                ListViewSelectionMode = ListViewSelectionMode.Multiple;
                mode = SelectMode.Query;
            }
            else
            {
                targetProgramUnit = (string)key["targetProgramUnit"];
                targetProgramUnitGroup = (string)key["targetProgramUnitGroup"];
                Lookup = Client.CurrentClient.GetCachedObject<StoreQuery>((string)key["AdministratorAddQueriesQuery"]);

                ListViewSelectionMode = ListViewSelectionMode.Multiple;
                mode = SelectMode.AddQueriesAsProgramUnits;
            }

            if (Lookup != null)
            {
                Lookup.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;
                await Lookup.SearchTextAsync(null);
            }

            OnApplicationViewStateChanged();
        }