解决Prism中Region的GetView不起作用问题

通常情况下在Region中添加View时我们需要先判断View是否在Region中已存在,但如果我们在Region.Add的方法调用不当时,我们在GetView中始终返回Null,原因自然是Add时出现了错误

错误回顾

view = ServiceLocator.Current.GetInstance(typeof(ApplicationView), "ApplicationView");

this.RegionManager.Regions[RegionNames.MainContentRegion].Add(view);

通过这种方式我们 在

var view=this.RegionManager.Regions[RegionNames.MainContentRegion].GetView("ApplicationView");

返回结果将始终为Null,下面演示正确的调用方法

var view=this.RegionManager.Regions[RegionNames.MainContentRegion].GetView("ApplicationView");
            if (view == null)
            {
                view = ServiceLocator.Current.GetInstance(typeof(ApplicationView), "ApplicationView");
                this.RegionManager.Regions[RegionNames.MainContentRegion].Add(view, "ApplicationView");
            }
            this.RegionManager.Regions[RegionNames.MainContentRegion].Activate(view);
原文地址:https://www.cnblogs.com/oldkingsir/p/2369188.html