【问题标题】:How to assign RootVisual from another class如何从另一个类分配 RootVisual
【发布时间】:2014-03-25 11:04:46
【问题描述】:

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

【问题讨论】:

    标签: c# silverlight interop scriptable


    【解决方案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();
    

    【讨论】:

    • 感谢您发布此信息。我也想通了,但没有时间更新我的问题。我现在试图实现从一个场景到另一个场景的过渡,但我猜那是另一个问题。再次感谢
    猜你喜欢
    • 2015-08-30
    • 2019-01-22
    • 2016-02-19
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2013-04-03
    相关资源
    最近更新 更多