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.