Starting with Prism learn WPF (eight) navigation navigation?

Source: Internet
Author: User

The original: from Prism to learn WPF (eight) navigation navigation?

0x6navigationbasic Navigation

The navigation in Prism provides a navigation-like feature that refreshes the UI based on the user's input.

Let's look at one of the simplest examples, navigate to a view via a button, where the view is registered as navication.

        public void Initialize()        {            _container.RegisterTypeForNavigation<ViewA>();            _container.RegisterTypeForNavigation<ViewB>();        }

In the Shell view, set two buttons and bind the following command with parameters:

  public delegatecommand<string> Navigatecommand {get; private set;}            Public Mainwindowviewmodel (Iregionmanager regionmanager) {_regionmanager = Regionmanager;        Navigatecommand = new delegatecommand<string> (Navigate); private void Navigate (string navigatepath) {if (Navigatepath! = null) _regionma Nager.        Requestnavigate ("Contentregion", Navigatepath); }
    <DockPanel LastChildFill="True">        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="5" >            <Button Command="{Binding NavigateCommand}" CommandParameter="ViewA" Margin="5">Navigate to View A</Button>            <Button Command="{Binding NavigateCommand}" CommandParameter="ViewB" Margin="5">Navigate to View B</Button>        </StackPanel>        <ContentControl prism:RegionManager.RegionName="ContentRegion" Margin="5"  />    </DockPanel>

Regionmanager uses the Requestnavigate method to obtain the registered navigation and binds to the region.

When you need to handle something based on the result of the call, you can use the following method:

void RequestNavigate(string regionName, string source, Action<NavigationResult> navigationCallback);

Of course, this method is called in the shell, but sometimes we need the view or ViewModel to participate in the navigation, such as when you request a navigation, Hopefully the navigation itself displays some information, and for this reason, Prism provides us with a inavigationaware interface.

    Summary://Provides a-on-objects involved in navigation-be notified of navigation//A    Ctivities.  Public interface Inavigationaware {////Summary://Called to determine if this instance can        Handle the navigation request.        Parameters://Navigationcontext://The navigation context. Returns://True if this instance accepts the navigation request;        otherwise, false.        BOOL Isnavigationtarget (Navigationcontext navigationcontext);        Summary://Called when the implementer are being navigated away from.        Parameters://Navigationcontext://The navigation context.        void Onnavigatedfrom (Navigationcontext navigationcontext);        Summary://Called when the implementer have been navigated to. Parameters://Navigationcontext:       The navigation context.    void Onnavigatedto (Navigationcontext navigationcontext); }

If you want to navigation the goal is also involved in the process of navigation, just let your viewmodel implement this interface, and then write your code in these methods can be.
The Isnavigationtarget method sets whether it is allowed to be set as the target of the navigation, and when his return value is Fasle, it will not be "navigated" to it.

In 19-NavigationParticipation The example, the target of region is:

        <TabControl prism:RegionManager.RegionName="ContentRegion" Margin="5"  />

TabControl when set to region, loading view automatically creates a page to hold the view, and if "navigate" to the same view he will find him in the page and display it. But if Isnavigationtarget returns false, the previous page is not displayed but a new page is created to load the view.

Passingparameters Navigation with parameters

When using navigation, bring the data source to the new Navigationtarget, then target applies the data. This will use the Navigationcontext parameter to navigation:

        private void PersonSelected(Person person)        {            var parameters = new NavigationParameters();            parameters.Add("person", person);            if (person != null)                _regionManager.RequestNavigate("PersonDetailsRegion", "PersonDetail", parameters);        }

Used in the Onnavigatedto method of target:

        public void OnNavigatedTo(NavigationContext navigationContext)        {            var person = navigationContext.Parameters["person"] as Person;            if (person != null)                SelectedPerson = person;        }

When navigating changes, you need some cue boxes to implement Iconfirmnavigationrequest
He has a confirmnavigationrequest method to make some judgments.

In the above example, when we jump between the view, Viewa and VIEWB are cached, but sometimes we want to destroy a when we jump to B, how do we do it?
Implement the Iregionmemberlifetime interface on the view or ViewModel, and set the value of the KeepAlive property to False.

Journal

Journal implements a browser-like push-back button that, when a region has multiple view, automatically records the view's load order and then switches back and forth between the view.
Prism is achieved through iregionnavigationjournal, when the view is loaded, reasoning, can be infinitely progressive and backward, I myself in the official example added a view also perfectly run.

        public void OnNavigatedTo(NavigationContext navigationContext)        {            _journal = navigationContext.NavigationService.Journal;        }

Then use:

_journal.GoBack();

Or

_journal.GoForward();

Starting with Prism learn WPF (eight) navigation navigation?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.