0

silverlight我试图RootVisual从另一个类分配对象时。

这样做的原因是 JavaScript 将执行一些Ajax查询,并且需要随时动态更改UI element

这是我到目前为止所做的,它似乎不起作用。

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;
    }

这是我的 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);


    }



}

这是调用 Scriptable myClas 的 JS

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

     slControl.Content.jsMyClass.SetVisual(1);
}
4

1 回答 1

1

我这样做的方式是保持根视觉相同,但更改其中的内容。

所以在你的 App.cs 文件中你这样做

            //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();

然后当你想切换到不同的视图时,你可以这样做

(App.Current.RootVisual as UserControl).Content = new Views.PreQualView();
于 2014-03-26T11:13:06.823 回答