LoadedPivotItem event on Pivot control not fired first time, WP7 - windows-phone-7

is there any reason why event LoadedPivotItem is not fired when DataContext of Pivot is being assigned therefore creating Pivot Items ?
It is only fired when I swype to next pivot item which is already handled by _SelectionChanged event.
protected override void OnNavigatedTo
(System.Windows.Navigation.NavigationEventArgs e)
{
var someData = LoadData();
pivot.DataContext = someData;
base.OnNavigatedTo(e);
}
void pivot_LoadedPivotItem(object sender, PivotItemEventArgs e)
{
// Will not stop here after data being assigned to DataContext
}
I need this event in particular cause I need PivotItem and it's data context for further operations.
Is there something I am doing wrong, or is there some other event that I can use here while getting a PivotItem ( PivotItemEventArgs ).
Thanks

I think the problem is the timing of how your page is loading. I don't see you attaching an event handler in your code, so I'm assuming that you're specifying the event handler in your XAML. The event may be firing too early, before everything on your page is fully loaded and wired up.
Try removing the event handler from your XAML and moving it to your OnNavigatedTo event (pseudocode):
protected override void OnNavigatedTo (NavigationEventArgs e)
{
var someData = LoadData();
pivot.DataContext = someData;
OnLoadedPivotItem += pivot_LoadedPivotItem;
base.OnNavigatedTo(e);
}
If that doesn't work, this article might be helpful. It describes the sequence of events that happens as the control tree is loaded. The suggestion that jumped out as potentially useful was attaching to the LayoutUpdated event instead of the Loaded event.

Related

WPF Shown Event fires while not fully painted

I want to open a form, make a screenshot and close it.
So I tried the Shown Event, but this fired before everything is painted.
Since Shown is the last event fired (according to MSDN), what can I do?
I think just delaying isn't such a good idea in an event driven environment.
Any advice? Thx in adv.
<
{
InitializeComponent();
UpdateData();
this.Shown += new System.EventHandler(this.PrintForm_Shown);
this.ShowDialog();
}
private void PrintForm_Shown(object sender, EventArgs e)
{
DoPrintForm();
Close();
}
>

Do code after OnNavigatedTo

I Have Code get IdUsers From Other Page
String IdUsers;
public Main_Wallets_Page()
{
InitializeComponent();
MessageBox.Show(IdUsers);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
String Id;
if (NavigationContext.QueryString.TryGetValue("IdUsers", out Id))
IdUsers = Id;
}
The MessageBox Alway be Null. I Want to MessageBox Show the "IdUsers" after OnNavigationTo (Do not put the MessageBox IN "OnNavigationTo").
How can i do it ?
You shouldn't use MessageBoxes in OnNavigatedTo because if the user does not press a button your app will crash since the framework thinks that navigation has failed. MessageBoxes in the constructor are equally as bad.
I can think of two options (I use #1 for these sorts of things):
Show the MessageBox in the Loaded event. But be careful it can be
fired more than once. In the constructor you might add the handler for the Loaded event and then in the handler you'd detach from the handler so that it is called only once.
Use Dispatcher.BeginInvoke around the MessageBox.Show call so that it does not block navigation. That might still block the Dispatcher thread. If you really wanted to go this route you could use ThreadPool.QueueUserWorkItem or a TPL Task.
I also have used OnLayoutUpdated in place of the Loaded event but I can't remember exactly why :) It seems like it might have been that the page hasn't yet displayed in Loaded and it has in the other event.
If this value was initialized, you can store it in application isolated storage. Then, when constructor is called, you can read it from there. In this case value of user ID will be initialized and MessageBox won't show you NULL.
DO NOT place MessageBox into OnNavigatedTo event.
Try to create an empty project with MainPage and Page2. Place button on MainPage to navigate to Page2. In Page2 place MessageBox in OnNavigatedTo event. Then everythig will work fine if you Start Debugging from VS. BUT if you deploy and run it you will see that when you navigate to Page2 you see MessageBox. Then don't do anything, just wait for about 15 sec. MessageBox will react as Canceled and APPLICATION WILL BE CRASHED! without any navigation to Page2 or MainPage. The same thing happens if you use Dispatcher.BeginInvoke around the MessageBox.Show.
I assume that OnNavigatedTo event has a timeout which works only when app is deployed. So you should run your MessageBox when Navigation is competed.
Everything works if you do
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
base.OnNavigatedTo(e);
var lcTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 200) };
lcTimer.Tick += (s2, e2) => {
(s2 as DispatcherTimer).Stop();
if (MessageBoxResult.OK == MessageBox.Show("Test, don't push", "", MessageBoxButton.OKCancel))
MessageBox.Show("OK");
else
MessageBox.Show("Cancel");
};
lcTimer.Start();
}
Note: If you have some code in OnNavigatedTo run above code at the end of OnNavigatedTo.
I liked what Austin Thompson(upvote) has adviced with ThreadPool.QueueUserWorkItem. But note that with this approach you need to place MessageBox inside the Dispatcher.BeginInvoke otherwise you will receive cross-thread exception. So the code is the following
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
base.OnNavigatedTo(e);
ThreadPool.QueueUserWorkItem((stateInfo) => {
Dispatcher.BeginInvoke(() => {
if (MessageBoxResult.OK == MessageBox.Show("Test don't push", "", MessageBoxButton.OKCancel))
MessageBox.Show("OK");
else
MessageBox.Show("Cancel");
});
});
}

Make a difference between a tab event and a flick event on listbox

I have a listbox which contains a list of images; I don't know how to differentiate betwwen a Flick event and a Tap event, to make a zoom on the chosen image?
There is a Tap event on all elements (in Mango). Tap event don't raised when user scrolls a list.
Also, you can place an image inside a retemplated Button (leave only content holder). Then you get for free Click event and Tilt Effect as well
There is additional support for detecting touch in the XNA library. Trying adding the Microsoft.Xna.Framework.Input.Touch reference to your project
Include the following using statement:
using Microsoft.Xna.Framework.Input.Touch;
Subscribe to the required events in your constructor as follows:
TouchPanel.EnabledGestures = GestureType.Tap | GestureType.Flick;
On your list box create an event for Manipulation Completed as follows:
ManipulationCompleted="ListBoxDays_ManipulationCompleted"
You could add code to the that event method to track the type of events that have been completed with the following code:
private void ListBoxDays_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
if (gesture.GestureType == GestureType.Tap)
{
//Do something
}
if (gesture.GestureType == GestureType.Flick)
{
//Do something else
}
}
}
Hope this Helps

Is it possible to detect the DOM element tapped in a WP7 WebBrowser control?

Is it possible to detect the DOM elements under a spot the user taps on a website using the WP7 WebBrowser control? I would like to let the user identify certain sections of the rendered page.
Yes. This code works pretty well for me:
private void button1_Click(object sender, RoutedEventArgs e)
{
// first define a new function which serves as click handler
webBrowser1.InvokeScript("eval", "this.newfunc_eventHandler = function(e) { window.external.notify(e.srcElement.tagName); }");
// attach function to body
webBrowser1.InvokeScript("eval", "document.body.addEventListener('click', newfunc_eventHandler, false);");
}
private void webBrowser1_ScriptNotify(object sender, NotifyEventArgs e)
{
// this should be called every time you tap an element; the value should be its tag name
Debug.WriteLine(e.Value);
}
It goes along Justin's answer (who beat me while coding). It first defines a click handler which then is attached to the page's body (all via InvokeScript). Every time you tap an element ScriptNotify should be called on the WebBrowser reporting the tapped element's tag name.
Make sure you have set IsScriptEnabled true for your browser control.
I'm guessing you'd have to build something off of the ScriptNotify Event.
At that point, you would register a JavaScript Event handler as usual but that event handler would, in turn, call window.external.notify("someMessageToHandle");. You would then have to route the calls appropriately.
If you don't have the ability to add the JavaScript event handlers directly on the page, you could instead add them using WebBrowserControl.InvokeScript.

RoutedEvents in WP7 Custom Control

In normal version of silverlight you can create an event handler by registering it by EventManager. Windows Phone 7 hasn't got that class.
My question is: How to create an event, which will be handled by the parent panels.
My scenario: I've created a custom class with some textbox in it. Foreach I've added my custom behavior, which raises when textblock is clicked. Behavior works like: "When this Textblock in custom control is clicked, please raise a custom event with my custom args (i want to pass them to the Custom Control itself (for example to specify to which VisualState change it)."
Can you help me how to handle my problem?
Could you provide sample code of what you are trying to do? it seems you want to create an event for when the TextBlock is clicked.
Add an event handler to the textblock:
public Event EventHandler<RoutedEventsArgs> TextClicked;
// Fire the event
private void OnTextClicked(object sender, RoutedEventArgs e)
{
if (TextClicked != null)
{
TextClicked(sender, e);
}
}
TextBlock.Click =+ OnTextBlockClicked;
private void OnTextBlockClicked(object sender, RoutedEventArgs e)
{
// Raise event
OnTextClicked(sender, e);
}
Something along those lines I think.

Resources