silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SL三个基本问题:窗口跳转、变量保存、参数传递大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

1.窗口跳转   在App中定义一个Grid控件: Grid rootGrid = new Grid(); private void Application_Startup(object sender, StartupEventArgs E) {     // this.RootVisual = new MainPage();     this.RootVisual = rootGrid;  th

1.窗口跳转

 

在App中定义一个Grid控件:

Grid rootGrid = new Grid();

private void Application_Startup(object sender,StartupEventArgs E)
{
    // this.RootVisual = new MainPage();
    this.RootVisual = rootGrid;
 this.rootGrid.Children.Add(new Login());
}

在App里面写一个方法解决页面转向:

public void RedirectTo(UserControl usercontrol)
{
  App app=(App)Application.Current;
  app.rootGrid.Children.Clear();
  app.rootGrid.Children.Add(usercontrol);
}

在xmal调用这个方法:

App app=(App)Application.Current;
// app.variablename = txtUserName.Text.Trim();
app.RedirectTo(new MainPage());


2.变量保存

Session是运行在服务器上的,而Silverlight运行在客户端。因此在Silverlight中使用Session的说法并不正确。

有两种方法实现Silverlight与Session的关联:

 

方法一、通过WCF使用ASP.NET中的Session[因BasichttpBinding不支持WCF中的Session,如使用WCF会话将报错 ]

  首先:在web.config中<system.serviceModel >下添加:

    <serviceHosTingEnvironment aspNetCompatibilityEnabled="true"/>

  然后:在服务类[不是接口]下添加如下属性:

    [AspNetCompatibilityrequirements(requirementsMode = AspNetCompatibilityrequirementsMode.Allowed)]

  接下来就可以使用Session,记得添加System.Web的引用

    httpContext.Current.Session["YourName"] = something;

    object something = httpContext.Current.Session["YourName"];

 

方法二、在客户端新建一个静态类模拟Session

     如要保存登陆信息,可在验证了用户名、密码之后在客户端保存相关信息。

using System;
using System.Collections.Generic;

namespace SessionDemo
{
    public static class SessionManager
    {
        private static Dictionary<String,object> session = new Dictionary<String,object>();

        public static Dictionary<String,object> Session
        {
            get { return SessionManager.session; }
            set { SessionManager.session = value; }
        }
    }
}

使用方法:

赋值:
SessionManager.Session["uname"] = "kunal";

取值:
txbUname.Text = SessionManager.Session["uname"].ToString();

 

3.参数传递

 

xaml向aspx :

 

HtmlWindow html = HtmlPage.Window;  

String url = "http://localhost:24690/MasterMain.aspx?USERID=" + UserID+"&ROLEID="+RolEID;                            
html.Navigate(new Uri(url));  


aspx向xaml:

 

html页面中:
<param name="initparams" value="path=GeneratedImages/dzc_output.xml,zoomIn=3" />
App里:
if (e.InitParams != null){
  foreach (var data in e.InitParams){


  }
}

还可以采用以下直接的方法:把参数直接存在: <input type="hidden" id="YouParaName" name="YouParaName" value="YouParaValue" >然后在SL里面:HtmlPage.Document.GetElementById("YouParaName").GetAttribute("value")这样也很方便,而且更容易理解,方便Post到服务器。

@H_944_146@

大佬总结

以上是大佬教程为你收集整理的SL三个基本问题:窗口跳转、变量保存、参数传递全部内容,希望文章能够帮你解决SL三个基本问题:窗口跳转、变量保存、参数传递所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。