Issue when using Routed Commands with a Frame - c#

My WPF project has a main window with a number of buttons and a frame. I display a page within the frame, and the buttons above use routed commands in order to call methods present in the page below. With me so far?
Everything works fine until I change the page. Even though this new page has methods which relate to the commands, it seems the routed commands are still looking for methods in the previous page. I don't know why this is happening when I've set the typeof() argument in the command itself:
public static RoutedCommand cmd = new RoutedCommand("Foo", typeof(BarPage));
How can I fix this? I've noticed that setting focus on a textbox in the newly-selected page will correct things, however this isn't a viable solution as the page will not always contain fields. I've also tried setting focus on the page itself but the problem still applies.
Thanks in advance.

Related

limiting tabbing to a custom in-window message box

I have made a custom MessageBox for my application and it launches as a UserControl. I have two buttons inside it and I would like to allow users to press Tab to switch between Buttons. However, since it's a UserControl overlaying the content, Pressing tab more than twice makes the focus go in the background on elements that aren't supposed to be tabbed at.
I can't figure out a good idea how to prevent this, I've thought of making a method that will select all elements and make their IsTabStop values to false and then restore them later, but I think that would be more of a problem then a solution.
Is there a way around this to limit tabbing only to the UserControl?
I would also appreciate advice on working with the message box.. the whole messagebox is an async function that has an infinitive loop until the answer is given. Is there another way to stop the application until one of the message box options was selected?
Crowcoder's reference has lead to correct MSDN page where I found my solution:
dialog = new UCMessageBox("Are you sure you want to exit the application?", MBType.YesNo);
AppMessageBox.Children.Add(dialog);
KeyboardNavigation.SetTabNavigation(dialog, KeyboardNavigationMode.Cycle);
The key was to call .SetTabNavigation function and direct it to my dialog (custom UserControl for the message box) and setting the KeyboardNavigationMode to Cycle.
After closing the UC rest of the application continued normally regarding navigation.

ScriptManager.RegisterStartupScript On Button Click (within updatepanel) Does not fire on FIRST click

My project is essentially a web page with a menu and an updatepanel. The user selects a choice from the menu, and a usercontrol is loaded into the updatepanel. I would prefer not to use an update panel, but, there are multiple databound controls that contain information that will then change the contents of another databound control. Anyways, my project calls for a message box to appear on the screen, which I have no issue in getting to work:
string s= "<script>alert('HayGuise');</script>";
ScriptManager.RegisterClientScriptBlock(c.Page, c.Page.GetType(), GUID.NewGUID().ToString(),s,false);
Now, I've tried a number of different tactics, including replacing GUID with a static string; I've used RegisterStartupScript, etc. My issue is that the message box does not show on the first click of the call button AFTER the updatepanel has updated to show a new usercontrol. But, if I click the button again, everything is great. If this is done on the very first form loaded into the updatepanel (opening up the site and selecting from the menu), everything works fine; it is only when the updatepanel updates again with a new form, that this will occur, and again, only on the first click.
Additionally, the code is being fired, and all code that follows the ScriptManager code fires, but the message box will not show until the button is clicked again. As I am testing this behavior at the moment, I've added the popup code to each of the buttons on the form, and the behavior is the same with each of them. It sucks that its taken so long to implement a reliable piece of code (for the messagebox) only to have this slight issue, and this is after I just figured out why my button events were firing twice on a single click. I pretty sure that it's going to be something simple, some sort of property that I'm not familiar with that needs to be set correctly. Damn, I'd love to just be able to settle all these little issues so I can proceed with the rest of the project. Sooooo close. Thank you for any help.
Edit: Additionally, the buttons are located in another usercontrol that is added to the usercontrol form that is shown in the updatepanel.
Edit II:
Thanks for your input, I apologize for getting back to this so late, I moved on to other parts of the project and just came back to this. The issue is not the startupscript method, nor the clientscriptblck method; the issue resides in the loss of value/viewstate/clientid's on postback.
At the time I posted this question, this project was a single page application. I tested functionality by creating a multipage application, and just kept going from there, vowing to come back to this issue. Well, I'm back and the issue does not reside in the update panel, it has something to do with my code, as I clear out the update panel (tried with a regular panel as well) and then reload the new usercontrol. When cleared, something goes haywire, and nothing fires on the first postback. Its a weird issue, that I have yet to find much (a couple articles) information on that is specific to just the first postback causing this issue. I'll update with an answer if I ever find one; the only fix I've found is to add a line to the menu select code, Server.Transfer(thecurrentpage), essentially refreshing the page. This solves all of the issues, but it seems a bit in poor taste. Thanks again for your input.
If you are working with AJAX UpdatePanel, following code should work for you. In my case, I was using update panel and none of the about worked except this one.
ScriptManager.RegisterStartupScript(myUpdatePanelID, myUpdatePanelID.GetType(), "myFunction", "myFunction();", true);
If you are using update panel in your page then the following code should work for you. You should use the RegisterStartupScript in the whole page using "this".ScriptManager.RegisterStartupScript(this, this.GetType(), "myFunction", "myFunction();", true);

Maps control SetView works only on the page's first load

I have a problem with Bing Maps control I use in my WP7 application.
When I'm navigated to the page with map control, it starts the GeoCoordinateWatcher. When GeoCoordinateWatcher has some location data for me, it calls a SetView() method to center the map at current location. There is also a button on ApplicationBar that's also starting the GeoCoordinateWatcher.
Now, the thing is, when I first navigate to this page, all works fine: GeoCoordinateWatcher starts, gives me the location data after some time, calls SetView() and the map centers when I need it too. The same with the button.
But if I press the Back button to get back to the main page and then try to navigate again to the map page, SetView() stops working. In debugging I see that the code surrounding it works as expected and the data passed to SetView() is correct, but nothing happens with the control and events for changing view don't fire either.
I'm assuming there may be something wrong with map initialization (or disposing on navigating from), but I don't know where to dig.
Edit:
I've tried changing Map.Center directly and it doesn't work in exactly the same way the SetView() doesn't: it works fine the first time page is navigated to and doesn't work on the other times.
Edit 2:
Ok, it gets weirder. In debugging I see that my map's center actually gets set to a correct value. But an actual control shows absolutely different location and me, moving map around, doesn't change the value that I see from code.
Edit 3:
I've added a button to ApplicationBar that just calls SetView() and it works fine. Apparently, the problem appears when I call SetView() as a result of GeoCoordinateWatcher.PositionChanged event raising. How could I work around that?
Try using map.Center = loc.CoOrdinates;
I've found the solution.
In my original code I've subscribed to the GeoCoordinateWatcher.Position property change event in the page constructor: App.PropertyChanged += AtmInfoPageOnAppPropertyChanged
All I had to do was to move that to the OnNavigatedTo event handler and add App.PropertyChanged -= AtmInfoPageOnAppPropertyChanged to the OnNavigatedFrom event handler.
I think, the problem was that keeping the old page subscribed to the event didn't allow it to be disposed, and at the same time didn't allow the new page to subscribe to that same event, thus causing the code to be called for the different page and different map control than the one displayed on the screen.

wp7 contentpresenter - setting content to the same page

I use a page (named containerpage) with a ContentPresenter (named PageContent) and set its content to a PhoneApplicationPage instance (contentpage) in the page's OnNavigatedTo() eventhandler. I also have a mainpage with a button "Show Form". When I click on that button, the program navigates to the containerpage, that sets its contentpresenter's content to the contentpage. My problem is: if I click on "show form", then press the back button and click on "show form" again, I get ArgumentException with the text
"The parameter is incorrect"
on line
this.PageContent.Content = contentpage;
in the containerpage's OnNavigatedTo() eventhandler. I guess this is because I already set another ContentPresenter's Content to this contentpage (because navigating creates a new page), but if this is the problem, how can I unassign my contentpage from the previous ContentPresenter's Content?
As decyclone mentioned, your question would be a lot clearer with detailed code. That said, you might implement OnNavigatedFrom() on your conatinerpage and set the Content to null.
You might want to consider how this design holds up if the app gets tombstoned on containerpage.
To be honest, I'm amazed that setting the Content property of one page to be an instance of another page even works! I really would advise against that as a long term solution. If the content of the first page is dynamic based on some condition, then use different UserControls instead.
If the data for the UserControl is not persistent between calls to ShowForm, then you can just create a new instance each time and then you won't have the re-parenting problem.
If you can elaborate on your application scenario and what you are trying to achieve, then we might be able to provide a better answer.

Loading .aspx into a Panel/UpdatePanel

I'm currently working on a web-based game using ASP.NET and C#. Previously, I've been just organizing the various game screens(start, hi-score, results, etc) into individual Panels nested within a main UpdatePanel and showing/hiding the appropriate Panels as the game progresses. However, I've severely underestimated the scale of the game and I'm gonna have to break it down to more manageable chunks.
My idea now is to have a Main.aspx with an empty UpdatePanel and I'll load the various screens as individual .aspx files into it. Kinda like with WinForms I think, when you create another new WinForm and add it to the current parent form.
I've been trying to find how to get about doing this in the code-behind, but I'm still at a lost. I could probably do this with Response.Redirect, but then there would be the usual flicker when the new page loads, which is something I want to avoid.
I did come across something interesting called a UFrame but that seems to work in the HTML instead of the code-behind.
I appreciate any help or suggestions in this matter, and apologize if this has been asked before. Thank you.
Edit: With regards to womp's answer, .ascx seem to be a step in the right direction for me. I successfully got my Startscreen control control up and running inside the Main.aspx. However, another problem has popped up, where the screen just goes blank when I try to add another usercontrol to a panel inside Main.aspx upon a button click.
The event handler I have for a button in the Startscreen control is:
protected void Btn_Arcade_Click(object sender, EventArgs e)
{
this.Parent.Controls.Add(LoadControl("Arcade.ascx"));
}
I'm not too sure if there's anything wrong with that, but the Parent in this case is basically what I called the GameContentPanel that resides within Main.aspx's UpdatePanel.
I don't think it's a problem with the HTML fudging up either, since I tested it by loading it first instead of the Startscreen, and everything showed up fine.
I've also tried loading both controls together at the start in Main.aspx, and both controls load correctly as well.
Can the "screens" be .ascx usercontrols? You can load a usercontrol into a panel using LoadControl("~/path/to/myControl.ascx"), and then those controls can take part in the lifecycle of the main page.
If they really have to be in separate .aspx pages, then you might need to look at using an iframe, or else maybe rethinking your approach. Webforms apps were designed to have lots of things happening inside the context of one page/form, and not transferring control back and forth to multiple pages.

Categories

Resources