Silverlight 4 added the static Clipboard class with the clearly-named SetText, GetText and ContainsText methods.
If running in-browser or out-of-browser without elevated trust the Clipboard methods can only be called as a result of user actions (such as Click, KeyDown). The first time a method is called, a security prompt is presented to the user asking them to grant the app access to the clipboard.
If the user selects 'No' a SecurityException is thrown.
If the app is running out-of-browser with elevated trust, the above dialog is not shown and the Clipboard methods can be called even if not in response to user initiated events such as in your page constructor, in a DispatcherTimer.Tick event, etc.
Example Code
<Button x:Name="btnCopy" Click="btnCopy_Click">Copy</Button>
<Button x:Name="btnPaste" Click="btnPaste_Click">Paste</Button>
<TextBox x:Name="txtBox"></TextBox>
private void btnCopy_Click(object sender, RoutedEventArgs e)
{
// Assumes we want to prevent the user from copy 'nothing' to the clipboard.
// Without this check, whatever text is currently in the clipboard will be replaced
// with nothing.
if (!string.IsNullOrEmpty(txtBox.Text))
{
try
{
Clipboard.SetText(txtBox.Text);
}
catch (SecurityException ex)
{
// If the user does not grant access for the app to access the clipboard
// a SecurityException is thrown
MessageBox.Show("Clipboard access is required to use this feature.");
}
}
}
private void btnPaste_Click(object sender, RoutedEventArgs e)
{
try
{
txtBox.Text = Clipboard.GetText();
}
catch (SecurityException ex)
{
// If the user does not grant access for the app to access the clipboard
// a SecurityException is thrown
MessageBox.Show("Clipboard access is required to use this feature.");
}
}
SHARE:
For a bit of Silverlight 3 fun:
Use\install at: http://www.dontcodetired.com/live/christmascountdownbeta/
There are some things which need improving such as resizing when run out of browser, abilty to sort/hide/filter data etc.
SHARE:
One of the new SL3 features is the ability to test if there is a network connection currently active. This complements the out of browser experience by letting an app use local (isolated) storage when off-line, and when a network connection becomes available, upload data to a central server.
To be informed when the network changes:
System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged += (sender, e) => txtNetStatus.Text = "Network available: " + System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable().ToString();
This example uses a lambda expression as a shortcut (rather than having a separate method); when the network status changes, we set the text of txtNetStatus to 'Network available: True' or 'Network available: False' depedning on the (boolean) result of GetIsNetworkAvailable()
.
SHARE:
If a user attempts to install a SLOOB app when it is already installed an InvalidOperationException
is thrown with the message "Application is already installed.". We can wrap in a try..catch to handle this:
try
{
Application.Current.Install();
}
catch (InvalidOperationException ex)
{
MessageBox.Show(ex.Message);
}
Or better yet, remove the install button from the UI if the application is already installed:
if (Application.Current.InstallState == InstallState.Installed)
btnInstall.Visibility = Visibility.Collapsed;
else
btnInstall.Visibility = Visibility.Visible;
SHARE:
When running a Silverlight 3 application out of browser (OOB) {or SLOOB=Silverlight Out Of Browser} you can enable the automatic download\install of updated versions.
This is not an automatic feature and requires some (simple) coding on the developers part.
In the App.xaml.cs add a callback for the CheckAndDownloadUpdateCompleted event (the example below uses a lambda but you could use a separate method with the signature void App_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
.
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
// Add callback to be executed when the check (and possible download) has been performed
this.CheckAndDownloadUpdateCompleted += (sender, e) =>
{
if (e.UpdateAvailable)
MessageBox.Show("Update downloaded, plese restart to take effect.");
};
InitializeComponent();
}
Next add a call to check for updates, this can be placed in the Application Startup event handler or you could have a "Check for updates" button in th U.
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
this.CheckAndDownloadUpdateAsync();
}
Now, every time the app start an update check will be performed, and if there is an updated version available it will be downloaded asynchronously. Once download the user will get the message box advising them to restart the app to start using the new version. The download\update just happens if available and there is currently no mechanism to allow the user to opt-out of an update.
SHARE: