locked
How to remove view when error the region does not contain the specified view RRS feed

  • Question

  •  How to remove view

    the following code get error "the region does not contain the specified view"

    var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>()
    
    foreach(var view in regionManager.Regions["Window1"].Views)
    {
       if(view != null)
          regionManager.Regions["Window1"].Remove(view);
    }

     

     


    Hello
    Saturday, May 7, 2011 11:35 AM

Answers

  • I followed Sheldon _Xiao[MSFT]'s suggestion but instead of copy-pasting, wrote it from scratch coz he had some minor typos that'll make it error out, here's the solution that worked for me:

    List<object> views = new List<object>();
    List<object> removeViews = new List<object>();
    
    if (regionManager.Regions["NavigationButtonRegion"] != null)
    {
        foreach (object view in regionManager.Regions["NavigationButtonRegion"].ActiveViews)
        {
            views.Add(view);
        }
        for (int i = 0; i < views.Count; i++)
        {
            if (views[i].GetType().Name == "ResponseAnalysisNavButtonView" ||
                views[i].GetType().Name == "QueriesNavButtonView")
                removeViews.Add(views[i]);
        }
    
        if (removeViews.Count > 0)
            for (int i = 0; i < removeViews.Count; i++)
            {
                regionManager.Regions["NavigationButtonRegion"].Remove(removeViews[i]);
            }
    }

    • Marked as answer by 沈世鈞 Thursday, June 21, 2012 8:04 AM
    Wednesday, May 30, 2012 9:15 PM

All replies

  • Hi marskovski,

    You could not use the foreach, you could try to use for instead of foreach, and then write your logic.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 11, 2011 6:39 AM
  • I have tried for loop before, however,
    regionManager.Regions["Window1"].Views
    do not have regionManager.Regions["Window1"].Views[i] and
    regionManager.Regions["Window1"].Views.count

    how to use for loop in this case?

    Hello
    Wednesday, May 11, 2011 1:14 PM
  • you could convert the "regionManager.Regions["Window1"].Views" to a collection, like "List<>", and then you could get the count property,

    List<object> views = new List<object>(_regionManager.Regions["NavigationRegion"].Views);

    for (int i =0;i<views.count; i++){

     _regionManager.Regions["NavigationRegion"].Remove(views[0]);

    }

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 12, 2011 11:55 AM
  • success to use this writing syntax to compile however, get the same error


    Hello
    Thursday, May 12, 2011 2:57 PM
  • you could try to this code:

      List<object> views = new List<object>();
     foreach (object view in regionManager.Regions[Window1].Views)
     {
              views.Add(view);
     }
     foreach (object view in views)
    {
              regionManager.Regions[Window1].Remove(view);
     }


    or:
    List<object> views = new List<object>();
     foreach (object view in regionManager.Regions[Window1].Views)
     {
              views.Add(view);
     }

    for (int i =0;i<views.count; i++){

     _regionManager.Regions["Window1"].Remove(views[0]);

    }

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 12, 2011 3:10 PM
  • After tried both, get the same error
    Hello
    Sunday, May 15, 2011 10:14 AM
  • Hi marskovski,

    Based on my experience, that code could work as you want, if you still get the same error, you could add a breakpoint to debug your app, and check which part cause the error.


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 5:10 AM
  • Hi marskovski,

    Any updates?


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 23, 2011 11:32 AM
  • How can I disconnect propertychange events from the views that I remove?  For example, I'm removing only certain types of views like below because these views will be bound to a configuration object of a certain type.  If I open a job of a different type then I need to close these and load new views for the new type.  However, what is happening is that the propertychanged events for the views I thought were remove still fire when I load the new configuration except that now the type is wrong and they get errors.

     

    IRegion contentRegion = _regionManager.Regions[RegionNames.ContentRegion];
    foreach (var view in contentRegion.Views) {
        if (typeof(IConfigurationView).IsAssignableFrom(view.GetType())) {
            contentRegion.Remove(view);
        }
    }

    Thanks!!

     

    Thursday, May 26, 2011 12:55 PM
  • i will try it tonight, and reply you tonight
    Hello
    Friday, May 27, 2011 7:41 AM
  • Hi Sheldon _Xiao[MSFT]

    debug show view[0] has instance, not null

    my CoreModules Which is Exported and IModule, and mousewheel change event used your above

    still is region does not contain the specified view.

    strange!!!!! your method show that it can not remove itself

    public void initialize()
    
    {
    
      var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
    
      regionManager.Regions[RegionNames.Window1].Add(new Hello.Modules.Views.FirstView());
    
      regionManager.Regions[RegionNames.Window2].Add(new Hello.Modules.Views.SecondView());
    
    }
    
    
    


    Hello
    Friday, May 27, 2011 11:41 AM
  • even use registerviewwithregion, still meet the error
    Hello
    Monday, May 30, 2011 1:21 PM
  • I met the same problem
    Sunday, June 5, 2011 10:14 AM
  • Who have success to remove view from itemcontrol in Prism 4?
    Hello
    Saturday, June 11, 2011 4:22 AM
  • Not me, I have the same problem and I believe either there are too many bugs with prism4 or the document are not up to date.
    Friday, June 17, 2011 9:45 PM
  • I had got the same problem when I created the module's view implementing IRegionMemberLifetime with KeepAlive property setted to false.

    Now, I remove IRegionMemberLifetime from the view and into my subscriber module's Initialize method I create the subscription in this way

    ServiceLocator
                    .Current
                    .GetInstance<IEventAggregator>()
                    .GetEvent<RibbonCommunicationModuleEvent>()
                    .Subscribe(OnCommand, ThreadOption.PublisherThread, TRUE);

    and it's working!

    So your module'view code-behind haven't to implements IRegionMemberLifetime interface...

    VIEW

        public partial class RibbonDeviceView : IRibbonDeviceView
        {
            public RibbonDeviceView()
            {
                InitializeComponent();
            }
        }

    MODULE

            public void Initialize()
            {
                RegisterModuleTypes();
                _ribbonDeviceViewModel = ServiceLocator.Current.GetInstance<RibbonDeviceViewModel>();

                ServiceLocator
                    .Current
                    .GetInstance<IEventAggregator>()
                    .GetEvent<RibbonCommunicationModuleEvent>()
                    .Subscribe(OnCommand, ThreadOption.PublisherThread, TRUE);
            }


    • Proposed as answer by Dio C Wednesday, May 30, 2012 9:11 PM
    • Unproposed as answer by Dio C Wednesday, May 30, 2012 9:12 PM
    • Proposed as answer by JayChase Tuesday, June 12, 2012 5:15 AM
    • Unproposed as answer by JayChase Tuesday, June 12, 2012 5:15 AM
    • Proposed as answer by JayChase Tuesday, June 12, 2012 5:20 AM
    Tuesday, July 5, 2011 9:43 AM
  • I followed Sheldon _Xiao[MSFT]'s suggestion but instead of copy-pasting, wrote it from scratch coz he had some minor typos that'll make it error out, here's the solution that worked for me:

    List<object> views = new List<object>();
    List<object> removeViews = new List<object>();
    
    if (regionManager.Regions["NavigationButtonRegion"] != null)
    {
        foreach (object view in regionManager.Regions["NavigationButtonRegion"].ActiveViews)
        {
            views.Add(view);
        }
        for (int i = 0; i < views.Count; i++)
        {
            if (views[i].GetType().Name == "ResponseAnalysisNavButtonView" ||
                views[i].GetType().Name == "QueriesNavButtonView")
                removeViews.Add(views[i]);
        }
    
        if (removeViews.Count > 0)
            for (int i = 0; i < removeViews.Count; i++)
            {
                regionManager.Regions["NavigationButtonRegion"].Remove(removeViews[i]);
            }
    }

    • Marked as answer by 沈世鈞 Thursday, June 21, 2012 8:04 AM
    Wednesday, May 30, 2012 9:15 PM
  • Near 1 year, haven't thought that many people need this skill, one of heros succeed, it's time to try

    歸妹趨無妄,無妄趨同人,同人趨大有。甲轉丙,丙轉庚,庚轉癸。子丑之交,辰巳之交,午未之交。風雷是一變,山澤是一變,水火是一變。乾坤相激,震兌相激,離巽相激。三增而成五,五增而成九

    Thursday, May 31, 2012 3:10 AM
  • This worked for me. If I set KeepAlive = true and then remove the view I don't get the exception. I've not had a good look at it, but I guess the keep alive setting causes the RegionManager to try to remove the view twice when calling Remove() causing the exception. Hope this helps because this issue is a really painful way of wasting a few days work.
    Tuesday, June 12, 2012 5:20 AM
  • Thank you, I just run across the same problem in Prism 6, and this work around made it work :)
    • Edited by adminSoftDK Tuesday, November 22, 2016 10:29 AM
    Tuesday, November 22, 2016 10:29 AM