Here’s an illustration of how to Command & Control a Mashup from an M3 program with a Smart Office Script, for example to launch a Mashup, to set values in a Mashup, to attach event handlers, to get values from the Mashup, and to close the Mashup. I learned this technique from norpe’s MashupBrowse post.
This is useful to add functionality in a Mashup where XAML alone is not sufficient.
You can attach the script to an M3 program, via Tools > Personalize > Scripts, via Tools > Personalize > Shortcut, or you can execute a stand-alone script.
Steps
First, create and deploy a simple Hello World Mashup (no values nor event handlers, to illustrate the point):
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <StackPanel> <Label Name="lblMessage" Content="Message" /> <TextBox Name="txtValue" /> <Button Name="btnOK" Content="OK" /> </StackPanel> </Grid>
Then, create a script that starts the Mashup:
var uri : String = "mashup:///?BaseUri=HelloWorld.mashup&RelativeUri=HelloWorld.xaml"; var task : Task = new Task(uri); var handler : RunnerStatusChangedEventHandler = Mashup_OnStatusChanged; DashboardTaskService.Current.LaunchTask(task, null, handler);
Then, get a reference to the Mashup:
function Mashup_OnStatusChanged(sender : Object, e : RunnerStatusChangedEventArgs) { if (e.NewStatus == RunnerStatus.Running) { // the Mashup is starting var runner : Runner = e.Runner; } else if (e.NewStatus == RunnerStatus.Closed) { // the Mashup is closing } }
Then, set values in the Mashup:
txtValue = runner.Host.HostContent.FindName("txtValue"); txtValue.Text = "Hello World!";
Then, attach event handlers:
var btnOK : Button = runner.Host.HostContent.FindName("btnOK"); btnOK.add_Click(Mashup_OnBtnOK);
Result
And here’s the result, the Mashup launched, the value “Hello World!” set in the TextBox, the Click event handler added to the button, and the value of the TextBox retrieved back, all that commanded & controlled from the Script, which would not have been all possible with XAML alone:
Source code
Here’s the complete source code:
import System; import System.Windows; import System.Windows.Controls; import Mango.Services; import Mango.UI.Core; import Mango.UI.Services; package MForms.JScript { class HelloWorldCC { var debug; var txtValue : TextBox; public function Init(element : Object, args : Object, controller : Object, debug : Object) { try { this.debug = debug; // launch the Mashup var uri : String = "mashup:///?BaseUri=HelloWorld.mashup&RelativeUri=HelloWorld.xaml"; var task : Task = new Task(uri); var handler : RunnerStatusChangedEventHandler = Mashup_OnStatusChanged; DashboardTaskService.Current.LaunchTask(task, null, handler); } catch (ex : Exception) { debug.WriteLine(ex); } } function Mashup_OnStatusChanged(sender : Object, e : RunnerStatusChangedEventArgs) { try { if (e.NewStatus == RunnerStatus.Running) { // the Mashup is starting debug.WriteLine("Mashup is starting"); var runner : Runner = e.Runner; // set values in the Mashup txtValue = runner.Host.HostContent.FindName("txtValue"); txtValue.Text = "Hello World!"; // attach an event handler to the Mashup var btnOK : Button = runner.Host.HostContent.FindName("btnOK"); btnOK.add_Click(Mashup_OnBtnOK); } else if (e.NewStatus == RunnerStatus.Closed) { // the Mashup is closing debug.WriteLine("Mashup is closing"); } } catch (ex : Exception) { debug.WriteLine(ex); } } function Mashup_OnBtnOK(sender : Object, e : RoutedEventArgs) { try { // get values from the Mashup MessageBox.Show(txtValue.Text); } catch (ex : Exception) { debug.WriteLine(ex); } } } }
Finally
Norpe’s code is more complete and you should implement its features in your script as well. For example norpe’s script checks if there is already an open instance of the Mashup, it can control the Mashup with keyboard shortcuts, it puts values back in the M3 panel, it closes the Mashup, it detaches event handlers, and it does cleanup.
That’s it!
UPDATE: Code to programmatically click on the button:
import System.Windows.Controls.Primitives;
btnOK.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
LikeLike
UPDATE2: Alternative code to programmatically click on the button:
import System.Windows.Automation.Provider;
import System.Windows.Automation.Peers;
var peer: ButtonAutomationPeer = new ButtonAutomationPeer(btnOK);
var invokeProv: IInvokeProvider = peer.GetPattern(PatternInterface.Invoke);
invokeProv.Invoke();
LikeLike
hi,
thanks for you effort.
i need to know, can i bind key like ‘F3’ to button action like ‘Search button’?. and how?
thanks…
LikeLike
Hi Mostafa, it’s probably possible but I don’t know how to do it in XAML. You can use the checkbox IsDefault to trigger the click event when you press the ENTER key. Otherwise in script it’s probably easier to do than XAML.
LikeLike
Thanks for your replay.
i already call the mashup from JScript. but i don’t know how to to it. can you please tell me?. in another word, can i add event listener on a key press like “F3” from mashup?
LikeLike
Mostafa, sorry, I don’t know.
LikeLike