.NET Framework adapter
This article pertains to setting up and using the OpenFin adapter for .NET Framework.
Setup
- Install latest version of the OpenFin.WPF or OpenFin.WinForm (depending on your use-case).
- These packages enable you to create your
runtimeOptions
object which indicates which version of the OpenFin Runtime to use, along with some other optional parameters. Here’s an example
var runtimeOptions = new Openfin.Desktop.RuntimeOptions
{
Version = "19.89.58.32",
EnableRemoteDevTools = true,
RemoteDevToolsPort = 9090
};
Refer to Semantic versioning for more information.
Usage
Inter Application Bus
Firstly we need to get an instance of our OpenFin runtime. Create an instance using the runtimeOptions
instance we created in the setup.
InterApplicationBus.Publish(runtime, "ATopic", new { name = "WPF Demo", text = "Message"});
To subscribe to messages from other applications you can do the following. Remember to unhook your MessageReceived
event handler in your production app.
var subscription = InterApplicationBus.Subscription<MyPayload>(this.runtime, "ATopic");
subscription.MessageReceived += (object sender, MessageBusMessageEventArgs<MyPayload> payload) =>
{
Console.WriteLine($"Message received {payload.Message.YourData}.");
};
Channel API
OpenFin’s Channel API allows you finer grained control over your messaging. With it we can create channels, and clients of those channels. Each channel can “register” a list of commands/actions that the channel provider will handle.
Firstly we need to get an instance of our OpenFin runtime. Create an instance using the ‘runtimeOptions‘ instance we created in the setup.
var runtime = Runtime.GetRuntimeInstance(runtimeOptions);
Now create a channel called “MyAppActions”.
var channel = runtime.InterApplicationBus.Channel.CreateProvider("MyAppActions");
channel.RegisterTopic<string>("SayHello", (payload) => { return $"Hello {payload}!"};);
await channel.OpenAsync();
Now we can create a client of the channel we created above.
client = this.runtime.InterApplicationBus.Channel.CreateClient("MyAppActions");
await client.ConnectAsync();
Console.WriteLine("Client channel connected");
Finally we can send messages between the two, like so;
var result = await client.DispatchAsync<string>("SayHello", "Your name goes here");
Console.WriteLine($"Responded with \"{result}\".");
This results in a message being returned which writes “Responded with “Hello Your name goes here.” to the console.
Embedded View - WPF
Open the WPF xaml file that you wish to embed a web view into.
Add the following namespace to the top of the file.
xmlns:OpenFin="clr-namespace:Openfin.WPF;assembly=Openfin.WPF"
Now add the OpenFin EmbeddView component to the view in the desired location.
<OpenFin:EmbeddedView x:Name="OpenFinEmbeddedView" />
Next, open the code-behind file for the same file that we added the “OpenFinEmbeddedView” control to above.
In the constructor of your window/control, ensure you’ve initialized your RuntimeOptions
as shown in the setup above.
Create an “ApplicationOptions” object, change the URI of the options to point to any site you want to connect to.
var appOptions = new Openfin.Desktop.ApplicationOptions ("of-chart", "of-chart-uuid", "http://cdn.openfin.co/embed-web/chart.html");
Initialize the instance of your “OpenFinEmbeddedView” (make sure you change the name if you used something different when you named the object in your xaml).
OpenFinEmbeddedView.Initialize (runtimeOptions, appOptions);
Finally, and optionally, listen to the ready
event on the “OpenFinEmbeddedView” to know when the EmbeddView is initialized and ready to go. Ensure that any changes to the view state is done with the UI Dispatcher.
OpenFinEmbeddedView.Ready += (sender, e) =>
{
//Any Interactions with the UI must be done in the right thread.
Utils.InvokeOnUiThreadIfRequired(this, () =>
textBlockReady.Text = "OpenFinEmbeddedView is ready");
}
Embedded View - WinForm
Include the OpenFin.WinForm
namespace in the Form that will contain the EmbeddedView.
using System;
using System.Windows.Forms;
using Openfin.WinForm;
namespace WinForms.Test
{
public partial class Form1 : Form
{
...
Then place an EmbeddedView control in the screen.
var OpenFinEmbeddedView = new EmbeddedView();
Controls.Add(OpenFinEmbeddedView);
Create an “ApplicationOptions” object, change the URI of the options to point to any site you want to connect to.
var appOptions = new Openfin.Desktop.ApplicationOptions("of-chart", "of-chart-uuid", "http://cdn.openfin.co/embed-web/chart.html");
Initialize the instance of your “OpenFinEmbeddedView” (make sure you change the name if you used something different when you named the object in your xaml).
OpenFinEmbeddedView.Initialize(runtimeOptions, appOptions);
Finally, and optionally, listen to the ready
event on the “OpenFinEmbeddedView” to know when the EmbeddView is initialized and ready to go. Ensure that any changes to the view state is done with the UI Dispatcher.
OpenFinEmbeddedView.Ready += (sender, e) =>
{
//Any Interactions with the UI must be done in the right thread.
Utils.InvokeOnUiThreadIfRequired(this, () =>
textBlockReady.Text = "OpenFinEmbeddedView is ready");
}
Notifications
Notifications requires the addition of an extra OpenFin.Notifications nuget package.
With Notifications you have the option of listening to the following events: NotificationCreated
, NotificationClosed
and NotificationActionOccurred
NotificationClient.NotificationClosed += NotificationClient_NotificationClosed;
NotificationClient.NotificationCreated += NotificationClient_NotificationCreated;
NotificationClient.NotificationActionOccurred += NotificationClient_NotificationActionOccurred;
Once you have wired up your listeners you can initialize the notification client so that you know when it is initialized and can check whether it is connected to the OpenFin Notification Center.
NotificationClient.InitializeAsync().ContinueWith(_ =>
{
NotificationClient.GetProviderStatusAsync().ContinueWith(status => {
// status.Connected will tell you if you are connected
// status.Version will tell you which version you are connected to
// react as appropriate.
});
});
To create a notification you will create a NotificationsObject
and pass it to the notification client:
var buttons = new List<ButtonOptions>()
{
new ButtonOptions
{
Title = "Button label",
IconUrl = "https://openfin.co/favicon-32x32.png",
OnNotificationButtonClick = new Dictionary<string, object>
{
{ "btn", "button1Clicked" }
},
IsCallToActionButton = true
}
}.ToArray();
await NotificationClient.CreateNotificationAsync("UniqueNotificationId", new NotificationOptions
{
Title = "Notification Title",
Body = "Text or Markdown Text",
Category = "Category",
Icon = "https://openfin.co/favicon-32x32.png",
OnNotificationSelect = new Dictionary<string, object>
{
{"task", "selected" }
},
Buttons = buttons,
NotificationIndicator = new NotificationIndicator {
IndicatorText = "Please action",
IndicatorType = IndicatorType.Failure
},
IsStickyNotification = true
});
To see the different types of notifications we recommend using our Notification Studio Tool, which will let you easily experiment with the settings available.
License
Please include your licenseKey in RuntimeOptions.LicenseKey
when launching apps from the .NET Adapter. If you’re interested in an Enterprise license, please contact us for one.
Updated 4 months ago