public void TryGetState_Exception_IncorrectType()
        {
            PageInfo navigationEntry = new PageInfo("Page Name", "Arguments");
            navigationEntry.SetState<string>("MyKey", "Test State");

            int result;
            var e = Assert.Throws<InvalidCastException>(() => navigationEntry.TryGetState<int>("MyKey", out result));

            Assert.Equal("Specified cast is not valid.", e.Message);
        }
        public void SetState_SameStateKeyOverwritesPreviousValue()
        {
            PageInfo navigationEntry = new PageInfo("Page Name", "Arguments");

            navigationEntry.SetState<string>("MyKey", "State A");
            navigationEntry.SetState<string>("MyKey", "State B");

            string result;
            bool success = navigationEntry.TryGetState<string>("MyKey", out result);

            Assert.Equal(true, success);
            Assert.Equal("State B", result);
        }
        public void TryGetState_ReturnsFalseIfKeyIsUndefined()
        {
            PageInfo navigationEntry = new PageInfo("Page Name", "Arguments");

            string result = "Old Value";
            bool success = navigationEntry.TryGetState<string>("Undefined", out result);

            Assert.Equal(false, success);
            Assert.Equal(null, result);
        }
        public void TryGetState_Exception_KeyIsEmpty()
        {
            PageInfo navigationEntry = new PageInfo("Page Name", "Arguments");

            string result;
            var e = Assert.Throws<ArgumentException>(() => navigationEntry.TryGetState<string>("", out result));

            Assert.Equal("The argument cannot be null or an empty string.\r\nParameter name: key", e.Message);
            Assert.Equal("key", e.ParamName);
        }
        public void TryGetSetState_Struct_StoresState()
        {
            PageInfo navigationEntry = new PageInfo("Page Name", "Arguments");

            navigationEntry.SetState<StructState>("MyKey", new StructState() { Text = "Text Value", Number = 42 });
            StructState result;
            bool success = navigationEntry.TryGetState<StructState>("MyKey", out result);

            Assert.Equal(true, success);
            Assert.Equal("Text Value", result.Text);
            Assert.Equal(42, result.Number);
        }
        public void TryGetSetState_Class_StoresNullState()
        {
            PageInfo navigationEntry = new PageInfo("Page Name", "Arguments");

            navigationEntry.SetState<ClassState>("MyKey", null);
            ClassState result;
            bool success = navigationEntry.TryGetState<ClassState>("MyKey", out result);

            Assert.Equal(true, success);
            Assert.Equal(null, result);
        }
        public void TryGetSetState_Int_StoresState()
        {
            PageInfo navigationEntry = new PageInfo("Page Name", "Arguments");

            navigationEntry.SetState<int>("MyKey", 42);
            int result;
            bool success = navigationEntry.TryGetState<int>("MyKey", out result);

            Assert.Equal(true, success);
            Assert.Equal(42, result);
        }
        public void TryGetSetState_String_StoresState()
        {
            PageInfo navigationEntry = new PageInfo("Page Name", "Arguments");

            navigationEntry.SetState<string>("MyKey", "Test State");
            string result;
            bool success = navigationEntry.TryGetState<string>("MyKey", out result);

            Assert.Equal(true, success);
            Assert.Equal("Test State", result);
        }
示例#9
0
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="pageInfo">Information on the arguments and state passed to the page.</param>
        public async void Activate(PageInfo pageInfo)
        {
            // TODO: Create an appropriate data model for your problem domain to replace the sample data

            string groupId = pageInfo.GetArguments<string>();
            var group = await SampleDataSource.GetGroupAsync(groupId);
            this.Group = group;
            this.Items = group.Items;

            // Restore the selected item

            string selectedItemId;

            if (pageInfo.TryGetState<string>("SelectedItem", out selectedItemId))
                SelectedItem = await SampleDataSource.GetItemAsync(selectedItemId);
            else
                SelectedItem = null;
        }