Commit d313a049 by liulongfei

导航控件更新

parent bd1a1995
......@@ -322,6 +322,7 @@
<Compile Include="Widgets\LabelValue\LabelValue.cs" />
<Compile Include="Widgets\NavigationControl\NavigationConfig.cs" />
<Compile Include="Widgets\NavigationControl\NavigationControl.cs" />
<Compile Include="Widgets\NavigationControl\NavigationControlCreateMode.cs" />
<Compile Include="Widgets\NavigationControl\NavigationItemControl.cs" />
<Compile Include="Widgets\NoneWindow\NoneWindow.cs" />
<Compile Include="Widgets\ResizeImageControl\ResizeImageControl.cs" />
......
......@@ -47,6 +47,11 @@ namespace VIZ.Framework.Common
public WeakReference<object> View { get; set; }
/// <summary>
/// 容器
/// </summary>
public WeakReference<NavigationItemControl> Container { get; set; }
/// <summary>
/// 视图创建后触发 参数: 导航配置, 视图
/// </summary>
public Action<NavigationConfig, object> ViewCreated { get; set; }
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
......@@ -26,9 +29,29 @@ namespace VIZ.Framework.Common
DefaultStyleKeyProperty.OverrideMetadata(typeof(NavigationControl), new FrameworkPropertyMetadata(typeof(NavigationControl)));
}
#region CreateMode -- 创建模式
/// <summary>
/// 创建模式
/// </summary>
public NavigationControlCreateMode CreateMode
{
get { return (NavigationControlCreateMode)GetValue(CreateModeProperty); }
set { SetValue(CreateModeProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for CreateMode. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty CreateModeProperty =
DependencyProperty.Register("CreateMode", typeof(NavigationControlCreateMode), typeof(NavigationControl), new PropertyMetadata(NavigationControlCreateMode.Delay));
#endregion
protected override DependencyObject GetContainerForItemOverride()
{
NavigationItemControl itemControl = new NavigationItemControl();
itemControl.OwnerNavigaiton = new WeakReference<NavigationControl>(this);
return itemControl;
}
......@@ -37,5 +60,47 @@ namespace VIZ.Framework.Common
{
return item is NavigationItemControl;
}
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
base.OnItemsSourceChanged(oldValue, newValue);
INotifyCollectionChanged newCollection = newValue as INotifyCollectionChanged;
if (newCollection != null)
{
newCollection.CollectionChanged -= NewCollection_CollectionChanged;
newCollection.CollectionChanged += NewCollection_CollectionChanged;
}
INotifyCollectionChanged oldCollection = oldValue as INotifyCollectionChanged;
if (oldCollection != null)
{
oldCollection.CollectionChanged -= NewCollection_CollectionChanged;
}
}
private void NewCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// 导航控件目前仅处理移除的情况
if (e.Action != NotifyCollectionChangedAction.Remove)
return;
if (e.OldItems == null)
return;
foreach (object item in e.OldItems)
{
if (!(item is NavigationConfig config))
continue;
if (config.Container == null)
continue;
if (!config.Container.TryGetTarget(out NavigationItemControl container))
continue;
container.DisposeContent();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Common
{
/// <summary>
/// 导航控件创建模式
/// </summary>
public enum NavigationControlCreateMode
{
/// <summary>
/// 延时
/// </summary>
Delay,
/// <summary>
/// 实时
/// </summary>
RealTime
}
}
using System;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
......@@ -21,6 +22,11 @@ namespace VIZ.Framework.Common
/// </summary>
public class NavigationItemControl : ListBoxItem
{
/// <summary>
/// 日志
/// </summary>
private readonly static ILog log = LogManager.GetLogger(typeof(NavigationItemControl));
static NavigationItemControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NavigationItemControl), new FrameworkPropertyMetadata(typeof(NavigationItemControl)));
......@@ -29,6 +35,7 @@ namespace VIZ.Framework.Common
public NavigationItemControl()
{
this.IsVisibleChanged += NavigationItemControl_IsVisibleChanged;
this.DataContextChanged += NavigationItemControl_DataContextChanged;
}
#region Key --
......@@ -70,6 +77,36 @@ namespace VIZ.Framework.Common
#endregion
/// <summary>
/// 所屬导航容器
/// </summary>
internal WeakReference<NavigationControl> OwnerNavigaiton;
/// <summary>
/// 销毁内容
/// </summary>
public void DisposeContent()
{
this.OwnerNavigaiton = null;
if (this.Content == null)
return;
IDisposable disposable = this.Content as IDisposable;
this.Content = null;
if (disposable == null)
return;
try
{
disposable.Dispose();
}
catch (Exception ex)
{
log.Error(ex);
}
}
/// <summary>
/// 可见性改变时触发
/// </summary>
private void NavigationItemControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
......@@ -77,21 +114,67 @@ namespace VIZ.Framework.Common
if (!this.IsVisible)
return;
if (this.Content != null && this.Content.GetType() == this.ViewType)
this.TryCreateContent();
}
/// <summary>
/// 上下文改变时触发
/// </summary>
private void NavigationItemControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (this.OwnerNavigaiton == null)
return;
if (!this.OwnerNavigaiton.TryGetTarget(out NavigationControl navigation))
return;
if (navigation != null && navigation.CreateMode != NavigationControlCreateMode.RealTime)
return;
this.TryCreateContent();
}
/// <summary>
/// 尝试创建内容
/// </summary>
private void TryCreateContent()
{
// 已经包含内容,则不创建
if (this.Content != null)
return;
NavigationConfig config = this.DataContext as NavigationConfig;
if (config != null)
{
this.TryCreateContentFromConfig(config);
}
else
{
this.TryCreateContentFromViewType();
}
}
if (this.ViewType != null)
/// <summary>
/// 尝试从ViewType属性创建内容
/// </summary>
private void TryCreateContentFromViewType()
{
if (this.ViewType == null)
return;
this.Content = this.ViewType.Assembly.CreateInstance(this.ViewType.FullName);
}
if (config != null)
/// <summary>
/// 尝试从导航配置创建视图
/// </summary>
/// <param name="config">导航配置</param>
private void TryCreateContentFromConfig(NavigationConfig config)
{
this.Content = config.ViewType.Assembly.CreateInstance(config.ViewType.FullName);
config.View = new WeakReference<object>(this.Content);
config.Container = new WeakReference<NavigationItemControl>(this);
config.ViewCreated?.Invoke(config, this.Content);
}
}
}
}
......@@ -126,11 +126,11 @@ namespace VIZ.Framework.Core
{
while (!this.IsDisposabled)
{
lock (this.Pool)
{
List<DelayInfo> list = this.Pool.Values.ToList();
DateTime now = DateTime.Now;
List<DelayInfo> removeList = new List<DelayInfo>();
foreach (DelayInfo info in this.Pool.Values)
foreach (DelayInfo info in list)
{
if (this.IsDisposabled)
break;
......@@ -150,6 +150,8 @@ namespace VIZ.Framework.Core
}
}
lock (this.Pool)
{
foreach (DelayInfo info in removeList)
{
this.Pool.Remove(info.Key);
......
......@@ -132,11 +132,15 @@ namespace VIZ.Framework.Core
{
while (!this.IsDisposabled)
{
List<TimerInfo> list = null;
lock (this.Pool)
{
list = this.Pool.Values.ToList();
}
DateTime now = DateTime.Now;
List<TimerInfo> removeList = new List<TimerInfo>();
foreach (TimerInfo info in this.Pool.Values)
foreach (TimerInfo info in list)
{
if (this.IsDisposabled)
break;
......@@ -144,26 +148,22 @@ namespace VIZ.Framework.Core
if (info.ExecuteTime + info.Interval > now)
continue;
try
{
if (info.IsDisposed)
{
removeList.Add(info);
}
info.Action?.Invoke();
}
catch (Exception ex)
if (info.Action != null)
{
log.Error(ex);
ThreadHelper.SafeRun(info.Action);
}
finally
{
info.ExecuteCount++;
info.ExecuteTime = now;
}
}
lock (this.Pool)
{
foreach (var item in removeList)
{
this.Pool.Remove(item.Key);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment