Routing class for navigation between forms - c#

I'm new to C# and object orientated coding therefore I was wondering how to create a routing class for navigation between forms in a .Net Framework WPF application.
At the moment I am navigating between forms like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
new Classes.SessionService();
}
private void Customer_Page_click(object sender, RoutedEventArgs e)
{
CustomerPage CP = new CustomerPage();
CP.Show();
this.Close();
}
private void Staff_Menu_click(object sender, RoutedEventArgs e)
{
Forms.StaffPage SP = new Forms.StaffPage();
SP.Show();
this.Close();
}
private void Exit_click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
I have been told it is better to create a routing class as it will separate code more, but I have not been able to find an example for navigating between forms but only between web pages.

Im strongly recommend you to use an existing library bevor starting to create your own infrastructure. (I've learnd it the hard way and ended up using prism ;) ) There are other great libraries out there, too. Just go to youtube and search for "c# prism outlook" there is a pretty good prism tutorial from brian lagunas.
Edit:
Just to give you an basic idea how you can solve this by yourself:
Main Window xaml:
<Window x:Class="WpfTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTestApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<WrapPanel Grid.Row="0" x:Name="Ribbon"/>
<ContentPresenter Grid.Row="1" x:Name="Content"/>
<StackPanel Grid.Row="2">
<Button Grid.Row="0" Content="SetContentFormA" Click="Button_Click"/>
<Button Grid.Row="0" Content="SetContentFormB" Click="Button_Click_1"/>
<Button Grid.Row="0" Content="AddNewFormAToRibbon" Click="Button_Click_2"/>
<Button Grid.Row="0" Content="RemoveFormBFromRibbon" Click="Button_Click_3"/>
</StackPanel>
</Grid>
MainWindow.cs:
public partial class MainWindow : Window
{
private FormB b;
public MainWindow()
{
InitializeComponent();
PageControler.Content = Content;
PageControler.Ribbon = Ribbon;
b = new FormB();
Ribbon.Children.Add(b);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PageControler.SetContent(new FormA());
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
PageControler.SetContent(new FormB());
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
PageControler.AddToRibbon(new FormA());
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
PageControler.RemoveFromRibbon(b);
}
}
PageControler:
public static class PageControler
{
public static ContentPresenter Content { get; set; }
public static WrapPanel Ribbon { get; set; }
public static void SetContent(UserControl control)
{
Content.Content = control;
}
public static void AddToRibbon(UserControl control)
{
Ribbon.Children.Add(control);
}
public static void RemoveFromRibbon(UserControl control)
{
Ribbon.Children.Remove(control);
}
}
FormA (& B):
<UserControl x:Class="WpfTestApp.FormA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfTestApp"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="Violet">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="Form A"/>
</Grid>
You can althou

Related

the event that was called from window1 to window 2 in WPF Not working

**
I have 2 windows in app. mainwindow with 2 buttons and window1 with a
label. btn one opens window1 and btn 2 changes window1 label color.
**
public partial class MainWindow : Window
{
Window1 sWindow = new Window1();public MainWindow()
{InitializeComponent();
}
private void btnFirstWindow_Click(object sender, RoutedEventArgs e)
{
sWindow.Show();
}
private void btnChangeBackground_Click_1(object sender, RoutedEventArgs e)
{
sWindow.ChangeBackground();
}
}
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public void ChangeBackground()
{
if (lblShowUser.Background == Brushes.Green)
{
lblShowUser.Background = new LinearGradientBrush(Colors.Red, Colors.Red, 90);
}
else
{
lblShowUser.Background = Brushes.Green;
}
}
}
IF I remove this part
**
private void btnFirstWindow_Click(object sender, RoutedEventArgs e)
{
sWindow.Show();
}
** From mainwindow and add both windows at app xaml
Startup="App_Startup"
StartupUri="MainWindow.xaml">
public partial class App : Application
{
void App_Startup(object sender, StartupEventArgs e)
{
Window1 sWindow = new Window1();
sWindow.Show();
}
}
the method of label changing color when i press the other button doesnt work
am new to C# and wpf So when answering please also give explantion on what to be done to the code and why
expecting it to work even if changes are made regarding when the other page shows up but its not working
Create an object as a field from Window1 class, then you can access its objects.
Output (tested in Visual Studio 2017, .Net Framework 4.5.2):
Note: if this solution helps you, please mark as answer (don't forget to vote for me).
XAML (Mainwindow):
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="OpenButton" Click="OpenButton_Click" Content="Open" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Margin="99,0,0,0"/>
<Button x:Name="ChangeColorButton" Click="ChangeColorButton_Click" Content="Changing color" HorizontalAlignment="Left" VerticalAlignment="Center" Width="99" Margin="331,0,0,0"/>
</Grid>
</Window>
XAML (Window1):
<Window x:Class="WpfApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="Window1" Closing="Window_Closing" Height="300" Width="300">
<Grid>
<Label x:Name="Label" Content="Label" HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto"/>
</Grid>
</Window>
C# (MainWindow):
public partial class MainWindow : Window
{
Window1 W1 = new Window1();
public MainWindow()
{
InitializeComponent();
}
private void OpenButton_Click(object sender, RoutedEventArgs e)
{
W1.ShowDialog();
}
private void ChangeColorButton_Click(object sender, RoutedEventArgs e)
{
W1.Label.Background = new SolidColorBrush(Colors.Blue);
}
}
C# (Window1):
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.Visibility = Visibility.Hidden;
}
}
Thanks

How to trigger a method in a XAML element when a textproperty changes

I really don't know how to search to find a solution to this (I googled a lot, maybe I'm blind...).
I have a ComboBox which also contains a TextBox. The ComboBox is instantiated in a separate Control.xaml with a specific DataContext, where it gets its content for the Popup list.
Now when I type something into the TextBox, I want to trigger a method which then filters the list of my DataContext for the specific elements.
So my ComboBox.cs has some of the following content:
public event EventHandler FilterTextChanged;
protected virtual void OnFilterTextChanged(EventArgs args)
{
FilterTextChanged?.Invoke(FilterText, args);
}
public string FilterText
{
get { return _filterText; }
set
{
//This point is reached when I type something into the TextBox within the ComboBox
if (_filterText == value) return;
_filterText = value;
OnFilterTextChanged(EventArgs.Empty);
OnPropertyChanged("FilterText");
}
}
And in my Control.xaml, I have configured it like this:
<my:ComboBox x:Name="FURecipeComboBox"
AuthorizationMode="IsEnabled"
IsTextSearchEnabled="False"
StaysOpenOnEdit="True"
FilterTextChanged="FURecipeComboBox_OnFilterTextChanged"
ItemsSource="{Binding RecipeFileNames}"
SelectedItem="{Binding Value, Delay=100, ElementName=AlphaNumericTouchpadTextVarIn}"
d:DataContext="{d:DesignInstance {x:Type adapter:ToolRecipeVariableInfo}, IsDesignTimeCreatable=False}">
</my:ComboBox>
But I just can't get it to catch the event "FilterTextChanged", and my method "FURecipeComboBox_OnFilterTextChanged" will not be reached anytime...
I would be really really glad for some hints or help!
Kind regards
BB
Have a look at RoutedEvents at microsoft docs
This is a example post from Stackoverflow
In your case try to change EventHandler to RoutedEventHandler.
I made a small example:
Main.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:UserControl1 HorizontalAlignment="Left" Height="207" Margin="348,175,0,0" VerticalAlignment="Top" Width="311" MyClick="UserControl1_MyClick"/>
</Grid>
Main.cs
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void UserControl1_MyClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("Yep");
}
}
}
control.cs
public partial class UserControl1 : UserControl
{
public event RoutedEventHandler MyClick;
public UserControl1()
{
InitializeComponent();
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
if (MyClick != null)
MyClick(this, new RoutedEventArgs());
}
}
}
control.xaml
<UserControl x:Class="WpfApp1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="372.313" d:DesignWidth="350">
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="218" Margin="59,54,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="202" KeyDown="textBox_KeyDown"/>
</Grid>

Redirect to another page in WPF on certain conditions

Hi i am making a WPF application.
I have a mainwindow, which i have my navigation in, then i also have a stackpanel, with this:
<Frame x:Name="_mainFrame" NavigationUIVisibility="Hidden"/>
inside, where i place my pages inside.
In My mainwindow i navigate to other pages using the following to for example move to the gameWindow:
private void NavigateGameWindow(object sender, RoutedEventArgs e)
{
_mainFrame.Navigate(new GameWindow());
}
This works fine, but now that i am inside that window (gameWindow), i am checking if a "player" is set, if not, i want to navigate to another page, where i can set certain values.
and then navigate back to GameWindow.
But how do i get a hold of _mainFrame, when it is a part of the mainwindow ?
It says in GameWindow on _mainFrame
The name _mainFrame, does not exist in the current context
Game Window
public partial class GameWindow
{
private int numberOfPlayers;
private Player[] players;
private INavigator _navigator;
public GameWindow(INavigator navigator)
{
_navigator = navigator; //assign navigator so i can navigate _mainframe to other pages.
// initialize game properties, check if they are set.
var gameProp = new GameProperties();
this.numberOfPlayers = 2;
this.players = gameProp.CheckPlayerIsSet(this.players);
//check if a player has been set
if (this.players != null)
{ // Player is set or has been set. proceed or start the game.
InitializeComponent();
}
else
{ // redirect to settings window because players has not been set!
_navigator.Navigate(new GameSettings());
}
}
}
Main Window
public partial class MainWindow : Window, INavigator
{
public MainWindow()
{
InitializeComponent();
}
private void ExitGame(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
public void Navigate(Page p)
{
_mainFrame.Navigate(p);
}
private void NavigateRulesWindow(object sender, RoutedEventArgs e)
{
Navigate(new GameRulesWindow());
}
private void NavigateGameWindow(object sender, RoutedEventArgs e)
{
Navigate(new GameWindow(this));
}
}
GameSettings
public partial class GameSettings : Page
{
public GameSettings()
{
InitializeComponent();
//var gameProps = new GameProperties();
// set number of players,, should prompt user, and get value!
//gameProps.SetNumberOfPlayers(2);
}
}
View for gamesettings
<Page x:Class="ITD.OOP_Projekt.WPF.Menu.GameSettings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ITD.OOP_Projekt.WPF.Menu"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="GameSettings">
<Grid Background="white">
<TextBox HorizontalAlignment="Left" Height="23" Margin="229,144,0,0" TextWrapping="Wrap" Text="This is game settings" VerticalAlignment="Top" Width="120"/>
</Grid>
</Page>
One very easy solution is this:
So with the following code you have only one Window (Mainwindow) and inside that Window you display your pages. You can compare it with your internet browser. You have one window and inside that window you can navigate between pages (settings, game, highscore, ...).
I hope this helps and you can get it to work!
If not i can try to upload a simple example to github in the evening.
Just get rid of your GameWindow and implement it as a page.
MainWindow
xaml:
<Window x:Class="PageSwitcher.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PageSwitcher"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden" />
</Grid>
cs:
public partial class MainWindow : INavigator
{
public MainWindow()
{
InitializeComponent();
Navigate( new Page1(this) );
}
public void Navigate( Page p )
{
MainFrame.Navigate( p );
}
}
Page1
xaml:
<Page x:Class="PageSwitcher.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PageSwitcher"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid Background="Green">
<Button Width="150" Height="30" Click="ButtonBase_OnClick" Content="Go to Page2" />
</Grid>
cs:
public partial class Page1 : Page
{
private INavigator _navigator;
public Page1(INavigator navigator)
{
_navigator = navigator;
InitializeComponent();
}
private void ButtonBase_OnClick( object sender, RoutedEventArgs e )
{
_navigator.Navigate(new Page2(_navigator));
}
}
Page2
xaml:
<Page x:Class="PageSwitcher.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PageSwitcher"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page2">
<Grid Background="Blue">
<Button Width="150" Height="30" Click="ButtonBase_OnClick" Content="Go to Page1"/>
</Grid>
cs:
public partial class Page2 : Page
{
private INavigator _navigator;
public Page2(INavigator navigator)
{
_navigator = navigator;
InitializeComponent();
}
private void ButtonBase_OnClick( object sender, RoutedEventArgs e )
{
_navigator.Navigate(new Page1(_navigator ));
}
}
Thats all you really need.
In this example you can switch between two pages on button click events.
Just start a new wpf project and copy the code.
Play around with it until you understand it and then try to implement it in your game :)
If you want to redirect the page from one page to another you can create one method or function in which you can write the conditions and use the below code for redirection. It will take care of the directory also.
NavigationService.Navigate(new Settings.LatestActiveUsers());

wpf Usercontrol in usercontrol no response

I have a strange problem in my project. There are pages made from usercontrol and menu bar (also usercontrol).
Here is my usercontrol that contains few buttons
public partial class UpperBar : UserControl
{
public UpperBar()
{
InitializeComponent();
}
public event EventHandler EventbtClicked;
private void btConnect_Click(object sender, System.Windows.RoutedEventArgs e)
{
EventbtClicked(this, e);
}
}
I added this in my page as follows:
<local:UpperBar VerticalAlignment="Top" Grid.Row="0" Height="78" Grid.ColumnSpan="3" Margin="0,2,0,0"/>
And in my page tried to call event:
public PageStatus()
{
InitializeComponent();
Plc.ExecuteRefresh += new EventHandler(RefreshLeds);
UpperBar.EventbtCliced += new EventHandler(UpperBatButtonClick);
}
protected void UpperBarButtonClick(object sender, EventArgs e)
{
//do something
}
But I can't access my event using this UpperBar.EventbtCliced, why ?
You need to access the instance of your class UpperBar in PageStatus, not the class UpperBar itself!
The easiest way for you here:
Name your UpperBar in your XAML, example:
<local:UpperBar x:Name="_myBar" x:FieldModifier="private"/>
Then use this instance in your PageStatus.xaml.cs:
public partial class MainWindow : Window {
public MainWindow()
{
InitializeComponent();
_myBar.EventbtClicked += new EventHandler(UpperBarButtonClick);
}
protected void UpperBarButtonClick(object sender, EventArgs e)
{
//do something
}
}
Now if you are working seriously in WPF, you should really learn about Databinding and MVVM, catching event this way is not the best way to do it at all.
You should use Custom Command (RoutedUICommand) rather than bubbling event from user control.
here are some steps to follow in contrast to your approach:
1: create class myCustomCommand.
namespace WpfApplication1
{
public class myCustomCommand.
{
private static RoutedUICommand _luanchcommand;//mvvm
static myCustomCommand.()
{
System.Windows.MessageBox.Show("from contructor"); // static consructor is called when static memeber is first accessed(non intanciated object)
InputGestureCollection gesturecollection = new InputGestureCollection();
gesturecollection.Add(new KeyGesture(Key.L,ModifierKeys.Control));//ctrl+L
_luanchcommand =new RoutedUICommand("Launch","Launch",typeof(myCustomCommand.),gesturecollection);
}
public static RoutedUICommand Launch
{
get
{
return _luanchcommand;
}
}
}
}
In the xaml of UserControl:
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:CustomCommands="clr-namespace:WpfApplication1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.CommandBindings>
<CommandBinding Command="CustomCommands:myCustomCommand.Launch" Executed="CommandBinding_Executed">
</CommandBinding>
</UserControl.CommandBindings>
<Grid >
<TextBox Name="mytxt" Height="30" Width="60" Margin="50,50,50,50" ></TextBox>
<Button Name="b" Height="30" Width="60" Margin="109,152,109,78" Command="CustomCommands:ZenabUICommand.Launch"></Button>
</Grid>
Now in User control code
Handle command_executed
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
mytxt.Text = "invoked on custom command";
}
}
}

How can I make the code-behind of UserControls inherit a base class?

I've got a page manager that keeps a collection of pages (usercontrols), sends itself to each page which can thus call its SwitchPage method at anytime enabling me to put in links from one page to another. This works well and allows me to have a quick menu etc.
public partial class PageManager : UserControl
{
private Dictionary<string, UserControl> Pages = new Dictionary<string, UserControl>();
private string defaultPageIdCode = "page1";
private UserControl currentPage;
private string currentPageIdCode;
public PageManager()
{
InitializeComponent();
LoadPages();
SwitchPage(defaultPageIdCode);
}
private void LoadPages()
{
Pages.Add("page1", new Page1(this));
Pages.Add("page2", new Page2(this));
}
public void SwitchPage(string pageIdCode)
{
currentPageIdCode = pageIdCode;
currentPage = Pages[pageIdCode];
PageArea.Content = currentPage;
}
}
However, each page (UserControl) has repeated functionality in it, e.g. saving the PageManager object internally, which I would like to put in a base class:
Page1.xaml:
<UserControl x:Class="TestPageManager23434.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Background="White"
Width="400"
Height="400"
>
<TextBlock Text="this is page1"/>
<Button Content="go to page 2" Click="Button_Click"
HorizontalAlignment="Left"
Width="200"
Height="30"
/>
</StackPanel>
</UserControl>
Page1.xaml.cs:
public partial class Page1 : UserControl
{
private PageManager pageManager;
public Page1(PageManager pageManager)
{
this.pageManager = pageManager;
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
pageManager.SwitchPage("page2");
}
}
Page2.xaml:
<UserControl x:Class="TestPageManager23434.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Background="White"
Width="400"
Height="400"
>
<TextBlock Text="this is page2"/>
<Button Content="go back to page 1" Click="Button_Click"
HorizontalAlignment="Left"
Width="250"
Height="30"
/>
</StackPanel>
</UserControl>
Page2.xaml.cs:
public partial class Page2 : UserControl
{
private PageManager pageManager;
public Page1(PageManager pageManager)
{
this.pageManager = pageManager;
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
pageManager.SwitchPage("page1");
}
}
How can make each UserControl inherit a base class? It doesn't work since each UserControl has XAML which can't be inherited. And if I try to inherit from a plain class that inherits UserControl then it tells me:
Partial declarations of
'TestPageManager23434.Page2' must not
specify different base classes
Instead of declaring the root element as UserControl, declare it as your derived class. You will need to specify the namespace as well e.g.
<local:PageBase x:Class="TestPageManager23434.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestPageManager23434">
</local:PageBase>

Categories