Handling Event without use of Button? - c#

I just created an Button in xaml so, it created an event for handling the button click.
I want to call the contents inside the event handler as a function. So, that i can call in some other place to satisfy my needs.
Currently i am working in Live tiles. examples that i got are all in the form of button click event to trigger the flip and cycle tiles.
I want to call those into function to make it work.
Or else any one give the example of Lives tiles from the beginning to end

Related

C# adding infinity loop in button click event and break it with another button click event

I tried to make a while (work){....}
inside button event called start
to do some stuff like label changing in the same form with random values
and I have another button called stop I want to make the work=false; so the while should be break
the problem is when I clicked start button and the form froze and did nothing
how I can do that like stay in the while loop and access all other events
As long as the loop runs, no events (like your other button's click) can be processed. This results in freezing the UI.
Better use a Timer instead of an infinite loop. The timer will not freeze the UI but call a Tick event at defined intervals and allow other events to be processed between two ticks. It is then easy for another button to stop this timer.
Since you have not mentioned any UI technology (is it WinForms, WPF, WebForms, MAUI, Xamarin, ...?) and not shown any code, it is difficult to give you example code.

MRTKv2 PressableRoundButton prefab with events with WMR HMD don't seem to be working for me

I've created a little test scene where I want to use the PressableRoundButton prefabs to additively load a scene when pressed. I've hooked up the button to call into a script and pass the name of the scene I want to load, this code is verified to work with a debug keyboard shortcut so I know that part is good. What doesn't seem to work is the button event itself. Perhaps I've gone and done something really stupid, but I stupidly can't figure out what I've done wrong.
I'm using the DefaultMixedRealityToolkitConfigurationProfile which may or may not be the problem but I'm a little unsure how to diagnose that. I kind of figured the default one would be the safest bet. I can see the button visually press down when I click on it using the WMR controller so all the mechanics behind the button are working in terms of having it push down and all when I hit the trigger while pointing at it
Button Events list
Thank you for sharing an image of your setup! Try calling your code not in the PressableButton component, but on the Interactable component's OnClick handler (you need to expand the "Events" dropdown in Interactable.
The PressableButton component actually will only raise the button pressed, etc. events when you are directly poking a button with near interaction. For pressing the button at a distance, use the OnClick event handler in Interactable.
Have a look at the picture here to see where the OnClick handler lives.
Note that OnClick will only fire on pointer unpressed, and will also (currently) not fire if you stay pressed for too long. If you'd like to instead run the code when you press down, you can instead handle the OnPress event.
Have a look here for that example.
Note that by default Interactables only handle an OnClick event -- you add more event handlers by adding "Event Receivers" by clicking the "Add Event" button. You can then pick the receiver type. In the case of the round button prefab, there is already a InteractableOnPressReceiver set up for you that exposes the OnPress and OnRelease events.
The way to think about buttons in MRTK is:
Interactable is actually a 'button' that can be interacted with both nearby and at a distance. It exposes many different event handlers, and has lots of visual options. It's called "Interactable" and not "Button" mostly for historical reasons (it was that way, people started using it, now changing it would break many people).
PressableButton is a more complex touch-only button that actually moves in with your finger. Pressable button actually talks to Interactable, setting things like its pressed state, so that folks can listen just to Interactable if they want to work both near and far.
In most cases, add your event handlers to Interactable, not pressable button. Pressable button is for touch only.

How to get a button event to repeat fire when holding down said button

I want to be able to hold down a button (i.e. MouseLeftButtonDown) and have it's event repeat fire.
I code Mouse.Capture(button) in the MouseLeftButtonDown event and Mouse.Capture(null) in MouseLeftButtonUp event.
If I hold the mouse button down, the MouseLeftButtonDown only fires once. I can see this not by setting a breakpoint but by using Console.Writeline().
I had this very code working at some point. Then I refactored and put the mouse events in an Interface. Everything still works except this repeat action now!
Update - More information:
Here is the before call which was in a specific class:
Mouse.Capture(sender as MenuGelButton)
MenuGelButton is a xaml creation of ellipses and text to make a cool button.
Since adding the interface (which is common code for several xaml creations all of which inherit the interface), here is the current call from within the interface:
Mouse.Capture(sender as UIElement)
Update of Update:
A mouse click event is not designed to auto repeat fire. A keyboard event is. I had this working (and it continues to work) on a keyboard event not the mouse event. Sorry for the confusion...
I believe you are looking for Microsoft's RepeatButton. You can use it in XAML just like you would use a Button, and it fires a click event multiple times.
From their documentation:
A RepeatButton is a button that raises Click events repeatedly from the time it is pressed until it is released. Set the Delay property to specify the time that the RepeatButton waits after it is pressed before it starts repeating the click action. Set the Interval property to specify the time between repetitions of the click action. Times for both properties are specified in milliseconds.
Try doing something like this:
mouse button --> fires event
in the event, set a timer of your liking (how many times per second would you like the action to happen?)
in a loop, every time timer fires, check to see if mouse button is still "down". If yes, fire. If not, don't fire.
you'll probably want to add another event of mouse button up, that will stop the timer, so it won't fire anymore (or bind to a bool that will let you know if you want to fire ... up to you ..)
Hope it helps.

Copy EventHandler of a Control to another

I have a Button (button1) , How can I copy it's Click property (I mean button1.Click) to another Button (button2), Something like:
button2.Click=button1.Click;
You cannot do this. Events only allow you to add or remove handlers, you cannot get a list of all of the handlers that they have. It is a function that is intentionally removed from you.
You could keep track of the handlers you assign to a particular button yourself, independently, and then add those handlers to another button, to accomplish this effect.

Prevent Click events on child view while swiping(flicking) parent. WinPhone8.1

I have canvas with listbox inside it.
each child element of listbox sets eventhandler for Click event.
On canvas I set eventhandlers for
ManipulationStarted="canvas_ManipulationStarted"
ManipulationDelta="canvas_ManipulationDelta"
ManipulationCompleted="canvas_ManipulationCompleted"
My code for swiping works perfect accept one thing, it fires Click eventhandler before ManipulationCompleted eventhandler.
But for example listbox in the same time scrolls perfectly and do not fire Click event.
So basically what I need is to handle manipulation events in same way listbox do.
If this condition is true:
private void canvas_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
e.DeltaManipulation.Translation.X > [some value]
....
}
I need to disable firing Click event on any child element of canvas, doesn't matter if it is inside listbox or not.
Why are you setting click handlers if you don't want them to fire?
Click fires on pointer pressed, so there's no way to tell if the user wanted to Click or to start a manipulation. You'll need to either decide based on the location clicked or a later event if you want to differentiate between "clicks" and swipes.
Instead of Click you can handle the Tap gesture along with the manipulation events. Since Tap fires on the pointer released the manipulation system will fire it if the user tap and releases in one spot and it will trigger the manipulations if the user presses and moves the pointer.
See How to handle manipulation events for Windows Phone 8 for more details.

Categories

Resources