Custom userControl events still running in the code in the visual studio, is that a bug?

Asked

Viewed 2,051 times

0

I am developing in WPF, and while doing a custom userControl, I had a surprise, when I went to use the Events "Routedeventhandler Loaded" and "Routedeventhandler Unloaded" of "System.Windows.Frameworkelement", I noticed that they fired when I wrote in the code.. I don’t know if this is normal, but I think it’s a bug, because theoretically it wasn’t supposed to trigger the event without the application running. Is this really a bug, or does it have an explanation?

If anyone wants to test I’ll put the code in.. (I’m using vs2017 Enterprise, framework 4.7.2)

userControl.Cs: (Custom userControl)

using System.Windows;
using System.Windows.Controls;

namespace wpf.componentes
{
    public class userControl : UserControl
    {
        private Window janelaAtual = new Window();
        private int i = 1;

        public userControl()
        {
            Loaded += userControl_Loaded;
            Unloaded += userControl_Unloaded;
            janelaAtual.Content = Window.GetWindow(this);
        }

        private void userControl_Loaded(object sender, RoutedEventArgs e)
        {
            InicializarConteudo();
        }

        private void userControl_Unloaded(object sender, RoutedEventArgs e)
        {
            FinalizarConteudo();
        }

        public virtual void InicializarConteudo()
        {
            MessageBox.Show("Carregar conteúdo nº: " + i);
            i++;
        }

        public virtual void FinalizarConteudo()
        {
            MessageBox.Show("Finalizar conteúdo nº: " + i);
            i++;
            janelaAtual.Close();
        }
    }
}

Mainwindow.xaml:

<Window x:Class="wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpf="clr-namespace:wpf.componentes"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <wpf:userControl />
    </Grid>
</Window>

Mainwindow.xaml.Cs:

using System.Windows;

namespace wpf
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

After having this code written, just compile without running the application, and view Mainwindow.xaml.. the message will appear, daughter of the instance of Visual Studio, because I can’t do anything in VS until I give OK to the message, then try to comment on the section where I include userControl in Mainwindow.xaml, the message will appear again..

  • view Mainwindow.xaml, you say open it in design mode?

  • is in the shaman, no design..

  • 1

    But wait, how do you know that it is triggering these events if you are not running the application and nor debugging your code?

  • read the post to the end? just compile the application, without running anything, or enter into debug, the event shoots in the own visual studio.. Soon after, I did the test by unchecking the design in "Tools > Options > XAML Designer > Enable XAML Designer", and it didn’t fire anymore.. Probably when you render Design, you’re running the events.. but theoretically it wasn’t meant to fire, you can test there.. for me this is a bug, but I am looking for explanations before reporting

  • 1

    read my first question? Joke... guy doesn’t seem like a bug in fact... If you have some components rederization in some instance, some events can be invoked... userControl has been loaded by main, right? But in this case, it’s just out of curiosity or is it getting in your way somehow?

  • hahah.. sorry if I was rude, hard to explain myself around here, I use this site.. Yes, it bothers me, because every time I compile, events are executed, and to do tests, I put these messages.. but to solve this problem, I removed the design.. I was curious, and if it is a bug, I can report to fix it ;D

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.