Interact with Html
Create new Project of SilverLight Applicatioon with host website
Add Following code to the .html page
.htmlContainer
{
margin-top:115px;
margin-left:0px;
float:left;
font-size:1.4em;
}
<div class="htmlContainer">
<input type="text" name="txtHtml" />
<input type="button" name="btnHtml" value="Demo" onclick="CallSL()" />
</div>
Main.Xaml:
<Grid x:Name="LayoutRoot" Background="White">
<Canvas>
<TextBox Canvas.Left="211" Canvas.Top="99" Height="23" Name="txtSV" Width="120" />
<Button Canvas.Left="211" Canvas.Top="149" Content="InteractHTML" Height="23" Name="btnInteractHtml" Width="75" Click="btnInteractHtml_Click" />
<Button Canvas.Left="218" Canvas.Top="32" Content="Call JS" Height="26" Name="btnJS" Width="113" Click="btnJS_Click" />
</Canvas>
</Grid>
Main.Xaml.cs:
Add namespace--> using System.Windows.Browser;
public MainPage()
{
InitializeComponent();
HtmlPage.RegisterScriptableObject("Page", this);
}
private void btnInteractHtml_Click(object sender, RoutedEventArgs e)
{
HtmlDocument doc = HtmlPage.Document;
HtmlElement element = doc.GetElementById("txtHtml");
if (element != null)
element.SetProperty("value", txtSV.Text);
}
Interact with JavaScript
Add Code to Html and Main.xaml page and see
In Html Page
function ShowMsg(message) {
alert(message);
}
function CallSL() {
var slcontrol = document.getElementById('silverlightControlHost');
slcontrol.Content.Page.Msg();
}
<div class="htmlContainer">
<input type="text" name="txtHtml" />
<input type="button" name="btnHtml" value="Demo" onclick="CallSL()" />
</div>
Main.Xaml.cs
private void btnJS_Click(object sender, RoutedEventArgs e)
{
HtmlPage.Window.Invoke("ShowMsg", "Hello SLight");
}
[ScriptableMember]
public void Msg()
{
MessageBox.Show("From SL");
}
No comments:
Post a Comment