My Silverlight series (3) -- Silverlight clipboard operation

Source: Internet
Author: User

For security reasons, the Silverlight ApplicationProgramYou cannot access too many local resources. For example, it only has openfiledialog instead of savefiledialog. In addition, Silverlight cannot directly operate the Windows clipboard.

 

When designing the form program, clipboard resources are often used. Under. net, you only need to operate the static methods in the system. Windows. Clipboard class to meet our needs. However, as a subset of WPF, Silverlight Assembly does not have the clipboard class. However, if we want to access the clipboard resources in Silverlight, we need to find another way. Because Silverlight has a mechanism for interacting with Asp.net and HTML Dom, we can use this method to access the clipboard.

 

 

< Script Type = "Text/JavaScript" >
Function Copytoclipboard () {
If (Arguments. Length >   0 ) {
Window. clipboardData. setdata ("ABCD", Arguments [0]);
}
}
</ Script >

In the HTML and aspx files (the carrier that calls the Silverlight program xap), add this section of JSCodeAnd then rewrite the app. XAML. CS file as follows:

 

Public APP ()
{
This . Startup + =   This . Application_startup;
This . Exit + =   This . Application_exit;
This . Unhandledexception + =   This . Application_unhandledexception;

Initializecomponent ();
}

Private   Void Application_startup ( Object Sender, startupeventargs E)
{
Page P= NewPage ();
System. Windows. browser. htmlpage. registerscriptableobject ("Silverlightapplication", P );
This. Rootvisual=P;
}

 

At the same time, add the corresponding code to the page. XAML. CS file:

 

Private   Void Copytoclipboard ()
{
Htmlpage. Window. eval ( " Copytoclipboard () " );
Object [] ARGs =   {"Abcdefg");
Htmlpage. Window. Invoke ("Copytoclipboard", ArgS );
}
[Scriptablemember ()]
Public   String Interinvole ()
{
Return "You have called the Silverlight method from the JS script.";
}

 

In this way, the abcdefg string is copied to the clipboard whenever the copytoclipboard () method in page. XAML. CS is called.

 

PS: in the above section [scriptablemember ()], I have never called it, but Silverlight only requires interfaces that are provided to JS call in interaction with Js, if not written, the compilation will fail.

PS: when running in Vista, the system will prompt whether to agree to access the clipboard. This is a problem with Vista, not the error of this Code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.