C# 从RegionManager注销棱柱区域

C# 从RegionManager注销棱柱区域,c#,prism,region,C#,Prism,Region,有没有办法从RegionManager取消注册区域 与 我确实在RegionManager注册了一个区域 但是,如果我需要删除该注册(以便再次注册完全相同的区域),该怎么办 =================== 这就是我的情况: 我有一个实例myView,它使用以下命令将UI控件(DependencyObject)myRegion注册为一个区域: RegionManager.SetRegionName(myRegion, regionName); RegionManager.SetRegion

有没有办法从RegionManager取消注册区域

我确实在RegionManager注册了一个区域

但是,如果我需要删除该注册(以便再次注册完全相同的区域),该怎么办

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

这就是我的情况:

我有一个实例
myView
,它使用以下命令将UI控件(DependencyObject)
myRegion
注册为一个区域:

RegionManager.SetRegionName(myRegion, regionName);
RegionManager.SetRegionManager(myRegion, myRegionManager);
当实例化
myView
时,也会实例化本地区域管理器对象
myRegionManager

然后我需要再次创建实例
myView
,它再次进行注册

因此,新实例
myView
还实例化了一个新的本地区域管理器
myRegionManager
,它当然没有与之关联的区域

然后,调用
RegionManager.SetRegionName(…)
RegionManager.SetRegionManager(…)
成功,但如果我随后尝试使用

IRegion region = myRegionManager.Regions[regionName];
我收到一个
区域更新异常

如果我

string name = RegionManager.GetRegionName(myRegion);

IRegionManager regionManager = RegionManager.GetRegionManager(myRegion);
在注册之前,第一次实例化
myView
时,这两个字段都是空的,但在第二次实例化
myView
时,它们确实有值


但是由于
myRegion
myRegionManager
也与
myView
一起被新实例化,我希望在调用第二次注册之前,名称和区域管理器也为空。

您可以使用
RegionManager
区域
-属性按名称删除区域:

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; }

    [...]
}
公共接口IRegionManager { /// ///获取按名称标识每个区域的区域集合。您可以使用此集合向当前区域管理器添加或删除区域。 /// 区域收集区域{get;} [...] } 及

公共接口IRegionCollection:IEnumerable,INotifyCollectionChanged
{
[...]
/// 
///从集合中删除。
/// 
///要删除的区域的名称。
///如果该区域已从集合中删除,则为。
bool-Remove(字符串regionName);
[...]
}

根据您的描述,听起来您需要同一视图的多个实例,其中定义了一个区域,并且能够在这些相应的区域内插入其他视图。这是正确的吗

查看有关ScopedRegions的文档:


您还可以观看本Pluralsight课程,该课程向您展示了如何使用ScopedRegions:

我扩展了我的帖子,以提供有关我的问题的更多信息。非常感谢。所以你有一个
myView
myRegion
上设置了一个区域名,然后你创建了一个新的
myView
和一个新的
myRegion
,新的
myRegion
仍然有旧的区域名,对吗?你确定你真的在看新的吗?如果首先重新创建
myView
,它可能会使用旧的
myRegion
或旧的
myRegionManager
或两者都使用。。。
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; }

    [...]
}
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);

    [...]
}