Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

z-xl-t/PrismRegionWithHcTabControl

Repository files navigation

I'm trying to use Prism and HandyControl to create a project, but now I have a very strange problem.

This is My Github repo: https://github.com/sanshiliuxiao/PrismRegionWithHcTabControl

(If you know Chinese, you can see the principle of my implementation) My Blog: http://sanshiliuxiao.top/notebook/CSharp/20210611-PrismWithTabControl/

I have three views (A, B, Cwithparameters) whose ViewModel inherits Inavigationaware.

If I click the button (ViewA or ViewB), It can execute three functions in turn (OnNavigatedFromIsNavigationTargetOnNavigatedTo).

But, if I click the button (ViewC), it just execute the OnNavigatedFrom.

Why? It's so strange!!!

3.gif 1.gif

Fix Inavigationaware problem

ISSUES: The Inavigationaware Interface Problem, Why Just exec OnNavigatedFrom

  1. Create the IViewName interface

  2. Modify function in Prism.Regions.RegionNavigationContentLoader.cs

       private IEnumerable<object> GetCandidatesFromRegionViews(IRegion region, string candidateNavigationContract)
        {
            return region.Views.Where(v => ViewIsMatch(v, candidateNavigationContract));
            // return region.Views.Where(v => ViewIsMatch(v.GetType(), candidateNavigationContract));
        }
    
        private bool ViewIsMatch(object v, string navigationSegment)
        {
    
            var names = new List<string>() { v.GetType().Name, v.GetType().FullName, (v as IViewName)?.ViewName ?? string.Empty, ((v as FrameworkElement)?.DataContext as IViewName)?.ViewName ?? string.Empty };
            return names.Any(x => x.Equals(navigationSegment, StringComparison.Ordinal));
        }
    
        //private static bool ViewIsMatch(Type viewType, string navigationSegment)
        //{
        //    var names = new[] { viewType.Name, viewType.FullName };
        //    return names.Any(x => x.Equals(navigationSegment, StringComparison.Ordinal));
        //}
  3. View or ViewModel to inherits the interface.

  4. When create a view, write the ViewName, like RegionWithHcTabControl.Helpers.TabSwitchOrAddHelper.cs:

        object content = System.Activator.CreateInstance(viewType);
    
        var v = ((content as FrameworkElement) as IViewName);
        if (v != null)
        {
            v.ViewName = viewName;
    
        }
        
        var vm = ((content as FrameworkElement)?.DataContext as IViewName);
        if (vm != null)
        {
            vm.ViewName = viewName;
        }

About

Prism 的多页面切换效果

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages