Using Events From A Class - events

I am trying to get a function to be called everytime an event occurs. In the KinectRegion class there is an event called HandPointerGrip: http://msdn.microsoft.com/en-us/library/microsoft.kinect.toolkit.controls.kinectregion.handpointergrip.aspx.
I see that it has declared the event and it seems to me that the event has already been set to be invoked(HandPointerEventArgs)? How do I attach a function to this event?
public Menu()
{
KinectRegion.HandPointerGripEvent+=Hand_Gripped; // why doesn't this work? :(
}
private void Hand_Gripped(object sender, HandPointerEvnetArgs e)
{
MessageBox.Show("I work!"); // I wish this would work
}
Been working hard on this problem and here is something I think will work. Afraid to test it. Learning a lot about routed events, delegates, and events.
namespace ...
{
public delegate void HandPointerEventHandler(object sender, HandPointerEventArgs e);
public partial class thePage : Page
{
public event HandPointerEventHandler HandGripped
{
add {this.AddHandler(KinectRegion.HandPointerGripEvent,value);}
remove {this.RemoveHandler(KinectRegion.HandPointerGripEvent,vlaue);}
}
public thePage()
{
InitializeComponent();
this.HandGripped += new HandPointerEventHandler(OnHandGripped);
}
protected virtual void OnHandGripped(object sender, HandPointerEventArgs e)
{
MessageBox.Show("hello"); //hopefully
}
}
}

The first block of code should work fine. My guess is that the HandPointerGripEvent is hooked up ok, it just never fires.
How are you setting up your KinectRegion?
Are you updating the interration library each frame?
Perhaps this helps?
Kinect SDK 1.7: Mapping Joint/Cursor Coordinates to screen Resolution

KinectRegion.AddHandPointerGripHandler(this.Button1, this.Button1_Click);
Here Button1 is:
< k:KinectTileButton x:Name="Button1" Height="150" Width="150" Content="Click"/ >
The namespaces:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:k="http://schemas.microsoft.com/kinect/2013"
Button1_Click is the method itself, for example:
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("YOU GOT ME !!!");
}
If you want to add a grip handler for another interface object, you just do:
KinectRegion.AddHandPointerGripHandler(< object name >, < method name >);
And s.o.

Related

Xamarin Forms WebView.CanGoBack always returns false on UWP

Using the Xamarin Forms WebView control, I'm overriding the OnBackButtonPressed() and finding that the CanGoBack always returns false in UWP.
I don't see this problem in Android.
Is this a XF bug or am I doing something wrong?
Note: I'm running XF v2.3.3.193
EDIT: I upgraded to XF 2.3.4.247 and the problem persists.
I have created a code sample and reproduce your issue when the WebView browse several website. And I have found reason in the Xamarin.Forms source code.
void UpdateCanGoBackForward()
{
((IWebViewController)Element).CanGoBack = Control.CanGoBack;
((IWebViewController)Element).CanGoForward = Control.CanGoForward;
}
The CanGoBack property will be changed when UpdateCanGoBackForward method invoked. And UpdateCanGoBackForward method was called only when the native NavigationCompleted event was invoked. So if some website could not be loaded quickly, the CanGoBack property would not be changed.
You can improve this design by custom WebView. And you could follow the code below.
CustomWebView.cs
Add the new property for CustomWebView.
public class CustomWebView : WebView
{
public bool CCanGoBack { get; set; }
public CustomWebView()
{
}
}
CustomWebViewRenderer.cs
And change the property when the ContentLoading event invoked.
[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace CustomWebViewTest.UWP
{
public class CustomWebViewRenderer : WebViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.ContentLoading += Control_ContentLoading;
}
}
private void Control_ContentLoading(Windows.UI.Xaml.Controls.WebView sender, Windows.UI.Xaml.Controls.WebViewContentLoadingEventArgs args)
{
(Element as CustomWebView).CCanGoBack = Control.CanGoBack;
}
}
}
MainPage.cs
private void backClicked(object sender, EventArgs e)
{
if (Browser.CCanGoBack)
{
Browser.GoBack();
}
}

ICommentEvent runs TWICE in ONE comment event Sitefinity

I have these code, according to document from sitefinity:
protected void Application_Start(object sender, EventArgs e)
{
Bootstrapper.Initialized += new EventHandler<ExecutedEventArgs>(Bootstrapper_Initialized);
}
public void Bootstrapper_Initialized(object sender, ExecutedEventArgs args)
{
if (args.CommandName == "Bootstrapped")
{
EventHub.Subscribe<ICommentEvent>(evt => CommentsEvent.CommentEventHandler(evt));
}
}
And the handler:
public static void CommentEventHandler(ICommentEvent evt)
{
// My code here
}
The problem is this handler always runs twice when a comment event happens (post a comment or approve a comment).
Could you please tell me why this happens and any possible way to avoid this? (I don't believe static boolean is a good idea).
Thanks
ICommentEvent is a base interface that is implemented by multiple events such as ICommentCreatingEvent, ICommentCreatedEvent, ICommentUpdatingEvent, ICommentUpdatedEvent and some others.
In your case it is fired twice due to firing of both ICommentCreatingEvent and ICommentCreatedEvent.
You can subscribe to just one of them and it should fire just once.

Assert that a mocked (MOQ thus DynamicProxy) event has no handlers attached

This is quite straight forward(ish) to do is the event is 'real' as in now created by DynamicProxy, but I can't work anything out for a mocked event.
The best way to explain what I'm trying to achieve is with code, please see the comment lines in the test method:
using System;
using Moq;
using NUnit.Framework;
namespace MOQTest
{
[TestFixture]
public class EventsMoqTest
{
[Test]
public void DetachTest()
{
var hasEventMock = new Mock<IHasEvent>();
using (var observer = new Observer(hasEventMock.Object))
{
//Assert that hasEventMock.Object has handler attached
}
//Assert that hasEventMock.Object DOES NOT have handler attached
}
}
public interface IHasEvent
{
event EventHandler AnEvent;
}
public class Observer : IDisposable
{
private readonly IHasEvent _hasEvent;
private readonly EventHandler _hasEventOnAnEvent;
public Observer(IHasEvent hasEvent)
{
_hasEvent = hasEvent;
_hasEventOnAnEvent = _hasEvent_AnEvent;
_hasEvent.AnEvent += _hasEventOnAnEvent;
}
void _hasEvent_AnEvent(object sender, EventArgs e)
{}
public void Dispose()
{
_hasEvent.AnEvent -= _hasEventOnAnEvent;
}
}
}
Unfortunately, you can't. This isn't really a moq issue, but the way the C# event keyword works with delegates. See this SO answer for more information.

Eventhandler is fired more than once

I have a problem with two of my EventHandlers, they work the same, so here is one:
private void Form1_Load(object sender, EventArgs e)
{
webBrowserWebsite.Url = new System.Uri(textBoxURL.Text, System.UriKind.Absolute);
webBrowserWebsite.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserWebsite_DocumentCompleted);
}
void webBrowserWebsite_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\CurrentData.wfd");
sw.Write(webBrowserWebsite.Document.Body.InnerText);
sw.Close();
}
The problem is, that the EventHandler fires multiple times, it doesn't stop!
Why is it doing this?
Thanks in advance
The code that you have written won't compile (your StreamWriter in your EventHandler isn't assigned to anything) and without more context as to how you are calling this, it is difficult to say for certain.
But the most likely reason is you are calling Form1_Load multiple times, but using the same webBrowserWebsite object. Each time the form loads, you are adding a new Event Handler. And since you aren't showing any code showing where you removing the event handler, I'm guessing it fires once for each time you call Form_Load.
Depending on your design, you are better off adding the event handler in the constructor so it is only added once regardless of the number of times you load the form.
public Form1()
{
webBrowserWebsite.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserWebsite_DocumentCompleted);
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowserWebsite.Url = new System.Uri(textBoxURL.Text, System.UriKind.Absolute);
}
Or remove the event handler in the event handler:
void webBrowserWebsite_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\CurrentData.wfd");
sw.Write(webBrowserWebsite.Document.Body.InnerText);
sw.Close();
webBrowserWebsite.DocumentCompleted -= webBrowserWebsite_DocumentCompleted;
}
Also, since StreamWriter implements IDisposible, you should be putting it inside of a using block or at least calling sw.Dispose() at the end of the method

How to sink COM events from Silverlight?

I want to raise an event from a .NET class, and receive this event in Silverlight code (out-of-browser, using the COM interop support added in SL4).
Can anyone point out the problem in my code? Do I maybe need to do more attribute-decorated interface boilerplate to get this working?
Code:
Rather than write native COM code, I am writing .NET code and exposing it via COM interop.
My event-raising .NET class looks like this:
using System;
namespace TestComInterop2 {
public class TestClass {
public event EventHandler TestEvent;
public void Fire() {
if (TestEvent != null)
TestEvent(this, EventArgs.Empty);
}
}
}
My SL4 client code looks like this:
...
private delegate void HandlerDelegate(dynamic sender, dynamic eventArgs);
private void TestEventSinking(object sender, RoutedEventArgs e)
{
// Create instance of COM-registered .NET class
var testClass = AutomationFactory.CreateObject("TestComInterop2.TestClass");
// Subscribe to event (second line fails with System.Exception)
//
// "Failed to add event handler. Possible reasons include: the object does not
// support this or any events, or something failed while adding the event."
//
AutomationEvent testEvent = AutomationFactory.GetEvent(testClass, "TestEvent");
testEvent.AddEventHandler(new HandlerDelegate(HandleTestEvent));
// Fire the event
testClass.Fire();
}
private void HandleTestEvent(object sender, object eventargs)
{
MessageBox.Show("Event fired");
}
...
Use the [ComSourceInterface] attribute on your class. The relevant MSDN Library topic is here.

Resources