Button Click will not navigate to a new WPF page - c#

I cannot figure out why my button click is not navigating to a new page. I have tried writing this code several ways, but none of them work. EnlargedScreenCap is a WPF page and is in the same directory as the window in which I want to load it.
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
NavigationService nav = NavigationService.GetNavigationService(this);
ImageSource image = sender as ImageSource;
EnlargedScreenCap esc = new EnlargedScreenCap();
esc.SetImage(image);
nav.Navigate(esc);
}
Written like this I get a null reference exception because NavigationService is not getting initialized.
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
ImageSource image = sender as ImageSource;
EnlargedScreenCap esc = new EnlargedScreenCap();
esc.SetImage(image);
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new Uri("//EnlargedScreenCap.xaml"), UriKind.RelativeOrAbsolute);
}
When written like the above code, I get an invalid URI error, although nav is still null.
I tried searching around and this problem has been brought up many times, but no one seems to have a decent explanation or at least one that I have been able to successfully apply.
Could someone point me in the right direction?

I do not think you should be creating the NavigationService the way you are doing. Also if you want to enable navigation all this should be done in a Frame
<Frame x:Name="_mainFrame" />
Then you could do something like
_mainFrame.NavigationService.Navigate(new Uri("EnlargedScreenCap.xaml", UriKind.Relative));
Without the double slashes in front.
Checkout this article.
http://paulstovell.com/blog/wpf-navigation
Hope it helps

Related

WPF Frame.Navigate triggering multiple events

I'm a fairly new to WPF and C#.
I have a frame component on my main window and 4 buttons next to it that navigate to different views in the frame. Within one the the views there is a DataGrid that has a SelectionChanged event which makes an SQL call to a database that fetches records, whose data is then used to populate a list of custom objects (these relate to the selected item on the DataGrid).
Anyways, the problem I have is that from time to time multiple calls (2 or 3) to the SelectionChanged event are being triggered at the same time for a single selection change (mouseclick) on the DataGrid.
The navigation button click events on the main window all look like this:
private void btn_MyDesk_Click(object sender, RoutedEventArgs e)
{
MainFrame.Navigate(new Uri("/Views/MyDeskView.xaml", UriKind.Relative));
}
private void btn_AllOrders_Click(object sender, RoutedEventArgs e)
{
MainFrame.Navigate(new Uri("/Views/AllOrdersView.xaml", UriKind.Relative));
}
After some experiementation, I've found that the bug only happens after changing views away from the view with the DataGrid, and then changing back to it (but not always). When the bug appears the number of calls generally corresponds to the number of times I had switched views. Furthermore, the bug will simply vanish if leave the program alone for a minute or two. This makes me suspect that there multiple instances of the DataGrid view lingering like ghosts in memory and duplicating event calls until they are cleaned up by a garbage collector.
Should I be cleaning something up each time I switch views, or am I looking in the wrong place?
Thank you in advance for any help.
Edit: In answer to #Peter Moore
I subscribe to the event in the DataGrid declaration within the views XAML: SelectionChanged="dtg_MyDeskOrderGrid_SelectionChanged"
Edit: This is the sequence that happens on a selection change in the data grid. It includes several UI changes while the SQL records for the new selection are retrieved and displayed on a second DataGrid (dtg_MyDeskOrderItems). While the SQL call is being made, the relevant controls are disbaled and a semi-transparent panel (bdr_DGLoadingPanel) is moved on screen to cover them and display a loading animation. When the work is done, the work area is re-enabled and the loading panel moved off screen. Focus is also returned to the main "order" Datagrid.
dtg_MyDeskOrderGrid: This is the main DataGrid showing all "Orders"
dtg_MyDeskOrderItems: This is a secondary DataGrid that is updated to show all "Items" in the selected order.
private void dtg_MyDeskOrderGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CurrSelectedOrder = (Order_class)dtg_MyDeskOrderGrid.SelectedItem;
if (CurrSelectedOrder.ItemList == null)
{
if (NowWorking == false)
{
NowWorking = true;
bdr_DGLoadingPanel.Margin = new Thickness(2);
dtg_MyDeskOrderGrid.IsEnabled = false;
bdr_FilterPanel.IsEnabled = false;
bdr_DGLoadingPanel.Focus();
img_LoadingCircle.RenderTransform = rt;
img_LoadingCircle.RenderTransformOrigin = new Point(0.5, 0.5);
da.RepeatBehavior = RepeatBehavior.Forever;
rt.BeginAnimation(RotateTransform.AngleProperty, da);
bdr_DGLoadingPanel.UpdateLayout();
worker.RunWorkerAsync();
}
}
else
{
dtg_MyDeskOrderItems.ItemsSource = null;
dtg_MyDeskOrderItems.ItemsSource = CurrSelectedOrder.ItemList;
dtg_MyDeskOrderItems.Items.Refresh();
}
}
private void worker_DoWork(object? sender, DoWorkEventArgs e)
{
DatabaseConnection DBConn9 = new DatabaseConnection();
DBConn9.FillOrderItems(CurrSelectedOrder);
}
private void worker_RunWorkerCompleted(object? sender, RunWorkerCompletedEventArgs e)
{
this.Dispatcher.Invoke(() =>
{
dtg_MyDeskOrderItems.ItemsSource = null;
dtg_MyDeskOrderItems.ItemsSource = CurrSelectedOrder.ItemList;
dtg_MyDeskOrderItems.Items.Refresh();
bdr_DGLoadingPanel.Margin = new Thickness(1000, 2, 2, 2);
rt.BeginAnimation(RotateTransform.AngleProperty, null);
dtg_MyDeskOrderGrid.IsEnabled = true;
bdr_FilterPanel.IsEnabled = true;
// The following work-around and accompanying GetDataGridCell function were used to give keyboard focus back to the datagrid to make navigation with arrow keys work again.
// It appears keyboard focus is not returned to the Datagrid cells when using the Datagrid.focus() method.
Keyboard.Focus(GetDataGridCell(dtg_MyDeskOrderGrid.SelectedCells[0]));
NowWorking = false;
});
}
Edit...
Following the advice of the commentors, I was able to fix the bug by unsubscribing from the event in the Unloaded event for the view containing the DataGrid:
private void uct_MyDeskView_Unloaded(object sender, RoutedEventArgs e)
{
dtg_MyDeskOrderGrid.SelectionChanged -= dtg_MyDeskOrderGrid_SelectionChanged;
}
However, I was not able to reproduce the bug using a barebones testing project.
The UI in the original project is quite heavy, so I'm wondering if it's not the old view and events lingering in memory as this seems to fit the behavior of the bug & fix (Only occuring when I navigate away and back causing a new view to be created, multiple event triggers corresponding to the number of times I navigated away, and then finally the bug vanishing of its own accord after a moment or two).
I won't be settling on this as a final solution and instead will learn about ways I can reuse instances of my views (as suggested by Bionic) instead of recreating them. The reason for this is, if the SelectionChanged event is getting multiple triggers from old view instances, then it is likely other events will suffer from the same bug. This would be bad.
#BionicCode If you are still around, could you repost your initial comment as a solution so I can mark it answered?
Thank you to everyone for all the help and education. ^_^
Following the advice of the commentors, I was able to initially fix the bug by unsubscribing from the event in the Unloaded event for the view containing the DataGrid:
private void uct_MyDeskView_Unloaded(object sender, RoutedEventArgs e)
{
dtg_MyDeskOrderGrid.SelectionChanged -= dtg_MyDeskOrderGrid_SelectionChanged;
}
However as this solution only disconnected the one event and didn't solve the root problem of old views lingering in the background and firing events before being cleaned up, I finally went the following code that keeps only one instance of my view in memory and reuses it.
private MyDeskView? currMyDeskView = null;
private void btn_MyDesk_Click(object sender, RoutedEventArgs e)
{
if (currMyDeskView == null)
{
currMyDeskView = new MyDeskView();
}
MainFrame.Navigate(currMyDeskView);
}
Thank you to everyone who helped me get over this bug.

Page loaded in frame is not styled

I can't for the life of me figure out how to make a multiple page application in wpf. My issue is that when I load a page into the frame it does not use the style of the page
xaml
<Frame x:Name="Main" Margin="0,82,0,0"</Frame>
c# code
private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
Main.DataContext = new settingsPage();
Main.Content = new settingsPage();
SettingsButton.Content = Main.DataContext.ToString();
}
Hopefully someone knows what is going on here and can help :)
Thanks
Here you can find a fully working example:
Usage of Frame control
The thing is that you just need to switch the source of the frame by using the NavigationService. That should do the trick with your styles.

How to change the background of a button programatically?

I need to change the BackgroundImage of a button on click of another button (In Windows Forms in C#). But I can't find out how to do it!!
I searched on the internet and found many examples and all of them use ImageBrush, ImageSource etc.... but these don't work on my application, it shows me errors every time I Use them.
I read on the internet that I have to add this namespace:
using Windows.UI.Xaml.Media.Imaging;
But it shows me an error on the begging which says to add this System before Windwons and when I add it:
using System.Windows.UI.Xaml.Media.Imaging;
it shows me than the error at UI .... I can't figure it out how to solve this!!
Please help me guys!
To Change Background Image of a button there are two ways i know.
Add the Image to the resources folder of your project and use.
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Properties.Resources.ImageName;
}
Use Image.FromFile();
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "//Card1.png");
}
You are trying to use solutions for WPF in Winforms. This will not work.
The class you need is System.Drawing.Image (or System.Drawing.Bitmap, which inherits from Image).
Bitmap b = new Bitmap(#"C:\myBitmap.jpg");
myButton.Image = b;
Be sure to call Dispose on your Bitmap if and when it is no longer in use.

Redirect page after changing UriMapper in Windows Phone 8

I have an app, and when the RootFrame is first initialized, I have it checking if its the first time the app has been launched. If it is, it changes the RootFrame UriMapper to a tutorial page. the problem is, I can't seem to figure out a way to redirect the user back to MainPage.xaml. It just won't do anything so far.
This is the code I'm using for changing the initial start up page in the App constructor:
if (App.Model.SelectFirstStart())
{
var mapper = new UriMapper();
mapper.UriMappings.Add(new UriMapping
{
Uri = new Uri("/MainPage.xaml", UriKind.Relative),
MappedUri = new Uri("/TutorialPage.xaml", UriKind.Relative)
});
RootFrame.UriMapper = mapper;
}
When the user taps a button in the tutorial page, it should redirect them to the MainPage.xaml. This is what I've tried so far:
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
And:
App.RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
Any help would be much appreciated. Thank you
I see a couple of issues.
First, you are not changing the mapping in your tutorial page so basically MainPage.xaml still maps to TutorialPage.xaml.
Second issue is even after fixing that, navigation to MainPage.xaml will still not work because despite the actual page being TutorialPage.xaml, RootFrame.CurrentSource will still point to MainPage.xaml no matter what.
To fix this you need to do the following in your tutorial page's button click
// Inside the Button click event of TutorialPage.xaml
// Change the mapping so that MainPage points to itself
((UriMapper)App.RootFrame.UriMapper).UriMappings[0].MappedUri =
new Uri("/MainPage.xaml", UriKind.Relative);
// Since RootFrame.CurrentSource is still set to MainPage, you need to pass
// some dummy query string to force the navigation
App.RootFrame.Navigate(new Uri("/MainPage.xaml?dummy=1", UriKind.Relative));
// Remove back entry so that if user taps the back button
// you won't get back to tutorial
App.RootFrame.RemoveBackEntry();

How to Implement NavigationService in Silverlight

My Silverlight application has multiple XAML pages. For example, one displays a clock, one displays a timer. I have buttons to switch back and forth like so:
private void switchRight(object sender, RoutedEventArgs e)
{
this.Content = new Clock();
}
private void switchLeft(object sender, RoutedEventArgs e)
{
this.Content = new Timer();
}
I am trying to use the NavigationService to switch back and forth so I can have other pages running in the background rather than creating a new instance each time.
I am trying
NavigationService.Navigate(new uri("/Timer.xaml", UriKind.Relative));
but it doesn't seem to do anything and I can't find any good examples to help.
Here is a link
http://blogs.msdn.com/b/dphill/archive/2009/04/28/silverlight-navigation-part-3.aspx ,
Beside, I think you can use Threading for background processes.i.e.when you start a timer no need to show any xaml.
But for page instances you need to manage it very carefully otherwise stackoverflow :)
Depending on business rules its hard to decide navigation as being in a web browser.
We created our own Wizard (with rules). You may create your own NavigationManager. For validation I can offer http://fluentvalidation.codeplex.com/

Categories