Create Event in WPF - c#

I want to rise an event to zoom in and zoom out the image once the gesture is recognized that it is valid (gesture recognized by Kinect). my image is shown in different user control loaded in a frame present in main window.
<Frame Name="currentFrame" NavigationUIVisibility="Hidden"></Frame>
and frame source is set like this.
currentFrame.Source = new Uri("Images.xaml", UriKind.RelativeOrAbsolute);
How can i create the custom event? and which one is the best ? tunnel or bubble?

I think in your case it's better to use tunel event, as what you need is immediate feedback from the user interaction and execute a single action on that: zooming. So there is no sence of notifying other controls between your usecontrol canvas (if any) and actual handling code.
For concrete implementation of the event, can have a look on:
Routed Events

Related

Struggling with WPF internal methods

UPDATE: So, I have a solution to my immediate problem. I haven't succeeded in making "my own" TreeView class. But, the reason I wanted to do that was because controls based on ButtonBase don't function in a Popup from a TreeView, and with the help of #MarkFeldman, I have found a solution that comes at it from a different angle.
The problem is that the MouseDown and MouseUp events bubble, and that bubbling crosses the logical tree boundary between the Popup and its owner. So, when you click on something hosted inside the Popup, the TreeViewItem and TreeView that ultimately own the Popup get to hear about it. This then triggers code inside the TreeView that checks, "Do I have focus?", and if not, helpfully sets focus back to itself -- but being a separate logical tree, the Popup has its own focus context, and so this effectively steals focus from the Button control while it is in the middle of processing a click. The Button responds to this by ignoring the click.
This erroneous handling in the TreeView only happens when MouseDown and MouseUp events reach it. What if there were a way to prevent it from seeing those events in the first place? Well, if you intercept the PreviewMouseDown and PreviewMouseUp events and mark them Handled, then the framework doesn't generate MouseDown and MouseUp events to begin with.
Looking at the Reference Source, it looks like ButtonBase's click handling is tied up in a couple of protected methods:
https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/Primitives/ButtonBase.cs,414
https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/Primitives/ButtonBase.cs,478
This means you can call them from your own subclasses! So, instead of making "my own" TreeView where all controls behave properly, instead I can make "my own" CheckBox that works properly in a Popup from a TreeView. Since all of the actual click handling is directly accessible, and the events it normally responds to use the same EventArgs type as the Preview events, and on top of it the default handling takes care of marking the events as Handled, the entire implementation boils down to this:
public class CheckBoxThatWorks : CheckBox
{
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) => base.OnMouseLeftButtonDown(e);
protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e) => base.OnMouseLeftButtonUp(e);
}
Nice!
ORIGINAL QUESTION:
I need to make a clone of the TreeView WPF class -- a copy of the control that runs out of my own code. (There is a bug in the control and Microsoft doesn't seem to deem it high-enough priority to fix, and it makes it completely impossible to host buttons (including check boxes and radio buttons) within pop-ups shown from TreeViewItems. link)
I am running into serious difficulties with the amount of internal shenanigans Microsoft has undertaken in the implementation of base WPF controls. A couple of issues I've bumped into:
Controls have a property HandlesScrolling that allows them to take over basic scrolling handling from ScrollViewer. But, it's marked internal, making it seemingly impossible for me to have a control of my own that does its own handling of scrolling from keyboard input. I was going to try having my TreeView handle keyboard scrolling in OnPreviewKeyDown instead of OnKeyDown, so that it can prevent KeyDown events from being raised for the keys it intercepts. I haven't gotten far enough to know what caveats there might be about this.
The Visual States system allows you to declare what styles should be applied when different states are entered, but actually entering states seems to be tied up in the virtual method ChangeVisualState on the Control type. All controls that want to switch between visual states can override this method and inspect their state to determine which Visual State should be shown. Oh wait. They can't because the method is internal! Apparently only Microsoft gets to create controls that set their own visual states??
Are there any strategies I can use to work around these limitations, or am I just completely out of luck?

WPF - Knowing which control will end up with focus

I've been playing around with events in WPF and have so far I've got good mileage out of 'Source' and 'OriginalSource' properties of the event args as well as using the sending control and FocusManager. Here's the thing, when a chain of events starts firing, is there any way to know what control will be ending up with focus at the end barring any intervening logic throughout the chain of events?
I'm afraid that the only reliable way of doing this is actually letting focus change and then handling it in some PreviewGotKeyboardFocus handler at top view level.
You can then know which control was going to get the focus, and cancel the change with e.Handled = true.
PD. There's a function in all UIElements called PredictFocus, but it only works with positional traverse changes, not with tab-based changes (or custom focusing).

How define priority for events in c#?

I'm developing a Windows Phone app using Map Control. Map control has CenterChanged and ZoomLevelChanged events.
When I change the zoom value for map, both events are raised (because the center changes as well).
But I want using only ZoomLevelChanged event, when I change zoom value.
How do I set priority for events if multiple events are available or how switch off CenterChanged event when ZoomLevelChanged is raised?
Any Solution/Demo/Link would be very much helpful for me.
You cannot define the priority for events, and you can't depend on the order of events.
I'm not a Windows Phone developer, but ask yourself: what if there were a zoom change at the same time as a large center change? You would need to handle both events, right? I would make that work, then make it work for the case of a zoom change with a small center change.
Events that happen "simultaneously" are added to the Dispatcher queue. If a single event sets "zoom" and then "center", the queue will contain the zoom event followed by the center event.
I'm not completely sure what you mean by defining priority. If you mean changing the order the event handlers are called, the only way to do this would be to modify the code that sets the two properties so that they are set in a different order. Once the event handlers are added to the queue, you can't reorder them.
If you want to handle only the first "simultaneous" event though, this should be possible by disabling the other handler until the end of the queue. Something like this:
// in ZoomLevelChanged handler:
_zooming = true;
mapControl.Dispatcher.BeginInvoke(new Action(() => _zooming = false));
// in CenterChanged handler:
if (_zooming) return;
// rest of implementation
BeginInvoke adds the action to the end of the queue, so the _zooming flag will only be reset once the immediate CenterChanged handler has been called and skipped, allowing subsequent CenterChanged events to be handled normally.

How to make a touchable notice top bar in windows phone?

How to make a touchable notice top bar in windows phone ?
I am new to C# and windows phone world.So may be my question has a simple
way to solve,but I google a lot ,and didn't work out.
here is my purpose: I have a timer running throughout my app,it request a
service for notice info every one hour, and show a "notice bar" on the top of
screen.
it is easy to get the information ,but when I want to show them to the Page,
here is my problem:
1.
I used system tray to show my info.
It works,but then I found there is no touch or click event for Progress
Indication bar.
I even add an event to Touch.FrameReported in App.xaml.cs , but still ,
when i touch the system tray area, the event doesn't fire.
2.
Then I want to use a Dynamic way to achieve it: add a text block to the
current page
I got the current page handler ,but case I only know the current page
handler's type is PhoneApplicationPage, I can't get my Root UI element
(all my page has a root element named "LayoutRoot")
And when I try to use reflect method to get the "LayoutRoot" property,
the return value is null.
The code looks like this :
Type type = PhoneApplicationPageHandler.getType()<
//I checked,the type is my page's type
type.getProperty("LayoutRoot") or type.getField("LayoutRoot")
//they all return null
BTW: LayoutRoot is a grid, and it is described in my page's .xmal file.
Now My option is to make all my page inherit a defalut page ,in this page ,I will
implement a method to fulfill my second way to simulate a "touchable top bar".
but I think this coding is ugly .
So, can anyone tell me :
1.how to add touch event to a SystemTray
or
2.how to get a handler of an ui element which is described in xaml, while I only have a PhoneApplicationPage type handler of that page.
You may use
1) a toast prompt described here http://windowsphonegeek.com/articles/WP7-Toast-Prompt-in-depth
2)or shell toast described here http://www.c-sharpcorner.com/UploadFile/ae35ca/working-with-toast-notification-in%C2%A0windows-phone-7/ according to what suits your requirement the best. 3)You may also create a custom control which you may place on the top on your mainPage and handle its tap event accordingly.

Get handles of ContextMenuStrip submenus

I am developing a plugin for an application, and I must register any controls I create with my host application, else they do not receive any messages and are effectively disabled. Registration is performed using a control's window handle. (Please treat this part as a given, it is only a background as to why I need this.)
In order to do this, for every winforms control I create, I use its HandleCreated event and HandleDestroyed event to recursively register/unregister the control's handle and any child controls it owns.
I can do the same thing with a ContextMenuStrip, and this is fine for the first level of items, but if any of those items have submenus, I do not know how I can get a handle to the sub-menu that has opened in order to register it.
The ToolStripMenuItem objects inherit from System.ComponentModel.Component and not from System.Windows.Forms.Control, so they do not have a .Handle property, nor HandleCreated and HandleDestroyed events.
See image for exactly which window I need the handle of (at time of creation).
Use ToolStripMenuItem.DropDown.Handle.

Categories

Resources