Window[] function in javascript Dev Express - c#

I'm writing a Dashboard Application in C#, using DevExpress components. I am trying to dynamically create a circular gauge, and update the gauges value using a callback function. In the DevExpress sample here:
http://demos.devexpress.com/ASPxGaugesDemos/Gauges/CircularGauge.aspx
They show how to update a gauge that has already been added to the form at design time. One of the calls in their sample has var gague = window[gaugeName]
When I make the call to this function the value comes back undefiend:
function PerformCallbackCore(gaugeName) {
var gauge = window[gaugeName];
m_isDirty = gauge.InCallback();
if (!m_isDirty)
gauge.PerformCallback();
}
Does anyone possibly know why I cannot retrieve the gauge value?
Thanks - Larry

It is due to bad code in the gauge demo. Yes, I use (and pay for) DX; I can say that.
The code uses setTimeout with a string (enough ick here!) and passes in the "name" of window property assigned to the control with ClientInstanceName (the code won't work if the name used and this don't match up). The demo code is further confusing/ugly by having a different "proxy" function for Gauge1 ... GaugeN.
Now, I suspect that the ClientInstanceName wasn't used or the correct name wasn't passed somewhere, so window[aPropertyName] evaluates to undefined, however ..
.. a better way to write it would be to use/pass the object directly instead of relying on the artificial "client name" / window property. See
ASPxClientGaugeControl (and follow links to the appropriate handler) and consider this:
<ClientSideEvents EndCallback="onEndGaugeCallback" />
With a single unified callback (and distinct lack of "client names" and useless proxy methods):
function onEndGaugeCallback (s, e) {
// "s" is for Sender; it evaluates to the appropriate [Gauge] control
// or rename it to "gauge" or whatever :-)
}
Happy coding!

Related

How to implement MObject picker in Maya

I have a plugin running inside Maya that needs to perform an 'eye dropper' test against objects in the scene. My Plugin is running as a hosted WPF control, so I have a C# button event callback that wants to operate in a modal fashion until the hit is performed or escape is pressed. This was really easy to do in 3D Studio Max, but I cannot find out how to do this in Maya.
Any advice?
I do miss that in 3dsMax, but as far as I know, no, there's no built-in functionality to do that.
Most tools in Maya work already having a selection before execution so then the tool can use cmds.ls(sl=True) to capture the selection and preform your validation.
What you can do is mimic an object picker by using a selection callback. There's cmds.scriptJob, but it's more efficient to use OpenMaya's callbacks. Here's an example that uses a class to store the callback's id and auto-manages it:
import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
class ObjectPicker():
_id = None # Store callback's id here.
def __init__(self):
# When this class is created, add the callback.
OpenMaya.MGlobal.displayWarning("Please pick an object")
ObjectPicker.add_callback()
#staticmethod
def on_selection_changed(*args):
# This gets triggered from the callback when the user changes the selection.
# Auto-remove the callaback afterwards.
print "Selection:", cmds.ls(sl=True)
ObjectPicker.remove_callback()
#staticmethod
def add_callback():
# First remove any existing callback, then store the id in this class.
ObjectPicker.remove_callback()
ObjectPicker._id = OpenMaya.MEventMessage.addEventCallback("SelectionChanged", ObjectPicker.on_selection_changed)
#staticmethod
def remove_callback():
# Remove the callback so it stops triggering the function.
if ObjectPicker._id is not None:
OpenMaya.MEventMessage.removeCallback(ObjectPicker._id)
ObjectPicker._id = None
# After calling this, pick a new object then it will print it in the Script Editor.
picker = ObjectPicker()
After creating an new instance of the class with picker = ObjectPicker(), a warning will pop-up to the user to pick an object. After the selection changes, it triggers the callback which prints the selection to the Script Editor then removes its own callback.
I think this might work, but Maya isn't 3dsMax, and at the end of the day it might be best to not force one software to work like another. So I'd consider to just stick with what everyone is already used to, and that is to work with the user's current selection.
Edit:
Sorry just noticed c++ tag, but the same concept should apply.
Edit #2:
I just learned about the command cmds.scriptCtx, so a picker does exist! I think it's an older command as it seems to only support MEL and the implementation doesn't feel that great. If you want to learn more then check out my answer to another question.

Log whether any function is entered

I have a WinForms app and I want to keep track of what buttons are clicked on. I figure I can put something like a Console.WriteLine("FUNCTION_NAME"); at the beginning of every function (of course, with FUNCTION_NAME replaced with the actual function's name). My question is: is there a better way? Some sort of programmatic way to grab the function name and do something with it when the function is run?
Usually, in WPF / MVVM, we'll have a base class for the logger that will use the CallerMemberNameAttribute , so you don't need to figure out the caller.
Instead of writing it out, you can use a logger (NLog, Log4net, etc), if you create the logger in the class, it will do part of the work automatically as well.
Here's a snippet. Note that this is done for automatic change notifications, but you can use the same for your purposes:
[NotifyPropertyChangedInvocator]
protected override void RaisePropertyChanged([CallerMemberName]string property = "")
{
base.RaisePropertyChanged(property);
}
just change the name of the functions obviously, and do what you need in it.
There is a way. Use CallerMemberNameAttribute. Example:
https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx#Anchor_7
EDIT:
Here is an example of AOP in C#:
https://msdn.microsoft.com/en-us/magazine/dn574804.aspx
But the performance will be severely degraded.

Can't determine if a certain UITestControl exists in my web app

I'm currently trying to help automate some coded UI tests using C# for a web application. A frequent problem I'm encountering is that it can be extremely difficult to determine if a UITestControl object exists on the page or not. Unfortunately, Microsoft's documentation on their MSDN website for anything regarding coded UI tests is practically non-existant (see here their page for UITestControl).
Basically what I'm asking is:
What is the best way to determine if a UITestControl exists on the page or not?
How does the UITestControl.Exists property work?
What does the UITestControl.Find() method do?
How does the UITestControl.TryFind() method work?
How I've tried to handle it:
As I mentioned earlier, the documentation on all of these classes and methods is mostly blank. The most you can get to describe any of the methods and properties is a 1 line description in Intellisense, so I've been experimenting with the methods that are listed.
First I tried checking if the UITestControl.Exists property was true, but over time and consulting others' experience with it, it became apparent that it always returns true, even if the browser isn't open. Since the option that seemed most obvious wasn't working, I tried using the UITestControl.Find() method, but since it takes no arguments and returns nothing I couldn't figure out what it did. I tried using the UITestControl.TryFind() method, and occasionally it worked, but I found that it only seemed to return false when I wasn't on the correct page; it always returned true otherwise. Clearly I had no idea how it worked, and shouldn't use it as a test.
I figured if I couldn't get the provided methods to do their job, I'd have to try to make my own tools. I most recently tried using Mouse.Hover(UITestControl) in a try/catch block to determine if the control exists like so:
public bool DoesExist(UITestControl control){
if(control == null)
return false;
try{ Mouse.Hover(control); }
catch (UITestException)
{
return false;
}
return true;
}
It works sometimes, but in certain situations it seems to return false positives for reasons I don't understand. I'm still flying blind, and I'm nearly out of ideas.
I am using Visual Studio 2012, and Microsoft .NET Framework version 4.5.50709.
Partial answer about the Find() and TryFind() methods.
After setting the various search properties in the class instance for the control the Find() method does the actual searching for a control to match. The SearchProperties are used to try and find a control. If no controls are found then the search fails - forget exactly what happens then, possibly an exception is thrown but the documentation does not state that. If one control is found that the Find() completes. If two or more are found then the search continues by using FilterProperties to reduce the number of controls found to one.
The Coded UI recorder generates code of the style UIControl aControl = this.UIMap.uione.uitwo.uithree; which leads to the question of how does uione get a value referring to a control such that uitwo can be evauated? The only answer I have found is in the Description part of http://blogs.msdn.com/b/balagans/archive/2009/12/28/9941582.aspx which says "the search for the control starts ( explicit by Find() or implicit by any usage of the control in actions or property validations )".
So Find() performs the search for a control and it can be called explicitly or implicitly.
TryFind() is basically the same as Find() except that it returns a boolean indicating whether the control was found. Again, the documentation is poor but I believe that TryFind() returns true if exactly one control is found, false otherwise.
Another useful find method is FindMatchingControls which returns a (possibly empty) collection of all controls that match the search criteria.
As per yonitdm's answer, using the BoundingRectangle can help when there are multiple items that match but most are not on display. The values of Top and Left can also be used. Doing a FindMatchingControls and screening the results to ignore anything with negative Top or Left may work.
When developing tests the DrawHighlight method is useful, it draws a rectangle around a control. The same sort of rectangle that is drawn when recording assertions with the cross-hairs tool.
The Coded UI content index has lots of good information. The link to "How does UI Test Framework find (search) for a control" may be particularly helpful for you.
Instead of using obj.Exists() we have coded our own exists method that uses a combination approach of EnsureClickable() and BoundingRectangle.Width>0 to make sure that the control has a screen point.
ETA- oops, sorry left off an important part. Updated to add .Width to make sure it's greater than 0, you may need to use length if you width is somehow not working.
I am using tryfind() .. it is working fine.
if (obj_webor.GenLink.TryFind())
{
logdata.WriteLine(obj_webor.GenInnerText + " Exist !");
}
else
{
logdata.WriteLine(obj_webor.GenInnerText + " Does Not Exist");
}
Earlier i was using obj_webor.GenLink.exist().. but is was giving error if it was control not existing and exception occurs. tryfind is ok

List-based publish subscribe pattern in c# (Wordpress hooks / filters)

I come from a PHP background and have used Wordpress quite a lot, I love how their plugin architecture works and the ability to hook events to event names. One of the best parts I like about it is being able to *add_filter()* to any database value just before it gets shown to the end user. My question is multi-part on how to replicate the whole plugin architecture in a C#.NET environment?
Part 1:
To create plug-ins I have researched the MEF framework would probably be the best (Managed Extensibility Framework -http://mef.codeplex.com/). This is designed specifically to take the grunt work out by giving you the ability to monitor directories for new plug-ins, tracking dependencies and other normal things. MEF ships with .NET 3.5+
Part 2
Hooking events? I can't seem to find much information about replicating a global channel based event system. From what I have upto yet I need a publish/subscribe pattern (which isn't that hard to make as you just create some concrete objects and give them events). The hard part is giving each event a 'channel' name and for all the events in the whole system to be part of a global collection (Mediator pattern).
To replicate: (http://codex.wordpress.org/Function_Reference/add_filter)
Example 1
// Add's my button to the end of the content
add_filter('the_content', 'my_plugin_button');
function my_plugin_button( $content ) {
// Add's my button to the end of the content
return $content . "<a href='#'>My button</a>";
}
OR
Example 2
// Add a new admin menu item by hooking in
add_action('admin_menu', 'my_plugin_menu');
function my_plugin_menu() {
add_options_page('My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options');
}
I hope your all with me upto yet? I have managed to replicate the functionality I need in Javascript and even jQuery has their .on() event function... same thing but channel or list based...
My 2 examples:
http://jsfiddle.net/AaronLayton/U3ucS/53/
http://jsfiddle.net/AaronLayton/eyNre/33/
Can anyone point me in the correct direction or is this the totaly wrong approach for c#?
I think NServiceBus can help you a lot with these issues. Udi Dahan which is the author of NServiceBus has also written a lot of articles about Domain Event pattern, which is a publish/subscribe mechanism.
Know it's been a long time since you posted this and you probably built something already. However I have been thinking about something like this myself. There are 2 options - really forget WordPress and try and build something much cleaner - it's a mess at the bottom of WordPress' code :D
Or this:
function the_content()
{
var result = get_the_content();
// do other stuff...if you want to.
execute_filters(ref result);
execute_actions(ref result);
return result;
}
function execute_filters(ref string result, string action_name)
{
var filters = get_pre_filters(action_name);
filters.ForEach(filter =>
{
/// somehow call the method name in filter. PHP is generally global. C# namespaced,
/// so you would need to think about that.
}
}
function execute_actions(ref string result, string action_name)
{
/// and so on....
}
When building something to mimic WordPress, you need to remember many of the issues of WordPress' plugin architecture (in my personal opinion)... It seems to want to run every plugin near enough on every page even if that page has nothing to do with that plugin. I onced installed a plugin that added 60 database queries to each page call, and it wasn't used.
Try and think smart about it when you are building it. Try and add a way to only have the plugins that are going to get used on the page/post of your new setup to be run e.g. in your database have a "Plugins" field on the post/page object with a list of plugins allowed to run on that page. That way you won't need to check all the plugins each time to see if it wants to run.
Anyways. Hope you got something working.

Monotouch.dialog - how to create a delegate to access an Element Value the moment it is altered

I am beginning to use the monotouch.dialog utility and would be most grateful if someone could advise me how it is possible to access instantaneous changes to a value of an element (EntryElement, FloatElement, BoolElement etc) just after it has been altered by the user. There is a delegate I could use like:-
public void ViewingElementApi ()
{
viewingroot = CreateViewingRoot ();
var dv = new DialogViewController (viewingroot, true)
{
Autorotate=true
};
dv.ViewDisappearing += delegate
{
Console.WriteLine(EntryElement.Value);
};
configure.PushViewController (dv, true);
}
This snippet was courtesy of an earlier question on this. However, I am really interested in getting a value the moment it is altered by the user if this is possible.
Thanks
Gordon Pagan
It sounds like a nice enhancement - but it's not something that's presently available across all types.
E.g. for the types you mentioned:
EntryElement has a Changed event;
BoolElement has a ValueChanged event;
FloatElement does not even if it uses an UISlider and it's ValueChanged event internally (but that's not publicly accessible outside MonoTouch.Dialog).
You best bet is to adapt MonoTouch.Dialog for your need (and later submit pull requests to share your work with others :-)

Categories

Resources