PrismLibrary / Prism

Prism is a framework for building loosely coupled, maintainable, and testable XAML applications in WPF, Xamarin Forms, and Uno / Win UI Applications..
Other
6.27k stars 1.64k forks source link

RegionManager.RegisterViewWithRegion triggers IActiveAware twice. #1030

Closed simonndev closed 7 years ago

simonndev commented 7 years ago

Package info

Repro steps

Create a module implements IModule

public class GoComicsModule : IModule
{
        private readonly IUnityContainer _container;
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;

        public GoComicsModule(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this._container = container;
            this._regionManager = regionManager;
            this._eventAggregator = eventAggregator;
        }

        public void Initialize()
        {
            this._container.RegisterType(typeof(Services.IGoComicsService), typeof(Services.GoComicsService), new ContainerControlledLifetimeManager());

            this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion, typeof(Components.Main.MainView));
        }
}

Create a view

<UserControl x:Class="ADU.Metro.Modules.GoComics.Components.Main.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:prism="http://www.codeplex.com/prism"
             xmlns:metro="http://metro.mahapps.com/winfx/xaml/controls"
             xmlns:local="clr-namespace:ADU.Metro.Modules.GoComics.Components.Main"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
...
</UserControl>

Create a ViewModel that implements IActiveAware

public class MainViewModel : IActiveAware
    {
        private readonly IGoComicsService _service;
        private bool _isActive = false;

        public MainViewModel(IGoComicsService service)
        {
            this._service = service;
        }

        public bool IsActive
        {
            get
            {
                return this._isActive;
            }

            **set
            {
                this._isActive = value;
                if (value)
                {
                    GetFeatures();
                }
            }**
        }
...
}

With the current version of Prism 6.3.0 (Unity for WPF), My MainView was initialized twice, so my ViewModel will be triggered to call the GetFeatures() method which will send a request to a WebApi to get JSON result twice.

Is it normal behavior of the RegisterViewWithRegion to IActiveAware? If yes, please show me a way how can I call GetFeatures() in my view-model once. Thank you.

simonndev commented 7 years ago

I dont know how to set the label to #WPF, please help me update the post. Thanks in advance.

brianlagunas commented 7 years ago

IActiveAware does not fire twice. You have something else in your code causing this issue. As to your question, there are number of approaches you can take. Continure to use IActiveAware, use INavigationAware in combination with the navigation framework, or just load your data in the ctor of the class so when it is created, you are already loading the data. I'm sure there are more ways to do it.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.