using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Threading;
using FWindSoft.Data;
namespace FWindSoft.Wpf.Controls
{
///
/// 导航窗体
///
[ContentProperty("Content")]
public class NChildWindow:UserControl
{
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title",
typeof(string),
typeof(NChildWindow), new PropertyMetadata(string.Empty));
public static readonly DependencyProperty AttachElementProperty = DependencyProperty.Register("AttachElement",
typeof(UIElement),
typeof(NChildWindow), new PropertyMetadata(null));
public NChildWindow()
{
BindingWidth();
this.Loaded+=NChildWindow_Loaded;
this.Unloaded+=NChildWindow_Unloaded;
//AttachElements = new ObservableCollection();
}
#region 事件封装
private static void NChildWindow_Unloaded(object sender, RoutedEventArgs e)
{
NChildWindow nW = sender as NChildWindow;
if (nW == null)
return;
nW.OnClose(new NChildWindowArgs(e) { RefParameter = NavigationBar.GetParameter(nW) });
}
private static void NChildWindow_Loaded(object sender, RoutedEventArgs e)
{
NChildWindow nW = sender as NChildWindow;
if (nW == null)
return;
nW.OnLoad(new NChildWindowArgs(e) { RefParameter = NavigationBar.GetParameter(nW) });
}
#endregion
protected virtual void OnLoad(NChildWindowArgs args )
{
if (EveryTimeLoad || !m_IsLoadData)
{
if (OnceBreakLoad.Value)
{
return;
}
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => {
if (args.RefParameter != null)
{
LoadData(args.RefParameter.StartParameter);
}
else
{
LoadData(null);
}
m_IsLoadData = true;
}));
}
}
protected virtual void OnClose(NChildWindowArgs args)
{
}
///
/// 窗口名称
///
public string Title
{
get { return this.GetValue(TitleProperty) as string; }
set { this.SetValue(TitleProperty,value); }
}
#region 打开窗体
public virtual void Show()
{
if (NavigationBar.CurrentNavigation != null)
{
NavigationBar.CurrentNavigation.Add(this);
}
}
public virtual void Show(NavigationBar bar)
{
if (bar != null)
{
bar.Add(this);
}
}
public virtual void Show(UIElement parent)
{
var bar = parent.GetParentType();
if (bar != null)
{
bar.Add(this);
}
}
public void Show(NParameter parameter)
{
NavigationBar.SetParameter(this, parameter);
Show();
}
#endregion
#region 加载信息模块
private bool m_IsLoadData = false;
public void ResetLoadData()
{
this.m_IsLoadData = false;
}
///
/// 每次都加载
///
public bool EveryTimeLoad { get; protected set; }
///
/// 是否打断一次加载【首次加载也可打断】
///
public MomentVariable OnceBreakLoad { get; private set; } = new MomentVariable(false);
protected virtual void LoadData(LoadParameter parameter)
{
}
#endregion
public void SetStartParameter(NParameter parameter)
{
NavigationBar.SetParameter(this, parameter);
}
public virtual void Close()
{
if (NavigationBar.CurrentNavigation != null)
{
NavigationBar.CurrentNavigation.Remove(this);
}
}
public void Close(NParameter parameter)
{
NavigationBar.SetParameter(this, parameter);
Close();
}
///
/// 附加元素
///
public UIElement AttachElement
{
get { return this.GetValue(AttachElementProperty) as UIElement; }
set { this.SetValue(AttachElementProperty, value); }
}
#region 附加功能
private void BindingWidth()
{
RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
rs.AncestorType = typeof(ScrollContentPresenter);
Binding binding = new Binding("ActualWidth") { RelativeSource = rs };
this.SetBinding(WidthProperty, binding);
}
#endregion
}
public class NChildWindowArgs : RoutedEventArgs
{
public NChildWindowArgs(RoutedEventArgs args)
{
this.RoutedEvent = args.RoutedEvent;//他的顺序要在handled赋值之上
this.Source = args.Source;
this.Handled = args.Handled;
}
///
/// 关联参数
///
public NParameter RefParameter { get; set; }
}
}