Prism 5到6.2 - InteractionRequest - 当导航到新对象

时间:2017-02-17 14:10:25

标签: prism interaction prism-6

PRISM 6.2 / EntityFramework 6.3.1 / StockTrader UI / UnityContainer

我实际上有一个使用PRISM 5.0.0的项目,并希望更新到PRISM 6.2。该项目在5.0.0上运行正常,但是当我更新到6.2时,我遇到了InteractionRequest的以下问题。

当我第一次使用Notifications导航到view / viewmodel时,一切正常,我可以像往常一样处理InteractionRequests。如果我向后导航并使用新对象再次导航到视图,则InteractionsRequest会将通知提升两次。 (...导航回去再次 - >>提升三次等等。)

由于某些原因,将出现“此视图未连接到PresentationSource”消息。

我发现,使用PRISM 6.2,InteractionRequest上的_invocationCount和_invocationList不会设置为“0”/“null”。所以,我认为InteractionRequest将多次调用通知。附上PRISM 5和PRISM 6.2的截图。

我该如何处理并解决问题。在我看来,这不是一件大事,但我实际上花了很多时间来寻找解决方案。感谢...

PRISM 5.0.0 - working fine

PRISM 6.2 - issue

2017.02.22添加了Sourccode。软件用于处理数据中心中的设备。我删除了所有不必要的源代码,但是使用这些文件仍然会出现问题。也许这是试图找到我的问题......

Rackmodule.cs - >初始化模块机架

public class RackModule : IModule
{
    private readonly IRegionManager _regionManager;
    private readonly IUnityContainer _container;

    public RackModule(IRegionManager regionManager, IUnityContainer container)
    {
        _regionManager = regionManager;
        _container = container;
    }

    public void Initialize() 
    {
        _container.RegisterType<IRackViewModel, RackViewModel>(new ContainerControlledLifetimeManager());
        _container.RegisterType<IRackToolbarViewModel, RackToolbarViewModel>(new ContainerControlledLifetimeManager());
        _container.RegisterType<IRackStatusbarViewModel, RackStatusbarViewModel>(new ContainerControlledLifetimeManager());
        _container.RegisterType<IRackSummaryViewModel, RackSummaryViewModel>(new ContainerControlledLifetimeManager());

        _container.RegisterType<IGeneralDataViewModel, GeneralDataViewModel>(new ContainerControlledLifetimeManager());
        _container.RegisterType<IPlanDataViewModel, PlanDataViewModel>(new ContainerControlledLifetimeManager());
        _container.RegisterType<IRackDataViewModel, RackDataViewModel>(new ContainerControlledLifetimeManager());

        _container.RegisterType<Object, GeneralDataView>(typeof(GeneralDataView).FullName);

        IRegion region = this._regionManager.Regions["MainRegion"];
        var rackView = _container.Resolve<RackView>();
        region.Add(rackView, "RackView");
        region.Activate(rackView);

        IRegion toolbarregion = this._regionManager.Regions["RackToolbarRegion"];
        var toolbarView = _container.Resolve<RackToolbarView>();
        toolbarregion.Add(toolbarView, "RackToolbarView");
        toolbarregion.Activate(toolbarView);

        IRegion statusbarregion = this._regionManager.Regions["RackStatusbarRegion"];
        var statusbarView = _container.Resolve<RackStatusbarView>();
        statusbarregion.Add(statusbarView, "RackStatusbarView");
        statusbarregion.Activate(statusbarView);

        _container.RegisterType<Object, RackSummaryView>(typeof(RackSummaryView).FullName);
        _regionManager.RequestNavigate(RegionNames.RackContentRegion, typeof(RackSummaryView).FullName); 
    }
}

RackSummaryViewModel.cs - &GT;机架概述。点击对象

,转到RackDataView
public class RackSummaryViewModel : BindableBase, IRackSummaryViewModel
{
    private readonly IRegionManager _regionManager;
    private readonly IEventAggregator _eventAggregator;
    private readonly IUnityContainer _container;

    public DelegateCommand<SearchEventArgs> OnSearch { get; private set; }
    public DelegateCommand AdvancedRackSearchCommand { get; private set; }

    public InteractionRequest<AdvancedRackSearchNotification> AdvancedSearchRequest { get; private set; }

    private ObservableCollection<RackSummaryEntry> _racks;
    public ObservableCollection<RackSummaryEntry> Racks
    {
        get { return _racks; }
        private set {SetProperty(ref _racks, value);}
    }

    private RackSummaryEntry _currentRack;
    public RackSummaryEntry CurrentRack
    {
        get { return _currentRack; }
        set
        {
            if (SetProperty(ref _currentRack, value))
            {
                if (_currentRack != null)
                {
                    var parameters = new NavigationParameters();
                    parameters.Add("RackID", _currentRack.PrimaryKey.ToString(GuidNumericFormatSpecifier));
                    _container.RegisterType<Object, RackDataView>(typeof(RackDataView).FullName);
                    _regionManager.RequestNavigate(RegionNames.RackContentRegion, new Uri(typeof(RackDataView).FullName + parameters, UriKind.Relative));
                }
            }
        }
    }

    private const string GuidNumericFormatSpecifier = "N";

    public RackSummaryViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
    {
        _regionManager = regionManager;
        _eventAggregator = eventAggregator;
        _container = container;

        ISessionFactory factory = new SessionFactory();
        container.RegisterType<IRepository, Repository>(new InjectionConstructor(factory.CurrentUoW));

        IUnitOfWork unitOfWork = factory.CurrentUoW;
        IRepository localrepository = new Repository(unitOfWork);

        var query = localrepository.GetList<DMS.Domain.Domain.Rack>();

        Racks = new ObservableCollection<RackSummaryEntry>(query
            .Select(x => new RackSummaryEntry
            {
                PrimaryKey = x.PrimaryKey,
                Country = x.Location.Address.Country,
                City = x.Location.Address.City,
                Street = x.Location.Address.Street,
                Building = x.Location.BuildingName,
                RoomName = x.Location.RoomName,
                RackName = x.RackName,
                RackHeight = x.RackHeight
            }).ToList());
    }
}

RackDataViewModel.cs - &GT;只有按钮“保存”和“返回”

public class RackDataViewModel : BindableBase, IRackDataViewModel, INavigationAware, IRegionMemberLifetime
    {
        private IRegionNavigationJournal _navigationJournal;
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;
        private readonly IEventAggregator _eventAggregator;

        public DelegateCommand GoBackCommand { get; private set; }
        public DelegateCommand SaveCommand { get; private set; }

        public InteractionRequest<INotification> SaveNotificationRequest { get; private set; }

        private const string RackIdKey = "RackID";
        private const string EType = "EditType";
        private const string GuidNumericFormatSpecifier = "N";
        public DMS.Domain.Domain.Rack rack;

        // [InjectionConstructor] check if necessary
        public RackDataViewModel(IRegionManager regionManager, IRegionNavigationJournal navigationJournal, IUnityContainer container, IEventAggregator eventAggregator, ILoggerFactory logFactory)
        {
            _regionManager = regionManager;
            _navigationJournal = navigationJournal;
            _container = container;
            _eventAggregator = eventAggregator;

            GoBackCommand = new DelegateCommand(OnGoBackExecute);
            SaveCommand = new DelegateCommand(OnSaveExecute);

            SaveNotificationRequest = new InteractionRequest<INotification>();
        }

        private void OnGoBackExecute()
        {
            if (_navigationJournal != null)
            {
                while (_navigationJournal.CanGoBack)
                    _navigationJournal.GoBack();

                _regionManager.Regions.Remove(RegionNames.RackGeneralDataRegion);
            }
        }

        private void OnSaveExecute() 
        {
            SaveNotificationRequest.Raise(new Notification { Content = "Save changes submitted", Title = "Save changes" });
        }

        public bool KeepAlive
        {
            get { return false; }
        }

        private Guid? GetRequestedRackId(NavigationContext navigationContext)
        {
            var rack = navigationContext.Parameters[RackIdKey];
            Guid rackId;
            if (rack != null)
            {
                if (rack is Guid)
                    rackId = (Guid)rack;
                else
                    rackId = Guid.Parse(rack.ToString());

                return rackId;
            }

            return null;
        }

        bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
        {
            var type = navigationContext.Parameters[EType];
            if (rack == null || ((string)type) == "New")
                return true;

            var requestedRackId = GetRequestedRackId(navigationContext);
            return requestedRackId.HasValue && requestedRackId.Value == rack.PrimaryKey;
        } 

        void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
        {
            Guid? rackId;
            NavigationParameters parameters = new NavigationParameters();

            string key = navigationContext.Parameters[RackIdKey].ToString();
            rackId = GetRequestedRackId(navigationContext);
            parameters = navigationContext.Parameters;

            _regionManager.RequestNavigate(RegionNames.RackGeneralDataRegion, new Uri(typeof(GeneralDataView).FullName + parameters, UriKind.Relative));
            _navigationJournal = navigationContext.NavigationService.Journal;
        }
    }

GeneralDataViewModel.cs - &GT;在RackDataView的区域中是否有机架数据

public class GeneralDataViewModel : BindableBase, IGeneralDataViewModel, INavigationAware
    {
        private IRegionNavigationJournal _navigationJournal;
        private readonly IRegionManager _regionManager;
        private readonly IRepository _repository;

        private const string RackIdKey = "RackID";

        public DMS.Domain.Domain.Rack Rack { get; set; }
        public List<Location> Locations { get; set; }

        public GeneralDataViewModel(IRegionManager regionManager, IRepository repository)
        {
            _regionManager = regionManager;
            _repository = repository;
        }

        private Guid? GetRequestedRackId(NavigationContext navigationContext)
        {
            var rack = navigationContext.Parameters[RackIdKey];
            Guid rackId;
            if (rack != null)
            {
                if (rack is Guid)
                    rackId = (Guid)rack;
                else
                    rackId = Guid.Parse(rack.ToString());

                return rackId;
            }
            return null;
        }


        bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
        {
            if (Rack == null)
                return true;

            var requestedRackId = GetRequestedRackId(navigationContext);
            return requestedRackId.HasValue && requestedRackId.Value == Rack.PrimaryKey;
        }

        void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
        {
            // Intentionally not implemented.
        }

        void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
        {
            var rackId = GetRequestedRackId(navigationContext);
            Rack = _repository.GetEntity<DMS.Domain.Domain.Rack>(rackId);

            _navigationJournal = navigationContext.NavigationService.Journal;
        }
    }

0 个答案:

没有答案