页面间传递数据的方式,使用Event

1.普通方式
新建两个页面:
子窗体
namespace GreenFutures.SLClient.Controls
{
public partial class NewWindowTypeWindow : ChildWindow
{
public event EventHandler ClickedReturn;
private void btnReturn_Click(object sender, RoutedEventArgs e)
{
if (ClickedReturn != null)
{
ClickedReturn(sender, e);
}
}
}
}
父窗体
namespace GreenFutures.SLClient.Views
{
public partial class MyDesktop : Page
{
private void m_QuotaList_SelectedQuotaChanged(object sender, SelectedQuotaChangedEventArgs e)
{
if (m_QuotaChart == null)
{
m_QuotaChart = new QuotaChart();
m_QuotaChart.ClickedReturn += new System.EventHandler(m_QuotaChart_ClickedReturn);
}
m_ContractId = e.ContractId;
ctlTransitioningContent.Content = m_QuotaChart;
}
private void m_QuotaChart_ClickedReturn(object sender, System.EventArgs e)
{
ctlTransitioningContent.Content = m_QuotaList;
}
}
}

子窗体中,在一个按钮事件中调用了一个EventHandler事件。

在父窗体中的一个SelectQuotaChanges事件中,定义子窗体,同时注册子窗体的事件,然后在父窗体的子控件的事件中处理相应逻辑

2.通过委托(delegate),定义Event 带参数类型

子窗体
namespace GreenFutures.SLClient.Controls
{
public partial class NewWindowTypeWindow : ChildWindow
{
#region event
public delegate void Return(NewWindowTypeData newWindowTypeData);
public event Return ClickSureBtnReturn;
#endregion
private void btnReturn_Click(object sender, RoutedEventArgs e)
{
if (ClickSureBtnReturn != null)
{
if (lstWindow.SelectedItem != null)
{
NewWindowTypeData data = lstWindow.SelectedItem as NewWindowTypeData;
ClickSureBtnReturn(data);
}
}
}
}
}
父窗体
namespace GreenFutures.SLClient.Views
{
public partial class MyDesktop : Page
{
private void btnNewWindow_Click(object sender, RoutedEventArgs e)
{
m_window = new NewWindowTypeWindow();
m_window.ClickSureBtnReturn += new NewWindowTypeWindow.Return(window_ClickSureBtnReturn);
}
private void window_ClickSureBtnReturn(NewWindowTypeData newWindowTypeData)
{
if (m_window.DialogResult == true)
{
m_nameGuid = Guid.NewGuid().ToString();
MyDesktopSetting setting = lstMyDesktop.SelectedItem as MyDesktopSetting;
RadDocking radDocking = m_DesktopControlDictionary[setting];
RadSplitContainer splitContainer = new RadSplitContainer();
splitContainer.SetValue(RadSplitContainer.InitialPositionProperty, DockState.FloatingDockable);
RadPaneGroup paneGroup = new RadPaneGroup();
AddWindow(newWindowTypeData.WindowType, splitContainer, paneGroup, 500, 300, 601, 246, "add_pane" + m_nameGuid, "add_paneTar" + m_nameGuid,
newWindowTypeData.WindowName, "add_content" + m_nameGuid);
splitContainer.Items.Add(paneGroup);
radDocking.Items.Add(splitContainer);
}
}
}
}
通过在子窗体定义事件,可以将参数传递到父窗体中,进行相应的逻辑操作,同事由此方法也可以定义带返回值的委托方法,例如:
public delegate double Return(NewWindowTypeData newWindowTypeData)
public event Return ClickSureBtnReturn;
3.Event的参数为继承EventArgs的自定义类
子窗体
namespace GreenFutures.SLClient.Views.Controls
{
public partial class Quota : UserControl
{
//定义事件
public event EventHandler<SelectedQuotaChangedEventArgs> SelectedQuotaChanged;
private void grdRoot_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (grdRoot.SelectedItem != null)
{
LocalLastQuota quota = this.grdRoot.SelectedItem as LocalLastQuota;
if (SelectedQuotaChanged != null)
{
SelectedQuotaChanged(this, new SelectedQuotaChangedEventArgs(quota.ContractId));
}
}
}
}
public class SelectedQuotaChangedEventArgs : EventArgs
{
private int m_ContractId;
public int ContractId
{
get { return m_ContractId; }
set { m_ContractId = value; }
}
public SelectedQuotaChangedEventArgs(int contractId)
{
m_ContractId = contractId;
}
}
}
父窗体
namespace GreenFutures.SLClient.Views
{
public partial class Home : Page
{
public Home()
{
InitializeComponent();
this.Title = ApplicationStrings.HomePageTitle;
m_QuotaList = new QuotaList();
m_QuotaList.SelectedQuotaChanged += new System.EventHandler<SelectedQuotaChangedEventArgs>(m_QuotaList_SelectedQuotaChanged);
ctlTransitioningContent.Content = m_QuotaList;
}
private void m_QuotaList_SelectedQuotaChanged(object sender, SelectedQuotaChangedEventArgs e)
{
m_ContractId = e.ContractId;
ctlTransitioningContent.Content = m_QuotaChart;
}
}
}
通过继承EventArgs类来重写一个类,类中定义一个属性ContractId,在父窗体中进行调用子控件的事件时,通过事件参数获得ContractId
其实该操作时实现父控件和子控件间传递参数,名为ContractId。除了本实例中的方法外,通过第2步操作介绍的方法也可以实现,例如,
在子控件中定义事件:
public delegate void Return(int contractId);
public event Return SelectedQuotaChanged;
声明该事件
if (grdRoot.SelectedItem != null)
{
LocalLastQuota quota = this.grdRoot.SelectedItem as LocalLastQuota;
if (SelectedQuotaChanged != null)
{
SelectedQuotaChanged(quota.ContractId);
}
}
在父控件中注册该事件,调用方式:
private void m_QuotaList_SelectedQuotaChanged(int contractId)
{
m_ContractId = contractId;
ctlTransitioningContent.Content = m_QuotaChart;
}
4.在子页面中注册已定义的事件
子控件窗体
public class LastQuotaData
{
private LastQuotaData()
{
Initialize();
}
#region Events
private event EventHandler Initialized;
/// <summary>
/// 添加数据初始化完成事件接收函数?
/// </summary>
/// <param name="handler"></param>
public void AddInitializedHander(EventHandler handler)
{
Initialized += handler;
if (m_IsInitialized)
{
handler(null, null);
}
}
/// <summary>
/// 移除数据初始化完成事件接收函数
/// </summary>
/// <param name="handler"></param>
public void RemoveInitializedHander(EventHandler handler)
{
Initialized -= handler;
}
#endregion
protected void Initialize()
{
SyncIntervalTime = TimeSpan.FromSeconds(1);
m_IsInitialized = true;
if (Initialized != null)
{
Initialized(null, null);
}
}
}
控件窗体
namespace GreenFutures.SLClient.Views.Controls
{
public partial class Quota : UserControl
{
privae EventHandler m_LocalLastDataInitializedEventHandler;
m_LocalLastDataInitializedEventHandler = new EventHandler(m_LocalLastData_Initialized);
m_LocalLastData.AddInitializedHander(m_LocalLastDataInitializedEventHandler);
private void m_LocalLastData_Initialized(object sender, EventArgs e)
{
//处理相关逻辑
}
}
}
需要注意的是他的特别之处在于在子页面类中声明了注册了事件,在控件中调用了一个包装的方法。
其实这种情况的使用不是父子控件中的调用,而是一个控件窗体中的调用,具体情况为:在一个窗体中,
通过异步方法函数获得返回值,确定是否异步加载完毕。
定义一个事件来告知是否完成初始化数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值