I'm having problems with Script#. I got a ASP.NET page which loads a JavaScript file and uses a object from there.
var tracker = Piwik.getTracker(...);
Now I also have a Script# generated JavaScript file which is also included in that same page. Now I want to use the tracker
object in that Script# .cs file.
Type.InvokeMethod(tracker, "trackPageView");
Doesn't compile: does not exist in the current context
Type.InvokeMethod("tracker", "trackPageView");
Compiles but outputs: 'tracker'.trackPageView();
Any ideas?
When trying to emit code for a 3rd party javascript library, using Script.Literal will work but usually should be avoided. The whole point of using Script# is so you get compiler support, and easy refactoring.
I would recommend creating some new classes in your Script# library or even a separate Script# Import library if you plan to use Piwik in other projects.
The key is to mark these classes with the attribute [Imported] - This attribute will prevent the creation of a js file for the classes but will give you all the strong typing and refactoring goodness that makes script# so wonderful.
I'm not sure what "Piwik" is but I'll assume it's a Singleton object in the javascript library. Create the following two classes.
[Imported]//we don't want this to show up in the javascript source.
public sealed class Tracker
{
public void TrackElement()//this will write trackElement() in emitted js.
{
}
}
[Imported]//we don't want this to show up in the javascript source.
public static class Piwik
{
public static Tracker GetTracker(string item)//this becomes getTracker()
{
return null;
}
}
You'll now be able to write code like this in Script#
Tracker tracker = Piwik.GetTracker("whatever");//c#
It will emit javascript that looks like this.
var tracker = Piwik.getTracker('whatever');//javascript
已经找到它: Script.Literal("tracker.trackPageView()");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.