windows phone 8 OnTouchFrameReported method fires on every screen once initialized - c#

I am a complete beginner in C# so please be gentle if it's a stupid problem :D
I have created OnTouchFrameReported method on one frame but function fires on every screen i go from there have i done something wrong ???
I can handle this problem by checking witch frame is on... but problem is now in emulator after this method is fired and i leave the frame my button clicks and other metods take 3-4s to fire... i culd use your help here guys i am realy stuck :D
XAML is irrelevant in this case and code-behind
void OnTouchFrameReported(object sender, TouchFrameEventArgs args)
{
try
{
this.SetCrop(args.GetTouchPoints(this.SourceImg));
}
catch (ArgumentException)
{
}
}

I ran into a similar problem recently. What I did was remove the method from Touch.FrameReported whenever I was not dealing with the frame I wanted simply by doing:
Touch.FrameReported -= OnTouchFrameReported;
I know you don't want to involve XAML but I created a mouseLeftButtonDown and MouseLeftButtonUp event for the control I was working with and added and removed the touch frame reported method respectively.
XAML
<Image MouseLeftButtonDown="mouseDown" MouseLeftButtonUp="mouseUp"/>
Code
public void mouseDown(object sender, MouseButtonEventArgs m)
{
Touch.FrameReported += OnTouchFrameReported;
}
public void mouseUp(object sender, MouseButtonEventArgs m)
{
Touch.FrameReported -= OnTouchFrameReported;
}

Related

How to select all text of a TextBox on mouse click? (TextBox.SelectAll() not working on TextBox.Enter)

I have a TextBox and I want all the text inside of it to be highlighted when the user clicks on it (so that they can replace it easily). I have the following event handler linked up the the TextBox:
private void TextBox_Enter(object sender, EventArgs e) {
SelectAll();
}
When I click on the TextBox, the text is only selected for a fraction of a second (sometime it's so fast I can't see it at all) and then it goes back to being a cursor. Does anyone know how to fix this or if there are any relatively simple workarounds?
I tried the same thing with the TextBox.MouseClick event (and it highlighted the text), but because it was the MouseClick event the text was highlighted every time I clicked the TextBox (even when the TextBox already had focus).
I have also tried SelectionStart = 0; SelectionLength = Text.Length, but the same thing happens. This leads me be believe the issue has something to do with the event.
I also tried the TextBox.GotFocus event and had the exact same problem.
I am doing this in a Windows Form application.
The reason why you didn't see the text getting selected is that the TextBox is busy when one of those events occurred (e.g., caret positioning). You actually select the text, but then the internal event handlers of the TextBox execute and remove the selection e.g. by setting the caret position.
All you have to do is to wait until the internal event handlers have completed.
You do this by using the Dispatcher. When you invoke the Dispatcher asynchronously the delegate is not immediately executed but enqueued and executed once all previously enqueued actions (like the internal event handlers) are cleared from the dispatcher queue.
So going with the TextBox.GotFocus event in WPF (or the TextBox.Enter in WinForms) and the asynchronous Dispatcher will do the trick:
WPF
private async void SelectAll_OnTextBoxGotFocus(object sender, RoutedEventArgs e)
{
await Application.Current.Dispatcher.InvokeAsync((sender as TextBox).SelectAll);
}
WinForms
private void SelectAll_OnTextBoxEnter(object sender, EventArgs e)
{
var textBox = sender as TextBox;
textBox.BeginInvoke(new Action(textBox.SelectAll));
}
Thankfully I found a solution! It turns out that the Click event is executed before the Enter event, this allowed me to set up a JustGotFocus variable and do the following:
private void myTextBox_Click(object sender, EventArgs e) {
this.JustGotFocus = true;
if (JustGotFocus) {
myTextBox.SelectAll();
}
}
private void myTextBox_Enter(object sender, EventArgs e) {
JustGotFocus = false;
}
If anyone else has this problem hopefully my solution is useful.

C# PictureBox and Timer don't work together

so I am trying to make a simple game in C# using PictureBox for painting and timer for update functions, but what I noticed is that when I start painting on my picturebox my timer stops working.. I have no idea why..
Here is the picturebox code:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Console.WriteLine("PicBox running!");
if (this.MyGame.InGame)
{
this.pictureBox1.Refresh();
e.Graphics.Clear(Color.White);
MyGame.CurrentMap.Draw(e.Graphics);
//e.Graphics.Dispose(); <- if I uncomment this, then the whole programm just freezes
}
}
Here is the timer code:
private void timer1_Tick(object sender, EventArgs e)
{
Console.WriteLine("Timer running!");
if (this.MyGame.InGame)
{
MyGame.CurrentMap.Update();
MyGame.UpdateTime();
}
}
Here is a method called by MyGame.CurrentMap.Draw(e.Graphics); :
public void Draw(Graphics g)
{
foreach(Planet item in Entities){
item.Draw(g);
}
}
Any help would be greately appreciated. I come from javascript, so I don't really know if I am doing something terribly wrong here.
When a timer tick occurs, you want to repaint your PictureBox. To do this, you should make it so that the PictureBox receives a Paint event. This is what Refresh() does, so the call to this.pictureBox1.Refresh(); goes in the end of timer1_Tick.
It doesn't make sense for the Paint event to contain a call to Refresh, because this, in turn, generates a Paint event.

Different objects calling same method, how can I set the "caller's" property?

I have 15 images on my WPF application. I want it so that whenever MouseUp on any of the images is called.. it'll call the same method.
I would like to do something similar to the psuedo code written here.. This would save so much time instead of writing 15 individual methods for each button. How can I do something like this?
private void BluePick1_Image_MouseUp_1(object sender, MouseButtonEventArgs e)
{
sender.ImageSource = something;
}
thank you for any help
if your event is always on a button :
private void ButtonMouseUp(object sender, MouseButtonEventArgs e) {
((Button)sender).ImageSource = something;
}
and
button1.MouseUp += ButtonMouseUp;
button2.MouseUp += ButtonMouseUp;

Is there an Drag/Drop Cancelled Event in WinRT?

im looking for an Drag/Drop cancelled event in Metro, this means if the user drags an item and drops it outside of an droppable area.
how can i achieve this or is there any workaround?
I have not found such an event (for c#/XAML)! Perhaps (hopefully) an event will exists in the final release!
As a temporary workaround, I have registered to the Window.Current.CoreWindow.PointerReleased-event.
On drag start then, I set a boolean indicator to true, and if drag ends, the PointerReleased-event will be fired and I can test for the boolean indicator.
Workaround
In the constructor of the Page (or whatever element) register to PointerReleased:
Window.Current.CoreWindow.PointerReleased+=CoreWindow_PointerReleased;
The eventhandler could look somehow like this:
void CoreWindow_PointerReleased(CoreWindow sender, PointerEventArgs args) {
if (m_isDragging) {
m_isDragging = false;
// Here you know that a drag-operation came to a end
}
}
And the indicator you can set for example as follows;
private void Entries_DragStarting(object sender, DragItemsStartingEventArgs e){
m_isDragging = true;
// ...
}
Dude,
instead of capturing pointer released on the whole page
you can register "pointer capture lost" on the listview or the item that you drag, i believe it fires less times at least :D
listView_PointerCaptureLost(object sender, PointerRoutedEventArgs e){
//do the logic you want;
}

Silverlight C# - Simulate a Click event on a button

I have an element with a Click method.
I would like to activate that method (or: fake a click on this element) within another function.
Is this possible?
No, there currently isnt a way to do this. See links:
https://stackoverflow.com/questions/4137528/ui-automation-for-wp7
windows phone 7: How to simulate control click programmatically
EDIT
This doesnt do exactly what you wanted to do, but you can do something like this and get the same outcome. I do this type of pattern alot in my code to do get the same outcome.
XAML
<Button Name="testClick" Click="testClick_Click">
<TextBlock Text="Click Me"></TextBlock>
</Button>
C#
private void testClick_Click(object sender, RoutedEventArgs e)
{
TestFunction(sender as Button);
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//Call same function as button
TestFunction(testClick);
}
private void TestFunction(Button bt)
{
//do stuff
}
Somewhere in your class initiate a RoutedEventArgs:
private RoutedEventArgs evnt;
Then later call this:
element_Click(element, evnt);
That should do it.

Categories