using Prism.Regions; using Syncfusion.UI.Xaml.NavigationDrawer; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace FATrace.WPLApp.RegionAdapter { public class SfNavigationDrawerRegionAdapter : RegionAdapterBase { public SfNavigationDrawerRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory) { } protected override void Adapt(IRegion region, SfNavigationDrawer regionTarget) { { //////Old Version //if (regionTarget == null) // throw new ArgumentNullException(nameof(regionTarget)); //bool contentIsSet = regionTarget.ContentView != null; //contentIsSet = contentIsSet || null != BindingOperations.GetBinding(regionTarget, ContentControl.ContentProperty); //if (contentIsSet) // throw new InvalidOperationException("ContentControlHasContentException"); //region.ActiveViews.CollectionChanged += delegate //{ // regionTarget.ContentView = region.ActiveViews.FirstOrDefault(); //}; //region.Views.CollectionChanged += //(sender, e) => //{ // if (e.Action == NotifyCollectionChangedAction.Add && region.ActiveViews.Count() == 0) // { // region.Activate(e.NewItems[0]); // } //}; } //New Version if (region == null) { throw new ArgumentNullException(nameof(region)); } if (regionTarget == null) { throw new ArgumentNullException(nameof(regionTarget)); } // 处理活动视图变化 - 这是界面切换的关键 region.ActiveViews.CollectionChanged += (s, e) => { // 当有视图被激活时,将其设置为ContentView if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null && e.NewItems.Count > 0) { regionTarget.ContentView = e.NewItems[0] as FrameworkElement; } }; // 处理视图集合变化 region.Views.CollectionChanged += (s, e) => { // 当添加新视图且没有活动视图时,自动激活它 if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null && e.NewItems.Count > 0 && region.ActiveViews.Count() == 0) { region.Activate(e.NewItems[0]); } }; } protected override IRegion CreateRegion() { return new SingleActiveRegion(); //return new Region(); } } }