0

I have prepared more than one UserControl for a windows program in XAML. Each user control works as a separate page. But I do page transitions in navigate class. In ".xaml.cs" when calling User control

Navigate.navigate (navigate_grid, new DeviceLayout ());

I'm using the line of code. But every time I create a new user control, the background functions don't work. How do I flip one instead of invoking a new user control each time?

class Navigate
{
    public static void navigate(Grid grd, UserControl uc)
    {
        if (grd.Children.Count > 0)
        {
            grd.Children.Clear();
            grd.Children.Add(uc);
        }
        else { grd.Children.Add(uc); }
    }
}

Example navigate:

    public SettingsView()
    {
        InitializeComponent();
        Navigate.navigate(navigate_grid, new SystemLayout());
    }

    private void system_button_click(object sender, RoutedEventArgs e)
    {
        Navigate.navigate(navigate_grid, new SystemLayout());
        previous_page.Text = "";
        current_page.Text = "SİSTEM";
        next_page.Text = "UYGULAMA";
    }

    private void application_button_click(object sender, RoutedEventArgs e)
    {
        Navigate.navigate(navigate_grid, new ApplicationLayout());
        previous_page.Text = "SİSTEM";
        current_page.Text = "UYGULAMA";
        next_page.Text = "BAĞLANTI";
    }

    private void connection_button_click(object sender, RoutedEventArgs e)
    {
        Navigate.navigate(navigate_grid, new ConnectionLayout());
        previous_page.Text = "UYGULAMA";
        current_page.Text = "BAĞLANTI";
        next_page.Text = "ÜRÜNLER";
    }

    private void product_button_click(object sender, RoutedEventArgs e)
    {
        Navigate.navigate(navigate_grid, new ProductsLayout());
        previous_page.Text = "BAĞLANTI";
        current_page.Text = "ÜRÜNLER";
        next_page.Text = "CİHAZLAR";
    }

    private void device_button_click(object sender, RoutedEventArgs e)
    {
        Navigate.navigate(navigate_grid, new DeviceLayout());
        previous_page.Text = "ÜRÜNLER";
        current_page.Text = "CİHAZLAR";
        next_page.Text = "YAZICILAR";
    }

    private void printer_button_click(object sender, RoutedEventArgs e)
    {
        Navigate.navigate(navigate_grid, new PrinterLayout ());
        previous_page.Text = "CİHAZLAR";
        current_page.Text = "YAZICILAR";
        next_page.Text = "KULLANICILAR";
    }

    private void users_button_click(object sender, RoutedEventArgs e)
    {
        Navigate.navigate(navigate_grid, new UsersLayout());
        previous_page.Text = "YAZICILAR";
        current_page.Text = "KULLANICILAR";
        next_page.Text = "BAKIM";
    }

    private void maintenance_button_click(object sender, RoutedEventArgs e)
    {
        Navigate.navigate(navigate_grid, new MaintenanceLayout());
        previous_page.Text = "KULLANICILAR";
        current_page.Text = "BAKIM";
        next_page.Text = "HAKKINDA";
    }

    private void info_button_click(object sender, RoutedEventArgs e)
    {
        Navigate.navigate(navigate_grid, new InfoLayout());
        previous_page.Text = "BAKIM";
        current_page.Text = "HAKKINDA";
        next_page.Text = "";
    }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • As a note, you can safely call `grd.Children.Clear()` even when there are no child elements. The `if` statement is pointless. – Clemens Sep 12 '19 at 09:35
  • Besides that, your question isn't very clear. In order to avoid creating of new controls, just create them once and store their reference in a class member, e.g. a field. – Clemens Sep 12 '19 at 09:37
  • What I want to say is, let's say that there are 2 user controls named x and y. x has its own function. When I show y usercontrol on the screen, I want x's functions to work. – Emircan Aydın Sep 12 '19 at 10:38

1 Answers1

0

Not sure if I get what you mean. When you change the UserControl with the use of any of those functions eg info_button_click you can't access this funtion anymore. That would be the case, because your XAML and .cs file are one class, containing those funcitons. If you change the UserControl (XAML) you will also change the .cs file. Therefore you can't access those functions anymore.

You could probably get the behaviour you want if you bind the commands to a viewmodel, which you could then pass through the navigation as well?

Sry, I'm still not sure what exactly it is you're doing.

snicz
  • 61
  • 7
  • What I want to say is, let's say that there are 2 user controls named x and y. x has its own function. When I show y usercontrol on the screen, I want x's functions to work. – Emircan Aydın Sep 12 '19 at 10:38
  • Use MVVM for this, here is a general concept of this [link](https://stackoverflow.com/a/13156041/8649015). With [this](https://stackoverflow.com/a/1468830/8649015) you could make some `ICommand` commands, that can then be bound to the UI-elements in each view – snicz Sep 12 '19 at 12:25