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());
}
public void RedirectTo(UserControl usercontrol)
{
App app=(App)Application.Current;
app.rootGrid.Children.Clear();
app.rootGrid.Children.Add(usercontrol);
}
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"];
如要保存登陆信息,可在验证了用户名、密码之后在客户端保存相关信息。
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到服务器。