<Canvas Width="300" Height="300"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Transparent"
MouseLeftButtonDown="createEllipse">
<TextBlock Text="click for circle" FontSize="40"/>
</Canvas>
function createEllipse(sender, args) {
var slControl = sender.getHost();
var e =
slControl.content.createFromXaml(
'<Ellipse Height="200" Width="200" Fill="Blue"/>');
var canvas = sender;
canvas.children.Add(e);
}
The idea is to concatenate the values received from GeneXus in the xaml that is passed to the createFromXaml function.
Another possible solution (which seems to be easier to handle), is to use the Load event of the Silverlight control to solve both problems, that is, to subscribe to the control events and to pass the properties to the control. The LOAD event will be handled by a class method of our user control. Thus, we will have access to all the properties and methods of the GeneXus user control. For example:
<Canvas Height="300" Width="300"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="transparent">
<TextBlock x:Name="myTextblock" Text="hello world" FontSize="50">
<TextBlock.Foreground>
<SolidColorBrush x:Name="myTextblockBrush" Color="Black"/>
</TextBlock.Foreground>
</TextBlock>
<Ellipse x:Name="myEllipse"
Height="200" Width="200"
Canvas.Left="30" Canvas.Top="80"
Stroke="Black" StrokeThickness="10" Fill="LightBlue" />
</Canvas>
function HelloWorld()
{
this.Width;
this.Height;
this.FontFace;
this.FontColor;
this.FontSize;
this.show = function()
{
///UserCodeRegionStart:[show] (do not remove this comment.)
this.createMySilverlightControl();
///UserCodeRegionEnd: (do not remove this comment.)
}
this.createMySilverlightControl = function()
{
var imgsDir = gx.staticDirectory;
Sys.Silverlight.createObjectEx({
source: gx.util.resourceUrl(imgsDir + 'SilverlightHelloWorld/helloworld.xaml', true),
parentElement: this.getContainerControl(),
id: this.containerName + "Silverlight",
properties: {
width: this.Width,
height: this.Height,
framerate:'24',
version: "0.9"
},
events: {
onLoad: Sys.Silverlight.createDelegate(this, this.handleLoad)
}
});
}
this.handleLoad = function(control, userContext, rootElement)
{
//myEllipse
rootElement.findName("myEllipse").addEventListener("MouseEnter", Sys.Silverlight.createDelegate(this, this.MyEllipseMouseEnter));
rootElement.findName("myEllipse").addEventListener("MouseLeave", Sys.Silverlight.createDelegate(this, this.MyEllipseMouseLeave));
//myTextblock
rootElement.findName("myTextblock").FontSize = this.FontSize;
rootElement.findName("myTextblock").FontFamily = this.FontFace;
rootElement.findName("myTextblockBrush").Color = this.FontColor;
}
this.MyEllipseMouseEnter = function(sender, args)
{
sender.findName("myEllipse").fill = "red";
}
this.MyEllipseMouseLeave = function(sender, args)
{
sender.findName("myEllipse").fill = "LightBlue";
}
}
Sys.Silverlight.createDelegate = function(instance, method) {
return function() {return method.apply(instance, arguments);}
}
We can see how the handleLoad method is subscribed to the events and how properties are assigned. Note also that the createObjectEx function is passed this.getContainerControl() as parent, so it won't be necessary to do a this.setHtml.
Sample
Silverlight Hello World can be downloaded here. Basically, it prints a Hello World message using the font, size and color received from GeneXus, and draws an ellipse that changes color on MouseEnter/MouseLeave.