I have developed a WPF application in which has a stack panel with four buttons that acts as a navigation system. The buttons correspond to a window. E.g About, Update, Rename and Exit. So when my application loads the AboutWindow
is opened. user can then select any button and the application will show that Window and close the current window.
XAML - AboutWindow.xaml
<StackPanel>
<Button Name="AboutBtn" Click="AboutBtnNavigate">About</Button>
<Button Name="UpdateBtn" Click="UpdateBtnNavigate">About</Button>
<Button Name="RenameBtn" Click="RenameBtnNavigate">About</Button>
<Button Name="ExitBtn" Click="ExitBtnNavigate">About</Button>
</StackPanel>
C# - AboutWindow.xaml.cs (class AboutWindow)
private void AboutBtnNavigate(object sender, RoutedEventArgs e)
{
this.Show()
}
private void UpdateBtnNavigate(object sender, RoutedEventArgs e)
{
UpdateWindow updateWindow = new UpdateWindow();
updateWindow.Show();
this.Close();
}
private void RenameBtnNavigate(object sender, RoutedEventArgs e)
{
RenameWindow renameWindow = new RenameWindow();
renameWindow .Show();
this.Close();
}
private void ExitBtnNavigate(object sender, RoutedEventArgs e)
{
ExitWindow exitWindow = new ExitWindow ();
exitWindow .ShowDialog();
}
So currently these event handlers are in the AboutWindow
class but I will need to use the same methods elsewhere, for example in UpdateWindow
class and RenameWindow
Class. Is there away to reuse these methods without having to rewrite the code, bearing in mind the code for each method will slightly change depending on the class they are in. Eg. if used in UpdateWindow
, UpdateBtnNavigate
will actually be just Show();
.
Essentially I am asking whats the best way to use OOP to limit the amount of repeating code in this situation. Hopefully this makes sense. Thanks in advance!