* UINavMenuHelper: 重构RemovePage方法

This commit is contained in:
Sunny 2022-11-29 18:06:11 +08:00
parent 95260be5b3
commit 989e074cdc

View File

@ -18,6 +18,7 @@
* *
* 2020-01-01: V2.2.0 * 2020-01-01: V2.2.0
* 2022-04-14: V3.1.3 * 2022-04-14: V3.1.3
* 2022-11-29: V3.3.0 RemovePage方法
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -396,20 +397,12 @@ namespace Sunny.UI
public UIPage GetPage(int pageIndex) public UIPage GetPage(int pageIndex)
{ {
if (pageIndex < 0) return null; var pages = GetPages<UIPage>();
foreach (var item in PageItems)
{
if (item.Value.PageIndex == pageIndex && item.Key != null)
{
var tabPage = item.Key;
var pages = tabPage.GetControls<UIPage>();
for (int i = 0; i < pages.Count; i++) for (int i = 0; i < pages.Count; i++)
{ {
if (pages[i].PageIndex == pageIndex) if (pages[i].PageIndex == pageIndex)
return pages[i]; return pages[i];
} }
}
}
return null; return null;
} }
@ -428,7 +421,7 @@ namespace Sunny.UI
if (item.Key != null) if (item.Key != null)
{ {
var tabPage = item.Key; var tabPage = item.Key;
var pages = tabPage.GetControls<UIPage>(); var pages = tabPage.GetControls<T>();
for (int i = 0; i < pages.Count; i++) for (int i = 0; i < pages.Count; i++)
{ {
if (pages[i] is T pg) if (pages[i] is T pg)
@ -443,32 +436,37 @@ namespace Sunny.UI
public UIPage GetPage(Guid guid) public UIPage GetPage(Guid guid)
{ {
if (guid == Guid.Empty) return null; if (guid == Guid.Empty) return null;
foreach (var item in PageItems) var pages = GetPages<UIPage>();
{
if (item.Value.PageGuid == guid && item.Key != null)
{
var tabPage = item.Key;
var pages = tabPage.GetControls<UIPage>();
for (int i = 0; i < pages.Count; i++) for (int i = 0; i < pages.Count; i++)
{ {
if (pages[i].PageGuid == guid) if (pages[i].PageGuid == guid)
return pages[i]; return pages[i];
} }
}
}
return null; return null;
} }
public bool RemovePage(int pageIndex) public bool RemovePage(int pageIndex)
{ {
if (pageIndex < 0) return false;
foreach (var item in PageItems) foreach (var item in PageItems)
{ {
if (item.Value.PageIndex == pageIndex && item.Key != null) if (item.Value.PageIndex == pageIndex)
{ {
TabPage tabPage = item.Key; UIPage page = GetPage(pageIndex);
tabControl.RemoveTabPage(tabPage.TabIndex); if (page != null)
{
TabPage tabpage = page.TabPage;
page.Dispose();
page = null;
if (tabpage != null)
{
tabpage.Parent = null;
tabpage.Dispose();
tabpage = null;
}
}
PageItems.TryRemove(item.Key, out _); PageItems.TryRemove(item.Key, out _);
return true; return true;
} }
@ -482,10 +480,23 @@ namespace Sunny.UI
if (guid == Guid.Empty) return false; if (guid == Guid.Empty) return false;
foreach (var item in PageItems) foreach (var item in PageItems)
{ {
if (item.Value.PageGuid == guid && item.Key != null) if (item.Value.PageGuid == guid)
{ {
TabPage tabPage = item.Key; UIPage page = GetPage(guid);
tabControl.RemoveTabPage(tabPage.TabIndex); if (page != null)
{
TabPage tabpage = page.TabPage;
page.Dispose();
page = null;
if (tabpage != null)
{
tabpage.Parent = null;
tabpage.Dispose();
tabpage = null;
}
}
PageItems.TryRemove(item.Key, out _); PageItems.TryRemove(item.Key, out _);
return true; return true;
} }