Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 4 Clipboard Security Exception "access is not allowed"?

I'm new in Silverlight and i am doing some tests. With my current test I try to display in real time the current Clipboard content. But there is a weird behaviors with this code :

namespace SilverlightTest
{
public partial class MainPage : UserControl
{
    private Timer _timer;

    public MainPage()
    {
        InitializeComponent();
        var dispatcher_timer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 0, 5)};
        dispatcher_timer.Tick += new EventHandler(timer_Callback);
        dispatcher_timer.Start();
    }

    private void timer_Callback(object state, EventArgs eventArgs)
    {
        current_clip_board.Content = Clipboard.GetText();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        current_clip_board.Content = Clipboard.GetText();
    }
}
}

The button Event and the timer Event are suppose to do exactly the same action. But it doesn't ! The Button works fine and set the clipboard text into the label but the timer throw an exception :

Clipboard access is not allowed

The question is : why ? :)

Thanks.

PS : I would bet on a thread problem :p

like image 264
Nicolas Guillaume Avatar asked Mar 26 '10 18:03

Nicolas Guillaume


1 Answers

Clipboard access, in a partial trust (in-browser) Silverlight application (the scenario you're likely referring to above), is restricted. The GetText property is accessible only in scenarios that the Silverlight runtime determines were initiated by the user. Your example is perfect -- by a button click for example. A dispatch timer however is not user initiated, so the property throws an exception (this is especially important within the context of a in-browser application, which could be a big security hole if you could create a Silverlight application that just ran silently in the browser, watching the user's clipboard updates without their knowledge).

See this clipboard documentation for more details.

like image 98
WiredPrairie Avatar answered Sep 26 '22 20:09

WiredPrairie