簡體   English   中英

從RegionManager取消注冊PRISM區域

[英]Unregister a PRISM Region from the RegionManager

有沒有一種方法可以從RegionManager取消注冊區域?

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

我確實在RegionManager上注冊了一個區域。

但是,如果我需要刪除該注冊(以便再次注冊完全相同的區域),該怎么辦?

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

這是我的情況:

我有一個實例myView ,它使用以下方法將UI控件(DependencyObject) myRegion為區域:

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

實例化myView ,本地實例管理器對象myRegionManager也被實例化。

然后,我需要再次創建實例myView ,該實例再次進行此注冊。

因此,新實例myView也實例化了新的本地區域管理器myRegionManager ,該區域管理器當然沒有與之關聯的區域。

然后,對RegionManager.SetRegionName(...)RegionManager.SetRegionManager(...)的調用成功,但是如果我嘗試使用以下方法訪問本地區域管理器myRegionManager

IRegion region = myRegionManager.Regions[regionName];

我正在收到RegionUpdateException

如果我放

string name = RegionManager.GetRegionName(myRegion);

IRegionManager regionManager = RegionManager.GetRegionManager(myRegion);

在注冊之前,兩個字段在myView的第一個實例化之前都是空的,但是在myView的第二個實例化期間它們確實具有值。

但是由於myRegionmyRegionManager也會與myView一起被新實例化, myView我希望名稱和區域管理器在調用第二個注冊之前也為空。

您可以使用RegionManagerRegions -Property按名稱刪除區域:

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

    [...]
}

根據您的描述,聽起來您需要具有一個已定義區域的同一個View的多個實例,並能夠將其他視圖插入這些相應的區域內。 這個對嗎?

請查看ScopedRegions上的文檔: https : //github.com/PrismLibrary/Prism/blob/master/Documentation/WPF/50-ComposedtheUserInterface.md#creating-multiple-instances-of-a-region

您還可以觀看此Pluralsight課程,該課程向您展示如何使用ScopedRegions: https ://app.pluralsight.com/library/courses/prism-showing-multiple-shells/table-of-contents

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM