Skip to main content

Microsoft Silverlight

Answered Question How to switch userControls in code?RSS Feed

(0)

Kostja
Kostja

Member

Member

38 points

16 Posts

How to switch userControls in code?

Hello

I have 2 user controls in my project : 1- authentification form - AuthForm and 2 - ChatForm

AuthForm user control has event UserAuthentificated.

How can i switch between these controls in code? I mean when user is authetificated then AuthForm control shold be replaced byChatForm

public partial class App : Application

{

    public App()

    {

        this.Startup += this.Application_Startup;

        this.Exit += this.Application_Exit;
        this.UnhandledException += this.Application_UnhandledException;

        InitializeComponent();

    }

   
private void Application_Startup(object sender, StartupEventArgs e)

    {

        var AuthForm = new AuthForm();

        this.RootVisual = AuthForm;

        AuthForm.UserAuthentificated += new UserAuthEventHandler(AuthForm_UserAuthentificated);

    }

    private void Application_Exit(object sender, EventArgs e){}

    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e){}

    void AuthForm_UserAuthentificated(object sender, UserAuthEventArgs e)

    {

        var chatForm = new ChatForm();        this.RootVisual = chatForm;

    }

}

It doesn`t work

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP
Answered Question

Re: How to switch userControls in code?

Kostja,

You could create a container control as your RootVisual and then let the container control orchestrate the display of the authentication and chat forms.
 

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks

CraigN
CraigN

Member

Member

352 points

89 Posts

Answered Question

Re: How to switch userControls in code?

I tried this out and looked at the source for the RootVisual property. It goes off into some unmanaged code. I have however found a post from Pete Brown explaining it.

I suppose the general way you would have to do this would be to make some form of UserControl container class as was suggested above. I'm seeing if I can mock you one up quickly. If you beat me to it, post it here.

Microsoft Xbox MVP

CraigN
CraigN

Member

Member

352 points

89 Posts

Answered Question

Re: How to switch userControls in code?

I decided to post the solution on my blog over here. Hope it helps you out.

Microsoft Xbox MVP
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities