Question

In silverlight Im trying to assign the RootVisual object from another class.

The reason for this is that JavaScript will be performing some Ajax queries and will need to dynamically change the UI element at any time.

Here is what I have done so far, it doesn't seem to work.

public class MyClass
{

    private UIElement _rootVisual;

    public MyClass(UIElement root)
    {
        _rootVisual = root;
    }

    public bool SetVisual(int id)
    {

        switch(id) {
           case 0: 
               this._rootVisual = new MyUI1();
               break;
           default: 
               this._rootVisual = new MyUI2();
               break;
        }

        return true;
    }

Here is my App.xaml.cs

    private void Application_Startup(object sender, StartupEventArgs e)
    {

        // Create A Scriptable object
        this.myclass= new MyClass( this.RootVisual );


        // Register Scriptable for JS Interop 
        HtmlPage.RegisterScriptableObject("jsMyClass", myclass);



        //Load the initial UI but should be able to access/change later via JS
        myclass.LoadScene(0);


    }



}

Here is the JS that calls the Scriptable myClas

function _test()
{
     slControl = document.getElementById("SilverlightControl");

     slControl.Content.jsMyClass.SetVisual(1);
}
Was it helpful?

Solution

The way I do it is to keep the root visual the same but change the content within it.

So in your App.cs file you do this

            //Setup a root content user control so we can swap out the content depending on what we want to show
        UserControl RootContent = new UserControl();
        RootContent.HorizontalAlignment = HorizontalAlignment.Stretch;
        RootContent.VerticalAlignment = VerticalAlignment.Stretch;
        DispatcherHelper.Initialize();
        this.RootVisual = RootContent;
        (this.RootVisual as UserControl).Content = new SplashScreenView();

And then when you want to switch to a different view you can just do this

(App.Current.RootVisual as UserControl).Content = new Views.PreQualView();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top