
====================================================================================================== 实现示例 设计Silverlight程序 首先,新建一个Silverlight程序,我这里将他命名为SilverX。 然后,新建一个自定义控件用于承载输入框,我这里将它命名为 SXPasswordBox 接下来,打开 SXPasswordBox.xaml,并将代码修改为以下代码。
<UserControl x:Class="SilverX.SXPasswordBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/Expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="50" d:DesignWIDth="400"> <GrID x:name="LayoutRoot" Background="White"> <PasswordBox x:name="_Value"/> </GrID></UserControl>
然后打开 SXPasswordBox.cs 加入一个方法,并对Js脚本公开
[ScriptableMemberAttribute]//对脚本公开成员public String GetValue(){ //在这里可以添加相应的加密代码,此处直接返回输入框的值。 return this._Value.Password;} 打开Mainpage.xaml将刚刚写的控件拖进来
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/Expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SilverX" x:Class="SilverX.MainPage" mc:Ignorable="d" d:DesignHeight="50" d:DesignWIDth="400"> <GrID x:name="LayoutRoot" Background="White"> <local:SXPasswordBox x:name="pwdBox"/> </GrID></UserControl>
打开Mainpage.cs ,注册刚刚拖进来的控件。
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += (obj,e) => { HTMLPage.RegisterScriptableObject("pwdBox",this.pwdBox); }; } } 这样,我们就完成了控件设计。
在页面中调用 首先,我们在Silverlight承载网站中打开承载此Silverlight应用的页面(SilverXTestPage.HTML),并且加入以下脚本
<script type="text/JavaScript"> var control = null; function plugInLoaded(sender,args) { control = sender.getHost();//获取宿主 } function getValue() { try { this.Text1.value= control.Content.pwdBox.GetValue(); } catch (e) { alert(e); } }</script> 将Object代码段修改为以下内容:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" wIDth="400" height="27"> <param name="source" value="ClIEntBin/SilverX.xap"/> <param name="onError" value="onSilverlightError" /> <param name="background" value="white" /> <param name="minRuntimeVersion" value="5.0.61118.0" /> <param name="autoUpgrade" value="true" /> <param name="onLoad" value="plugInLoaded"/> <!--这一行是新加的--> <a href="http://go.microsoft.com/fwlink/?linkID=149156&v=5.0.61118.0" > <img src="http://go.microsoft.com/fwlink/?linkID=161376" alt="获取 Microsoft Silverlight" /> </a> </object>
设计页面,加入一个输入框和一个按钮
<input ID="Text1" type="text" /><input ID="button1" type="button" value="Get" onclick="getValue();"/><!--点击按钮调用Js函数-->
运行效果:
示例程序下载:SilverX.zip 总结
以上是内存溢出为你收集整理的用Silverlight自定义控件代替ActiveX安全密码输入框全部内容,希望文章能够帮你解决用Silverlight自定义控件代替ActiveX安全密码输入框所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)