Best WinAppDriver code snippet using AppUIBasics.Common.SaveStateEventArgs.SaveStateEventArgs
NavigationHelper.cs
Source:NavigationHelper.cs  
...131        public void OnNavigatedFrom(NavigationEventArgs e)132        {133            var frameState = SuspensionManager.SessionStateForFrame(this.Frame);134            var pageState = new Dictionary<string, object>();135            this.SaveState?.Invoke(this, new SaveStateEventArgs(pageState));136            frameState[_pageKey] = pageState;137        }138        #endregion139    }140    /// <summary>141    /// RootFrameNavigationHelper registers for standard mouse and keyboard142    /// shortcuts used to go back and forward. There should be only one143    /// RootFrameNavigationHelper per view, and it should be associated with the144    /// root frame.145    /// </summary>146    /// <example>147    /// To make use of RootFrameNavigationHelper, create an instance of the148    /// RootNavigationHelper such as in the constructor of your root page.149    /// <code>150    ///     public MyRootPage()151    ///     {152    ///         this.InitializeComponent();153    ///         this.rootNavigationHelper = new RootNavigationHelper(MyFrame);154    ///     }155    /// </code>156    /// </example>157    [Windows.Foundation.Metadata.WebHostHidden]158    public class RootFrameNavigationHelper159    {160        private Frame Frame { get; set; }161        SystemNavigationManager systemNavigationManager;162        private Microsoft.UI.Xaml.Controls.NavigationView CurrentNavView { get; set; }163        /// <summary>164        /// Initializes a new instance of the <see cref="RootNavigationHelper"/> class.165        /// </summary>166        /// <param name="rootFrame">A reference to the top-level frame.167        /// This reference allows for frame manipulation and to register navigation handlers.</param>168        public RootFrameNavigationHelper(Frame rootFrame, Microsoft.UI.Xaml.Controls.NavigationView currentNavView)169        {170            this.Frame = rootFrame;171            this.Frame.Navigated += (s, e) =>172            {173                // Update the Back button whenever a navigation occurs.174                UpdateBackButton();175            };176            this.CurrentNavView = currentNavView;177            // Handle keyboard and mouse navigation requests178            this.systemNavigationManager = SystemNavigationManager.GetForCurrentView();179            systemNavigationManager.BackRequested += SystemNavigationManager_BackRequested;180            // must register back requested on navview181            if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 6))182            {183                CurrentNavView.BackRequested += NavView_BackRequested;184            }185            // Listen to the window directly so we will respond to hotkeys regardless186            // of which element has focus.187            Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated +=188                CoreDispatcher_AcceleratorKeyActivated;189            Window.Current.CoreWindow.PointerPressed +=190                this.CoreWindow_PointerPressed;191        }192        private void NavView_BackRequested(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewBackRequestedEventArgs args)193        {194            TryGoBack();195        }196        private bool TryGoBack()197        {198            // don't go back if the nav pane is overlayed199            if (this.CurrentNavView.IsPaneOpen && (this.CurrentNavView.DisplayMode == Microsoft.UI.Xaml.Controls.NavigationViewDisplayMode.Compact || this.CurrentNavView.DisplayMode == Microsoft.UI.Xaml.Controls.NavigationViewDisplayMode.Minimal))200            {201                return false;202            }203            bool navigated = false;204            if (this.Frame.CanGoBack)205            {206                this.Frame.GoBack();207                navigated = true;208            }209            210            return navigated;211        }212        private bool TryGoForward()213        {214            bool navigated = false;215            if (this.Frame.CanGoForward)216            {217                this.Frame.GoForward();218                navigated = true;219            }220            return navigated;221        }222        private void SystemNavigationManager_BackRequested(object sender, BackRequestedEventArgs e)223        {224            if (!e.Handled)225            {226                e.Handled = TryGoBack();227            }228        }229        private void UpdateBackButton()230        {231            if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 6))232            {233                this.CurrentNavView.IsBackEnabled = this.Frame.CanGoBack ? true : false;234            } else235            {236                systemNavigationManager.AppViewBackButtonVisibility = this.Frame.CanGoBack ? AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed;237            }238            239        }240        /// <summary>241        /// Invoked on every keystroke, including system keys such as Alt key combinations.242        /// Used to detect keyboard navigation between pages even when the page itself243        /// doesn't have focus.244        /// </summary>245        /// <param name="sender">Instance that triggered the event.</param>246        /// <param name="e">Event data describing the conditions that led to the event.</param>247        private void CoreDispatcher_AcceleratorKeyActivated(CoreDispatcher sender,248            AcceleratorKeyEventArgs e)249        {250            var virtualKey = e.VirtualKey;251            // Only investigate further when Left, Right, or the dedicated Previous or Next keys252            // are pressed253            if ((e.EventType == CoreAcceleratorKeyEventType.SystemKeyDown ||254                e.EventType == CoreAcceleratorKeyEventType.KeyDown) &&255                (virtualKey == VirtualKey.Left || virtualKey == VirtualKey.Right ||256                (int)virtualKey == 166 || (int)virtualKey == 167))257            {258                var coreWindow = Window.Current.CoreWindow;259                var downState = CoreVirtualKeyStates.Down;260                bool menuKey = (coreWindow.GetKeyState(VirtualKey.Menu) & downState) == downState;261                bool controlKey = (coreWindow.GetKeyState(VirtualKey.Control) & downState) == downState;262                bool shiftKey = (coreWindow.GetKeyState(VirtualKey.Shift) & downState) == downState;263                bool noModifiers = !menuKey && !controlKey && !shiftKey;264                bool onlyAlt = menuKey && !controlKey && !shiftKey;265                if (((int)virtualKey == 166 && noModifiers) ||266                    (virtualKey == VirtualKey.Left && onlyAlt))267                {268                    // When the previous key or Alt+Left are pressed navigate back269                    e.Handled = TryGoBack();270                }271                else if (((int)virtualKey == 167 && noModifiers) ||272                    (virtualKey == VirtualKey.Right && onlyAlt))273                {274                    // When the next key or Alt+Right are pressed navigate forward275                    e.Handled = TryGoForward();276                }277            }278        }279        /// <summary>280        /// Invoked on every mouse click, touch screen tap, or equivalent interaction.281        /// Used to detect browser-style next and previous mouse button clicks282        /// to navigate between pages.283        /// </summary>284        /// <param name="sender">Instance that triggered the event.</param>285        /// <param name="e">Event data describing the conditions that led to the event.</param>286        private void CoreWindow_PointerPressed(CoreWindow sender,287            PointerEventArgs e)288        {289            var properties = e.CurrentPoint.Properties;290            // Ignore button chords with the left, right, and middle buttons291            if (properties.IsLeftButtonPressed || properties.IsRightButtonPressed ||292                properties.IsMiddleButtonPressed)293                return;294            // If back or forward are pressed (but not both) navigate appropriately295            bool backPressed = properties.IsXButton1Pressed;296            bool forwardPressed = properties.IsXButton2Pressed;297            if (backPressed ^ forwardPressed)298            {299                e.Handled = true;300                if (backPressed) this.TryGoBack();301                if (forwardPressed) this.TryGoForward();302            }303        }304    }305    /// <summary>306    /// Represents the method that will handle the <see cref="NavigationHelper.LoadState"/>event307    /// </summary>308    public delegate void LoadStateEventHandler(object sender, LoadStateEventArgs e);309    /// <summary>310    /// Represents the method that will handle the <see cref="NavigationHelper.SaveState"/>event311    /// </summary>312    public delegate void SaveStateEventHandler(object sender, SaveStateEventArgs e);313    /// <summary>314    /// Class used to hold the event data required when a page attempts to load state.315    /// </summary>316    public class LoadStateEventArgs : EventArgs317    {318        /// <summary>319        /// The parameter value passed to <see cref="Frame.Navigate(Type, object)"/>320        /// when this page was initially requested.321        /// </summary>322        public object NavigationParameter { get; private set; }323        /// <summary>324        /// A dictionary of state preserved by this page during an earlier325        /// session.  This will be null the first time a page is visited.326        /// </summary>327        public Dictionary<string, object> PageState { get; private set; }328        /// <summary>329        /// Initializes a new instance of the <see cref="LoadStateEventArgs"/> class.330        /// </summary>331        /// <param name="navigationParameter">332        /// The parameter value passed to <see cref="Frame.Navigate(Type, object)"/>333        /// when this page was initially requested.334        /// </param>335        /// <param name="pageState">336        /// A dictionary of state preserved by this page during an earlier337        /// session.  This will be null the first time a page is visited.338        /// </param>339        public LoadStateEventArgs(object navigationParameter, Dictionary<string, object> pageState)340            : base()341        {342            this.NavigationParameter = navigationParameter;343            this.PageState = pageState;344        }345    }346    /// <summary>347    /// Class used to hold the event data required when a page attempts to save state.348    /// </summary>349    public class SaveStateEventArgs : EventArgs350    {351        /// <summary>352        /// An empty dictionary to be populated with serializable state.353        /// </summary>354        public Dictionary<string, object> PageState { get; private set; }355        /// <summary>356        /// Initializes a new instance of the <see cref="SaveStateEventArgs"/> class.357        /// </summary>358        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>359        public SaveStateEventArgs(Dictionary<string, object> pageState)360            : base()361        {362            this.PageState = pageState;363        }364    }365}...SaveStateEventArgs
Using AI Code Generation
1using System;2using Windows.UI.Xaml;3using Windows.UI.Xaml.Controls;4using Windows.UI.Xaml.Navigation;5using AppUIBasics.Common;6{7    {8        public Scenario4()9        {10            this.InitializeComponent();11        }12        protected override void OnNavigatedTo(NavigationEventArgs e)13        {14            if (e.Parameter != null)15            {16                SaveStateEventArgs pageState = e.Parameter as SaveStateEventArgs;17                if (pageState != null)18                {19                    if (pageState.PageState.ContainsKey("TextBoxContent"))20                        TextBoxContent.Text = pageState.PageState["TextBoxContent"] as string;21                }22            }23        }24        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)25        {26            SaveStateEventArgs pageState = new SaveStateEventArgs();27            pageState.PageState["TextBoxContent"] = TextBoxContent.Text;28            e.Parameter = pageState;29        }30    }31}32using System;33using Windows.UI.Xaml;34using Windows.UI.Xaml.Controls;35using Windows.UI.Xaml.Navigation;36using AppUIBasics.Common;37{38    {39        public Scenario5()40        {41            this.InitializeComponent();42        }43        protected override void OnNavigatedTo(NavigationEventArgs e)44        {45            if (e.Parameter != null)46            {47                SaveStateEventArgs pageState = e.Parameter as SaveStateEventArgs;48                if (pageState != null)49                {50                    if (pageState.PageState.ContainsKey("TextBoxContent"))51                        TextBoxContent.Text = pageState.PageState["TextBoxContent"] as string;52                }53            }54        }55        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)56        {57            SaveStateEventArgs pageState = new SaveStateEventArgs();58            pageState.PageState["TextBoxContent"] = TextBoxContent.Text;59            e.Parameter = pageState;60        }61    }62}63using System;64using Windows.UI.Xaml;SaveStateEventArgs
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Windows.UI.Xaml;7using Windows.UI.Xaml.Controls;8using Windows.UI.Xaml.Data;9using Windows.UI.Xaml.Documents;10using Windows.UI.Xaml.Input;11using Windows.UI.Xaml.Media;12using Windows.UI.Xaml.Navigation;13using AppUIBasics.Common;14{15    {16        public SaveStateEventArgs()17        {18            this.InitializeComponent();19        }20        protected override void OnNavigatedTo(NavigationEventArgs e)21        {22            var state = e.Parameter as SaveStateEventArgs;23            if (state != null && state.PageState != null)24            {25                if (state.PageState.ContainsKey("textBox"))26                {27                    textBox.Text = state.PageState["textBox"] as string;28                }29            }30        }31        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)32        {33            var state = e.Parameter as SaveStateEventArgs;34            if (state != null)35            {36                state.PageState["textBox"] = textBox.Text;37            }38        }39    }40}41using System;42using System.Collections.Generic;43using System.Linq;44using System.Text;45using System.Threading.Tasks;46using Windows.UI.Xaml;47using Windows.UI.Xaml.Controls;48using Windows.UI.Xaml.Data;49using Windows.UI.Xaml.Documents;50using Windows.UI.Xaml.Input;51using Windows.UI.Xaml.Media;52using Windows.UI.Xaml.Navigation;53using AppUIBasics.Common;54{55    {56        public SaveStateEventArgs()57        {58            this.InitializeComponent();59        }60        protected override void OnNavigatedTo(NavigationEventArgs e)61        {62            var state = e.Parameter as SaveStateEventArgs;63            if (state != null && state.PageSaveStateEventArgs
Using AI Code Generation
1using System;2using Windows.UI.Xaml;3using Windows.UI.Xaml.Controls;4using Windows.UI.Xaml.Navigation;5{6    {7        public NavigationMode NavigationMode { get; private set; }8        public object PageState { get; private set; }9        public SaveStateEventArgs(NavigationMode mode, object state)10        {11            this.NavigationMode = mode;12            this.PageState = state;13        }14    }15    {16        private readonly Dictionary<string, object> pageState = new Dictionary<string, object>();17        private readonly List<WeakReference> registeredEvents = new List<WeakReference>();18        public SaveStateHelper()19        {20        }21        public void RegisterEvents(Page page, NavigationMode mode)22        {23            this.RegisterEvents(page, mode, null);24        }25        public void RegisterEvents(Page page, NavigationMode mode, object state)26        {27            page.Loaded += this.OnPageLoaded;28            page.Unloaded += this.OnPageUnloaded;29            this.pageState[page.GetHashCode().ToString()] = state;30            if (mode == NavigationMode.New)31            {32                this.CleanUpState(page);33            }34        }35        public void UnregisterEvents(Page page)36        {37            page.Loaded -= this.OnPageLoaded;38            page.Unloaded -= this.OnPageUnloaded;39            this.pageState.Remove(page.GetHashCode().ToString());40        }41        public object GetPageState(Page page)42        {43            object state = null;44            this.pageState.TryGetValue(page.GetHashCode().ToString(), out state);45            return state;46        }47        private void OnPageLoaded(object sender, RoutedEventArgs e)48        {49            Page page = sender as Page;50            if (page != null)51            {52                this.CleanUpState(page);53                var frame = page.Parent as Frame;54                if (frame != null)55                {56                    var state = this.GetPageState(page);57                    var args = new SaveStateEventArgs(frame.BackStackDepth > 0 ? NavigationMode.Back : NavigationMode.New, state);58                    this.InvokeHandler(page, "LoadState", args);59                }60            }61        }62        private void OnPageUnloaded(object sender, RoutedEventArgs e)63        {64            Page page = sender as Page;65            if (page != null)66            {67                var frame = page.Parent as Frame;68                if (frame != null)69                {SaveStateEventArgs
Using AI Code Generation
1using System;2using Windows.UI.Xaml;3using Windows.UI.Xaml.Controls;4using Windows.UI.Xaml.Navigation;5using AppUIBasics.Common;6using System.Collections.Generic;7using System.Collections.ObjectModel;8{9    {10        public SaveStateEventArgs()11        {12            this.InitializeComponent();13            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;14        }15        private void SaveStateEventArgs_Loaded(object sender, RoutedEventArgs e)16        {17            if (this.NavigationContext.QueryString.ContainsKey("list"))18            {19                string list = this.NavigationContext.QueryString["list"];20                if (list == "1")21                {22                    this.DefaultViewModel["Items"] = AppUIBasics.SampleDataSource.GetGroup("Group-1").Items;23                }24                else if (list == "2")25                {26                    this.DefaultViewModel["Items"] = AppUIBasics.SampleDataSource.GetGroup("Group-2").Items;27                }28            }29        }30        protected override void OnNavigatedFrom(NavigationEventArgs e)31        {32            e.Parameter = this.DefaultViewModel["Items"];33        }34    }35}36using System;37using Windows.UI.Xaml;38using Windows.UI.Xaml.Controls;39using Windows.UI.Xaml.Navigation;40using AppUIBasics.Common;41using System.Collections.Generic;42using System.Collections.ObjectModel;43{44    {45        public SaveStateEventArgs()46        {47            this.InitializeComponent();48            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;49        }50        private void SaveStateEventArgs_Loaded(object sender, RoutedEventArgs e)51        {52            if (this.NavigationContext.QueryString.ContainsKey("list"))53            {54                string list = this.NavigationContext.QueryString["list"];55                if (list == "1")56                {57                    this.DefaultViewModel["Items"] = AppUIBasics.SampleDataSource.GetGroup("Group-1").Items;58                }59                else if (list == "2")60                {61                    this.DefaultViewModel["Items"] = AppUIBasics.SampleDataSource.GetGroup("Group-2").Items;62                }63            }64        }65        protected override void OnNavigatedFrom(NavigationEventArgs e)66        {SaveStateEventArgs
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Windows.UI.Xaml;7{8    {9        public Dictionary<string, object> PageState { get; private set; }10        public bool SuspensionManagerSessionState { get; private set; }11        public SaveStateEventArgs(Dictionary<string, object> pageState, bool suspending)12        {13            this.PageState = pageState;14            this.SuspensionManagerSessionState = suspending;15        }16    }17}18using System;19using System.Collections.Generic;20using System.Linq;21using System.Text;22using System.Threading.Tasks;23using Windows.UI.Xaml.Navigation;24{25    {26        public Dictionary<string, object> PageState { get; private set; }27        public LoadStateEventArgs(object parameter, Type sourcePageType, Dictionary<string, object> pageState)28            : base(parameter, sourcePageType)29        {30            this.PageState = pageState;31        }32    }33}34using System;35using System.Collections.Generic;36using System.Linq;37using System.Text;38using System.Threading.Tasks;39using Windows.UI.Xaml.Controls;40{41    {42        public Dictionary<string, object> PageState { get; private set; }43        public bool SuspensionManagerSessionState { get; private set; }44        public SaveStateEventArgs(Dictionary<string, object> pageState, bool suspending)45        {46            this.PageState = pageState;47            this.SuspensionManagerSessionState = suspending;48        }49    }50}51using System;52using System.Collections.Generic;53using System.Linq;54using System.Text;55using System.Threading.Tasks;56using Windows.UI.Xaml.Navigation;57{SaveStateEventArgs
Using AI Code Generation
1{2    public MainPage()3    {4        this.InitializeComponent();5    }6    protected override void OnNavigatedTo(NavigationEventArgs e)7    {8        var args = e.Parameter as SaveStateEventArgs;9        if (args != null)10        {11            var state = args.PageState;12            if (state != null)13            {14                if (state.ContainsKey("text"))15                {16                    textBlock.Text = state["text"] as string;17                }18            }19        }20    }21}22{23    public MainPage()24    {25        this.InitializeComponent();26    }27    protected override void OnNavigatedTo(NavigationEventArgs e)28    {29        var args = e.Parameter as SaveStateEventArgs;30        if (args != null)31        {32            var state = args.PageState;33            if (state != null)34            {35                if (state.ContainsKey("text"))36                {37                    textBlock.Text = state["text"] as string;38                }39            }40        }41    }42}43{44    public MainPage()45    {46        this.InitializeComponent();47    }48    protected override void OnNavigatedTo(NavigationEventArgs e)49    {50        var args = e.Parameter as SaveStateEventArgs;51        if (args != null)52        {53            var state = args.PageState;54            if (state != null)55            {56                if (state.ContainsKey("text"))57                {58                    textBlock.Text = state["text"] as string;59                }60            }61        }62    }63}64{65    public MainPage()66    {67        this.InitializeComponent();68    }69    protected override void OnNavigatedTo(NavigationEventArgs e)70    {71        var args = e.Parameter as SaveStateEventArgs;72        if (args != null)73        {74            var state = args.PageState;75            if (state != null)SaveStateEventArgs
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.IO;4using System.Linq;5using System.Runtime.InteropServices.WindowsRuntime;6using Windows.ApplicationModel;7using Windows.ApplicationModel.Activation;8using Windows.Foundation;9using Windows.Foundation.Collections;10using Windows.UI.Xaml;11using Windows.UI.Xaml.Controls;12using Windows.UI.Xaml.Controls.Primitive;13using Windows.UI.Xaml.Data;14using Windows.UI.Xaml.Input;15using Windows.UI.Xaml.Media;16using Windows.UI.Xaml.Navigation;17{18    {19        public App()20        {21            this.InitializeComponent();22            this.Suspending += OnSuspending;23        }24        protected override void OnLaunched(LaunchActivatedEventArgs args)25        {26            Frame rootFrame = Window.Current.Content as Frame;27            if (rootFrame == null)28            {29                rootFrame = new Frame();SaveStateEventArgs
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Windows.UI.Xaml;7{8    {9        public SaveStateEventArgs() { }10        public SaveStateEventArgs(Dictionary<string, object> pageState)11        {12            PageState = pageState;13        }14        public Dictionary<string, object> PageState { get; set; }15    }16}17        Public Sub New()18        Public Sub New(pageState As Dictionary(Of String, Object))19        Public Property PageState As Dictionary(Of String, Object)20using System;21using System.Collections.Generic;22using Windows.UI.Xaml.Navigation;23{24    {25        public LoadStateEventArgs(NavigationEventArgs e, Dictionary<string, object> pageState)26        {27            NavigationEventArgs = e;28            PageState = pageState;29        }30        public NavigationEventArgs NavigationEventArgs { get; private set; }31        public Dictionary<string, object> PageState { get; private set; }32    }33}Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
