Passing a parameter of "Type" to a function, how can a new instance of
that type be created? Example, form has many buttons, when one is pressed
it swaps out the current view control with one associated with the button
that was pressed.
There is 20 or so buttons in all and the layout isn't locked down (could change as
this is a first project with Silverlight / WPF and I'm just playing around at this point).
Since the layout could change I'm trying to keep all the logic in just a couple functions
instead of spread throughout all the button event handlers.
Thanks,
Paul
Current method (works)
void SwitchView(UserControl newView)
{
MainViewCtrl.Children.Clear();
newView.Margin = new Thickness(4);
MainViewCtrl.Children.Add(newView);
}
bool IsCurrentViewSameAs(Type ctrl)
{
UserControl uc = (UserControl)MainViewCtrl.Children[0];
if (uc.GetType() == ctrl)
return true;
return false;
}
void Btn1_Click(object sender, RoutedEventArgs e)
{
if (IsCurrentViewSameAs(typeof(ctrlView1)) == true)
return;
SwitchView(new ctrlView1());
}
void Btn2_Click(object sender, RoutedEventArgs e)
{
if (IsCurrentViewSameAs(typeof(ctrlView2)) == true)
return;
SwitchView(new ctrlView2());
}
Would like to somehow do something like the this?
void SwitchView(Type newViewType)
{
UserControl uc = (UserControl)MainViewCtrl.Children[0];
if (uc.GetType() == ctrl)
return;
MainViewCtrl.Children.Clear();
newView.Margin = new Thickness(4);
MainViewCtrl.Children.Add( ??????????? ); // <<-- Hm, how to create a new instance
// of type newViewType ???
}
void Btn1_Click(object sender, RoutedEventArgs e)
{
SwitchView(typeof(ctrlView1));
}
void Btn2_Click(object sender, RoutedEventArgs e)
{
SwitchView(typeof(ctrlView2);
}