Rectangle 27 2

To communicate between a Silverlight control and javascript in an ASP.net page you can use the HTML bridge. Essentially this allows you call specially marked methods in your Silverlight control as well call javascript routines from Silverlight.

Marking a Silverlight method or attribute as accessible to javascript:

public partial class Page : UserControl
    {

    void Page_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        // Register this object in the page as Scriptable
        // so it can be accessed from within JavaScript
       HtmlPage.RegisterScriptableObject("Page", page);
    }

    [ScriptableMember]
    public string HelloFromSilverlight()
    {
        return "Hello";
    }

        [ScriptableMemberAttribute]
        public int SomeValue
        {
            get 
            {   return _someValue;}
        }

}

Calling the Silverlight method from javascript:

// Get a reference to the actual Silverlight
        // plugin element within the page
        var plugin = pluginObject.get_element();

        // Call the HelloFromSilverlight method of our Silverlight object
        var text = plugin.Content.Page.HelloFromSilverlight();

How to redirect to silverlight page from asp.net - Stack Overflow

asp.net silverlight
Rectangle 27 28

For anybody developing in C# on Silverlight here's a pretty neat trick that I've just discovered that allows evaluation of an expression by calling out to the Javascript engine:

double result = (double) HtmlPage.Window.Eval("15 + 35");

I wonder if you could reference this elsewhere. Probably not, but it would be cool.

As this evaluates arbitrary Javascript code, you probably want to be sure to sanitize your input and make sure you're not directly displaying the result. (I would think this would be a good way to introduce XSS without realizing it)

Try entering numbers with a leading zero, the result isn't reliable. "054 + 6" gives you 50 for example.

@djerry, that is because numbers with a leading zero are considered octal by JS's evaluator, and octal 054 equals decimal 44.

c# - Is there a string math evaluator in .NET? - Stack Overflow

c# .net
Rectangle 27 27

For anybody developing in C# on Silverlight here's a pretty neat trick that I've just discovered that allows evaluation of an expression by calling out to the Javascript engine:

double result = (double) HtmlPage.Window.Eval("15 + 35");

I wonder if you could reference this elsewhere. Probably not, but it would be cool.

As this evaluates arbitrary Javascript code, you probably want to be sure to sanitize your input and make sure you're not directly displaying the result. (I would think this would be a good way to introduce XSS without realizing it)

Try entering numbers with a leading zero, the result isn't reliable. "054 + 6" gives you 50 for example.

@djerry, that is because numbers with a leading zero are considered octal by JS's evaluator, and octal 054 equals decimal 44.

c# - Is there a string math evaluator in .NET? - Stack Overflow

c# .net
Rectangle 27 1

Silverlight is a client-side technology. You are allowed though to call Javascript functions. Having that, you can take advantage of the ASP.NET AJAX to make calls to web methods on the host ASPX page. Even better, you can force a full post-back of the ASPX page from Javascript.

(sorry for the late response. i've just come accross this when looking for something else)

c# - Server-Side eventing between Silverlight and the host .aspx page ...

c# silverlight events user-controls
Rectangle 27 11

On the page hosting the silverlight control, you could setup a javascript timer and do an ajax call to an Http Handler (.ashx) every 5 minutes to keep the session alive. Be sure to have your Handler class implement IRequiresSessionState.

I recommend the Handler because it is easier to control the response text that is returned, and it is more lightweight then an aspx page.

You will also need to set the response cache properly to make sure that the browser makes the ajax call each time.

Here is the sample code for an HttpHandler

public class Ping : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.ContentType = "text/plain";
        context.Response.Write("OK");
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

Then if you use jQuery, you can put this on your host aspx page

setInterval(ping, 5000);

function ping() {
    $.get('/Ping.ashx');
}

The interval is in milliseconds, so my sample will ping every 5 seconds, you probably want that to be a larger number. Fiddler is a great tool for debugging ajax calls, if you don't use it, start.

It is more lightweight than a nearly-blank aspx page? I'm kind of a newbie to ASP.NET/AJAX and this seems like it's more complicated. Can you provide a sample of the ashx file I would need to use?

I've tried this, and the javascript doesn't seem to work in IE8. I just downloaded the latest version of jQuery for this purpose. Is there something else that the handler/page needs?

There isn't anything else you should need. There is a great tool called Fiddler2 you can use to watch network traffic and make sure that the call is being made. It could be a path problem, in which case fiddler would report a 404. I would recommend using that tool to see what is happening. Otherwise, IE8 has good javascript debugging tools that might also get you closer.

if you are calling a handler, yes, you still need it. If you are making a call to a controller action, then you would not need it.

c# - Preventing an ASP.NET Session Timeout when using Silverlight - St...

c# asp.net silverlight authentication session
Rectangle 27 3

If you have anything in particular you want to hide (like a proprietary algorithm), put that on the server, or put it in a Flash movie and call it with JavaScript. Writing ActionScript is very similar to writing JavaScript, and you can communicate between JavaScript and ActionScript. You can do the same with Silverlight, but Silverlight doesn't have the penetration Flash does.

However, remember that any mobile phones can run your JavaScript, but not Silverlight or Flash, so you're crippling your mobile users if you go with Flash or Silverlight.

encryption - How can I hide or encrypt JavaScript code? - Stack Overfl...

javascript encryption obfuscation
Rectangle 27 1

Or you can call a managed C# (or whatever) language function in a Silverlight control by calling through the JavaScript bridge.

This is one of those tricky questions where the answer is "not directly, but with a little help from Ajax or the web page DOM I can, and this is how I do it...".

Can you call a C# function from JavaScript? - Stack Overflow

c# javascript asp.net
Rectangle 27 1

Yes, sorry! I updated the question. It's indeed the facebook 'I like' button.

Should be a comment.

Compiler: Please do not include clarification requests on answers in the future. Use the comments. They exist for a reason.

How do I add the like facebook button to a silverlight application? - ...

silverlight silverlight-4.0 silverlight-toolkit
Rectangle 27 1

I think you have one of two options. In your view model for that silverlight control, during the initialization, Bind the navigate URI for a hyperlink button to the desired URI you want to navigate to. Option 2 (a lot smoother): On the click method, Invoke a javascript method on the page that hosts the silverlight object. That method would then do some sort of smooth jquery transition or just a simple navigation for you. Option 1: <HyperlinkButton NavigateUri="{Binding DesiredURL}" TargetName="_blank" />

public void OnFancyNavigate(string _destination)
    {
        //call the browser method/jquery method (I used constants to centralize the names of the respective browser methods
        try
        {
            HtmlWindow window = HtmlPage.Window;
            window.Invoke(Constants.TBrowserMethods.BM_FANCYNAVIGATE, new object[] { _destination});
        }
        catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
    }

Lastly, define the javascript method in the aspx/html/.js file that hosts the xap content:

function fancyNavigate(_destination) {
//some fancy jquery or just the traditional document.location change here

C# will locate the javascript method when invoked from your code, and you should be good to go

asp.net - Silverlight page navigation - Stack Overflow

asp.net silverlight
Rectangle 27 1

Let's just see a bit about the history of MVVM: MVVM was originally defined by Microsoft for use with Windows Presentation Foundation (WPF) and Silverlight, having been officially announced in 2005 by John Grossman in a blog post about Avalon (the code-name for WPF). It also found some popularity in the Adobe Flex community as an alternative to simply using MVC. In recent years, MVVM has been implemented in JavaScript in the form of structural frameworks such as KnockoutJS, Kendo MVVM and Knockback.js, with an overall positive response from the community.

Meanwhile, You are right, AngularJS was closer to MVC (or rather one of its client-side variants), but over time by many refactorings and API improvements, it's now closer to MVVM the $scope object could be considered the ViewModel that is being decorated by a function that we call a Controller.

The software behaviors that are common to MVC, MVP, and MVVM are:

1)Data Layer / Business Logic (Model): This is the behavior which applies the business logic to the application's data

2) Presentation Layer / UI ( View ): View is responsible for the visual presentation of the application.

3) Application Logic ( Controller, Presentation or View Model ): This behavior holds the logic that implements the interaction between the model and the view.

MVVM provides a clear separation between the UI and application logic.

Server-side library: Silverlight ,Windows Phone apps,Adobe Flex or Tanuki which is an MVVM-inspired web framework that fancies idiomatic Ruby, DRY and extensibility by its design, or another example would be The WebCore 3 PHP framework which is a platform-independent framework that uses the MVVM pattern.

So I would say this is not correct to say that a specific pattern is just for Client or Server side, It's totally related to the framework that we are using or probably environment that we are going to establish for our project whether in Front or in Back-end development.

angularjs - Why is Angular called MV* framework - Stack Overflow

angularjs design-patterns model-view-controller mvvm
Rectangle 27 1

Let's just see a bit about the history of MVVM: MVVM was originally defined by Microsoft for use with Windows Presentation Foundation (WPF) and Silverlight, having been officially announced in 2005 by John Grossman in a blog post about Avalon (the code-name for WPF). It also found some popularity in the Adobe Flex community as an alternative to simply using MVC. In recent years, MVVM has been implemented in JavaScript in the form of structural frameworks such as KnockoutJS, Kendo MVVM and Knockback.js, with an overall positive response from the community.

Meanwhile, You are right, AngularJS was closer to MVC (or rather one of its client-side variants), but over time by many refactorings and API improvements, it's now closer to MVVM the $scope object could be considered the ViewModel that is being decorated by a function that we call a Controller.

The software behaviors that are common to MVC, MVP, and MVVM are:

1)Data Layer / Business Logic (Model): This is the behavior which applies the business logic to the application's data

2) Presentation Layer / UI ( View ): View is responsible for the visual presentation of the application.

3) Application Logic ( Controller, Presentation or View Model ): This behavior holds the logic that implements the interaction between the model and the view.

MVVM provides a clear separation between the UI and application logic.

Server-side library: Silverlight ,Windows Phone apps,Adobe Flex or Tanuki which is an MVVM-inspired web framework that fancies idiomatic Ruby, DRY and extensibility by its design, or another example would be The WebCore 3 PHP framework which is a platform-independent framework that uses the MVVM pattern.

So I would say this is not correct to say that a specific pattern is just for Client or Server side, It's totally related to the framework that we are using or probably environment that we are going to establish for our project whether in Front or in Back-end development.

angularjs - Why is Angular called MV* framework - Stack Overflow

angularjs design-patterns model-view-controller mvvm
Rectangle 27 0

Aha! I figured it out. Our app uses an iframe, so the rendered html looks something like this

<html>
<head></head>
<body>
Stuff
<iframe>
    <html>
        <head></head>
        <body>Other Stuff</body>
    </html>
</iframe>
<body>
</html>

And the Silverlight control in question is in the iframe. The problem was that the file that contained the showPopup function was referenced in the outer <head> (why I could call the function with the IE toolbar) but not the inner <head>. Adding a reference to the file in the in-the-iframe <head> solved the problem.

Sort of anticlimactic, but thanks for all the help.

Calling A Javascript function from Silverlight - Stack Overflow

javascript silverlight
Rectangle 27 0

Actually referencing the script again from the iframe is not the most efficient way to reference code contained in the parent. If your function is called "showPopup", you can insert this in your iframe:

<script type="text/javascript">
    var showPopup = parent.showPopup;
</script>

And voil. The explanation for this is that all "global" functions and objects are part of this "global namespace"... which is the "window" object. So if you're trying to access "global" functions from a child, you need to either call the function on the parent (e.g parent.showPopup('....')) or declare a local alias for it (which is what we do in the above example).

Calling A Javascript function from Silverlight - Stack Overflow

javascript silverlight
Rectangle 27 0

I had the same problem in VS 2010 with SL 4. I had created a few methods and put them into one single JS file. However this file had not been added to the head section of the ASPX file. Adding it solved the problem. The difference is that though I did not have a separate head section in the iframe, I had the problem and it got solved.

Calling A Javascript function from Silverlight - Stack Overflow

javascript silverlight
Rectangle 27 0

I believe in Silverlight 2 this may have been permitted, but ever since it has not. This was a breaking behavior change, requiring the action to be "user initiated" through a user action -in- Silverlight: something like a key press or mouse click on a button with a click event handler.

There isn't a way to pop up and open the file dialog any other way.

Thanks for the clarification Jeff, although I am a bit disappointed :)

Though disappointing, the situation through JS could be as bad as popup windows: imagine the annoyance of a site that for some reason or another pops up the open file dialog when you browse to it. It blocks the browser and waits for something to happen - even worse if it happens in a loop.

Call OpenFileDialog Silverlight from JavaScript - Stack Overflow

javascript silverlight file-upload
Rectangle 27 0

Or you can call a managed C# (or whatever) language function in a Silverlight control by calling through the javascript bridge.

This is one of those tricky questions where the answer is "not directly, but with a little help from Ajax or the web page DOM i can, and this is how i do it....".

Can you call C# function from javascript? - Stack Overflow

c# javascript asp.net
Rectangle 27 0

Silverlight virtually requires that everything be done asynchronously. Any long running synchronous process will hang the browser if run on the UI thread. If you never reach the 'checkResponse' line of code it is possible that an unhandled exception was thrown on the previous line, and it is being swallowed. You can check in the dev tools of your browser to see if there are any javascript errors. I am surprised that calling XMLHttpRequest synchronously works at all since I would expect it to lock up the UI thread. But, the solution depends on your definition of async.

  • calling the sync XHR request on a background thread and then marshalling to the UI thread (eg with Dispatcher.BeginInvoke) when you are ready
  • setting up an XMLHttpRequest wrapper that makes the call in async mode and raises a callback in Silverlight on completion

While these options are async, they don't require your server code to be written any differently (it can stay synchronous). I have seen a web server process a call for over an hour before finally returning a response, at which time the Silverlight app raised a callback. You could even use tools like the TPL await/async, or co-routines available in many mvvm frameworks to make the code appear very procedural/synchronous while still performing its actions asynchronously.

internet explorer 8 - IE8 freeze caused by long synchronous xmlhttpreq...

silverlight internet-explorer-8 xmlhttprequest freeze synchronous
Rectangle 27 0

The answer to your question is: no. By design and for very good reasons in-browser code (be that inside Silverlight Application) and in Javascript you can't arbitarily launch any automation server. Could you imagine the havoc that would be wrought if just visiting a website could cause the launch and manipulation of all sort of apps on your computer?

How to create ActiveXObject in javascript function which is called usi...

javascript silverlight activexobject
Rectangle 27 0

I'm not familiar with Silverlight, but if theid has value "Salut!" inside of SayHello, then you cannot eval it, since it is a string of text, not code. You should change the line alert(eval(theid)); to just alert(theid);.

I have tried this but it doesn't work. Undefined is what I get.

Call JavaScript function from Silverlight 4.0 application - Stack Over...

javascript silverlight-4.0
Rectangle 27 0

Just a quick note on why HtmlPage.Window.Invoke("location.reload(true);"); doesn't work: it appears from my quick testing that the way Silverlight implements Invoke is to look up a property with the given name on the specified JavaScript object and call it. So this code would say, "Find a property on the window object named 'location.reload(true);' and call it with zero arguments." There is, instead, a GetProperty method that will let you get the window's location property and invoke reload on that with the parameter true. The final code looks like this:

((ScriptObject)HtmlPage.Window.GetProperty("location")).Invoke("reload", true);

Can Silverlight initiate Page Refreshes? - Stack Overflow

silverlight-2.0