建一个文件SerialRecorderUpdWinProxy.xaml,前端可以为空
此SerialRecorderUpdWinProxy.xaml.cs文件的代码如下
namespace SerialRecorder.Views
{
public partial class SerialRecorderUpdWinProxy : UserControl
{
private string _ChildWindowControlType;
private ChildWindow _Child;
public object Child
{
get { return _Child; }
set { _Child = (ChildWindow)value; }
}
public string ChildWindowControlType
{
get { return _ChildWindowControlType; }
set
{
_ChildWindowControlType = value;
_Child = (ChildWindow)Activator.CreateInstance(Type.GetType(_ChildWindowControlType));
}
}
public SerialRecorderUpdWinProxy()
{
Visibility = Visibility.Collapsed;
}
public void Show()
{
//_Child.Width = Width;
//_Child.Height = Height;
_Child.Show();
}
}
}
在需要使用ChildWindow的页面中添加资源
<UserControl.Resources>
<Views:SerialRecorderUpdWinProxy x:Key="SerialRecorderUpdWinProxy" ChildWindowControlType="SerialRecorder.Views.SerialRecorderUpdWin" />
</UserControl.Resources>
<Button Content="新增" Height="23" Name="button2" Width="75" Margin="0,0,10,0" Command="{Binding ClickNewButton}" CommandParameter="{StaticResource SerialRecorderUpdWinProxy}" />
调用Show()方法
private DelegateCommand clickNewButton;
public DelegateCommand ClickNewButton
{
get
{
if (clickNewButton == null)
{
clickNewButton = new DelegateCommand((parameter) =>
{
parameter.GetType().GetMethod("Show").Invoke(parameter, null);
});
}
return clickNewButton;
}
set { clickNewButton = value; }
}