将参数从aspx传递给xaml

时间:2014-01-16 13:32:29

标签: c# asp.net silverlight-5.0

嗨,我是Silverlight的新手。下面是我的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/ICartPrinting.xap"/>
      <param name="onError" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="5.0.61118.0" />
      <param name="initparams" id="initParams" runat="server" value="key1=10" />
      <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;width:0px;border:0px"></iframe></div>

我正在使用10标记传递值initparams。我的MainPage.xaml代码是

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        var paramvalues = e.InitParams;
        foreach (var item in paramvalues)
        {
            MessageBox.Show(item.Value);    
        }
    }

根据此代码消息框应该显示,但是当我运行应用程序时,不会出现任何消​​息框。我也无法调试silverlight代码。任何帮助都会得到满足。感谢

2 个答案:

答案 0 :(得分:0)

我不认为您可以在应用程序启动期间调用可视控件。尝试这样的东西来获得价值:

<param name="initParams" value="key1=10" />

private void Application_Startup(object sender, StartupEventArgs e)
{
    foreach (var item in e.InitParams)
    {
        this.Resources.Add(item.Key, item.Value);
    }

    this.RootVisual = new MainPage();
}


public partial class MainPage : UserControl
{
    string module = string.Empty;
    int key1 = 0;

    public MainPage()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {

        if (App.Current.Resources.Contains("key1"))
        {
            int.TryParse(App.Current.Resources["key1"].ToString(), out key1);
        }

    }
}

答案 1 :(得分:0)

您必须先设置RootVisual才能显示MessageBox。

private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new MainPage();

    var paramvalues = e.InitParams;
    foreach (var item in paramvalues)
    {
        MessageBox.Show(item.Value);    
    }
}