0
votes

I need a way to get the regions I defined inside one of my views. For example:

<UserControl ...
             mvvm:ViewModelLocator.AutoWireViewModel="True"
             d:DataContext="{d:DesignInstance errorBars:ErrorBarViewModel, IsDesignTimeCreatable=True}"/>
    <ContentControl mvvm:RegionManager.RegionName="{x:Static regions:SidebarRegions.ErrorSidebarContentRegion}" />

Inside my ViewModel, I dynamically set the view to the region.

_navigationService.Navigate<AnySubView>(SidebarRegions.ErrorSidebarContentRegion);

Now I need to remove the region from the RegionsManager. Thats quiet easy, but I need a way to remove it recursivly. Thats because the ErrorBarViewModel does not know if the View it navigated to also defines a Region. If it does, I will get an error when removing the View and adding a new instance if it again, because the regions are already defined.

My thought was iterating through the DependencyProperties or AttachedProperties and find out, which regions the child has defined, but it doesn't seem like this could work.

Does anyone have an idea how to get the regions a View has defined?

1
I'd expect that removing a view that contains a region should also remove that region for good. There once was a bug report for something like this, but it has been closed without action github.com/PrismLibrary/Prism/issues/1157 - Haukinger
I think it is removing that region, but If that regions contains a view which defined an other region, this subregion is not removed - DirtyNative
I think it's either a bug or a limitation, either way, you'll have to head over to github, I suppose. - Haukinger

1 Answers

0
votes

According to the explanation of your question, I guess your application scenario is as follows:

  • Reuse this value in multiple views
  • When navigation reuse ErrorSidebarContentRegion view, an exception occurs.Maybe because Region was unique in Prism, there couldn't be a duplicate RegionName

My advice for the above scenario is to adjust the logic like this:

  1. RegionName is dynamically generated
  2. Change your exception prompt(For your reference:HandyControl

dynamically regionName

public class AttactedErrorSidebarContentRegion
{
    private static readonly Type ownerType = typeof(AttactedErrorSidebarContentRegion);

    internal static readonly DependencyPropertyKey ValuePropertyKey =
        DependencyProperty.RegisterAttachedReadOnly("Value",
            typeof(string), ownerType,
            new PropertyMetadata(default(string)));

    public static readonly DependencyProperty ValueProperty = ValuePropertyKey.DependencyProperty;

    private static void SetValue(DependencyObject element, string value)
    {
        element.SetValue(ValuePropertyKey, value);
    }

    public static string GetValue(DependencyObject element)
    {
        var value = (string) element.GetValue(ValueProperty);
        if (string.IsNullOrEmpty(value))
        {
            value = GetErrorSidebarContentRegionForView(element.GetType());
            SetValue(element, value);
        }
        return value;
    }

    public static string GetErrorSidebarContentRegionForView(Type view)
    {
        var name = view.Name.TrimEnd("View".ToCharArray());
        var region = $"{name}.ErrorSidebarContentRegion";
        return region;
    }

    public static string GetErrorSidebarContentRegionForViewModel(Type viewModel)
    {   
        var name = viewModel.Name.TrimEnd("ViewModel".ToCharArray());
        var region = $"{name}.ErrorSidebarContentRegion";
        return region;
    }
}