• SilverLight在aspx页面调用自定义的控件


    需要在不同的aspx页面调用不同的usercontrol控件,需要修改App.xaml.cs中Application_Startup函数中的代码,因为silverlight中都是从这个函数中去调用xaml文件的。

    方法1,修改App.xaml.cs代码如下:

      private void Application_Startup(object sender, StartupEventArgs e)
            {
                //this.RootVisual = new MainPage();
                if (!e.InitParams.ContainsKey("InitPage"))
                {
                    this.RootVisual = new MainPage();
                    return;
                }
                
                switch(e.InitParams["InitPage"])
                {
                    case "ConfrimBox":
                        this.RootVisual = new ConfrimBox();
                        return;
                    default :
                        this.RootVisual = new ConfrimBox();
                        return;
                }
            }

     其中index.aspx的代码如下:、

        <div id="silverlightControlHost">
            <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
                width="100%" height="100%">
                <param name="source" value="ClientBin/SecondTest.xap" />
                <param name="InitParams" value="InitPage=ConfirmBox" />
                <param name="onError" value="onSilverlightError" />
                <param name="background" value="white" />
                <param name="minRuntimeVersion" value="5.0.61118.0" />
                <param name="autoUpgrade" value="true" />
                <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration: none">
                    <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight"
                        style="border-style: none" />
                </a>
            </object>
            <iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px;  0px;
                border: 0px"></iframe>
        </div>

    注意看标红的一行代码,这行代码里面指定了param参数InitParams,并且key为"InitPage",value为"ConfrimBox"
    这时再看Application_Startup函数中的代码,根据判断是否带有InitParams参数,如果则根据其value值打开相应的UserControl.

    方法2:从第一种方式我们看出了弊端,实际项目中不可能这么去写switch,那还不写死去,所以我们想到了反射,代码如下:

     //使用反射
                //----------------------------反射方法1--------------------------------------//
                //Assembly assembly = Assembly.GetExecutingAssembly();    
                //string rootName = string.Format("SecondTest.{0}", e.InitParams["InitPage"]);
                //UIElement rootVisual = assembly.CreateInstance(rootName) as UIElement;
                //this.RootVisual = rootVisual;
    
                //---------------------------------反射方法2----------------------------------//
                String rootName = String.Format("SecondTest.{0}", e.InitParams["InitPage"]);
                Type type = Type.GetType(rootName);
                UIElement rootVisual = Activator.CreateInstance(type) as UIElement;
                this.RootVisual = rootVisual;

    这样就可以实现我们的要求了。

  • 相关阅读:
    防止SSH自动断线
    php intval()和floatval()
    开启 intel vt-d
    前用户sudo免密码
    分区工具parted的详解及常用分区使用方法
    Proxmox qm命令应用实例
    Mac上制作Centos7系统U盘安装盘
    FinalShell Mac OS版,Linux版安装及教程
    python爬虫彩票案例,并自动发微信
    Python利用itchat库向好友或者公众号发消息
  • 原文地址:https://www.cnblogs.com/bianlan/p/2949382.html
Copyright © 2020-2023  润新知