开发者

Changing theme calls UserControl_Loaded event

I am not getting why behavior of WPF user control and Windows forms user control is different . I added window loaded event which just shows message box as :

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Main Window Loaded","WPF");
    }

Also I created one user control and added loaded event as :

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("User Control Loaded.","WPF");
    }

I have put this user control in main window.

When i launch this , I get both message box, User controls as well as windows.

Now , When i change my theme from Aero to any High contrast then user control's message box is开发者_运维技巧 shown again.

Why this is happening ? Why this is different from Windows form ? What should I do to avoid showing it multiple times ?

Wajeed


You could have a boolean field which stores the state of whether the dialog was shown yet or not. If you change the theme the UI-elements will reload so naturally the event will fire again.

if (!_diagWasShown)
{
    _diagWasShown = true;
    //Show dialogue
}


you can create bool variable, which will indicate if MessageBox was shown.

bool isUserMessageBoxShown = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    if (!isUserMessageBoxShown)
    {
        MessageBox.Show("User Control Loaded.","WPF");
        isUserMessageBoxShown  = true; 
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜