一.添加用户控件
1.在项目工程中添加-》新建项-》Silverlight用户控件(SilverlightControl.xaml)
2.Page 页中
<UserControl x:Class="UserControlDemo.Page"
xmlns:jl="clr-namespace:UserControlDemo;assembly=UserControlDemo">
使用:
<jl:SilverlightControl x:Name="HomeAddress" />
二.清除用户控件的方法
1.用户控件部分
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public partial class SilverlightControl : UserControl
{
public class AddressEventArgs : RoutedEventArgs
{
public object Tag { get; set; }
public AddressEventArgs(object tag)
{
this.Tag=tag;
}
}
public delegate void AddressEventHandler(object o, AddressEventArgs e);
public event AddressEventHandler Closed;
void Close_Click(object sender, RoutedEventArgs e)
{
// remove this from the parent's children collection
Panel parent = this.Parent as Panel;
if (parent != null)
{
parent.Children.Remove(this);
// alert the parent that we closed
if (Closed != null)
{
Closed(this, new AddressEventArgs(this.Tag));
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
void Create_Click(object sender, RoutedEventArgs e)
{
SilverlightControl auc = new SilverlightControl();
auc.Tag = "1";
auc.Closed += new SilverlightControl.AddressEventHandler(auc_Closed);
MasterContainer.Children.Add(auc);
}
void auc_Closed(object o,SilverlightControl.AddressEventArgs e)
{
//Code用户控件关闭后的操作
}