C#C
C#2y ago
Mashmo

Inheriting a base window class in WPF

I have a couple of windows that use the same CloseWindow, MinimizeWindow, and WindowMouseDown functions that are implemented the same
This feels kind of a bad practice, so I tried implementing a base function for window that has all 3 of those functions and inheriting it in all of those windows
However it gave me a lot of problems in using the base Window functions (e.g. Show and Close) outside the class, and much more errors
Does anybody know a better way to address this problem?
This is the class I tried using as a base class:
C#
namespace Client
{
    public abstract class TriviaClientWindow : Window
    {
        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
        private void CloseWindow(object sender, RoutedEventArgs e)
        {
            ((App)Application.Current).StopMusic();
            Thread.Sleep(50);
            Close();
        }
        private void MinimizeWindow(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}
Was this page helpful?