0
votes

Is there a way to un-register a region from the RegionManager?

With

RegionManager.SetRegionName(myRegion, regionName);
RegionManager.SetRegionManager(myRegion, myRegionManager);

I do register a region at the RegionManager.

But what, if I need to remove that registration (in order to register the exact same region again)?

===================

This is my situation:

I have an instance myView that registers a UI control (a DependencyObject) myRegion as a region by using:

RegionManager.SetRegionName(myRegion, regionName);
RegionManager.SetRegionManager(myRegion, myRegionManager);

When myView is instantiated, also the local region manager object myRegionManager is instantiated.

Then I need to again create the instance myView, which again makes this registration.

Hence, the new instance myView also instantiates a new local region manager myRegionManager, which of course has no regions associated with.

Then, the call to RegionManager.SetRegionName(...) and RegionManager.SetRegionManager(...) succeedes, but if I then try to access the local region manager myRegionManager by using

IRegion region = myRegionManager.Regions[regionName];

I am getting an RegionUpdateException!

If I place

string name = RegionManager.GetRegionName(myRegion);

IRegionManager regionManager = RegionManager.GetRegionManager(myRegion);

in front of the registration, both fields are empty by the first instantiation of myView, but they do have values during the second instantiation of myView.

But since both the myRegion and the myRegionManager also get newly instantiated together with myView I expect the name and region manager also to be empty before calling the second registration.

2
So you have a myView that sets a region name on a myRegion and then you create a new myView and a new myRegion and the new myRegion still has the region name of the old one, correct? Are you sure you're really looking at the new one? if you recreate the myView first, it might use the old myRegionor the old myRegionManageror both... - Haukinger

2 Answers

0
votes

You can use RegionManager's Regions-Property to remove the region by name:

public interface IRegionManager
{
    /// <summary>
    /// Gets a collection of <see cref="IRegion"/> that identify each region by name. You can use this collection to add or remove regions to the current region manager.
    /// </summary>
    IRegionCollection Regions { get; }

    [...]
}

and

public interface IRegionCollection : IEnumerable<IRegion>, INotifyCollectionChanged
{
    [...]

    /// <summary>
    /// Removes a <see cref="IRegion"/> from the collection.
    /// </summary>
    /// <param name="regionName">Name of the region to be removed.</param>
    /// <returns><see langword="true"/> if the region was removed from the collection, otherwise <see langword="false"/>.</returns>
    bool Remove(string regionName);

    [...]
}
0
votes

Based on your description, it sounds like you need to have multiple instances of the same View which has a region defined in it, and be able to inject other views inside those respective regions. Is this correct?

Check out the documentation on ScopedRegions: https://github.com/PrismLibrary/Prism/blob/master/Documentation/WPF/50-ComposingtheUserInterface.md#creating-multiple-instances-of-a-region

You can also watch this Pluralsight course that shows you how to use ScopedRegions: https://app.pluralsight.com/library/courses/prism-showing-multiple-shells/table-of-contents