Cannot go from one Page to a new Page while sending values in WPF C#
In my MainWindow.xaml.cs, I open a new Page (PkmnSelect) using this:
PkmnSelect pkmnSelect = new PkmnSelect();
Content = pkmnSelect;
Then, once the user has chosen their team of Pokémon in this page (PkmnSelect), they can click Start. The Start button has this as code:
Battle battle = new Battle(userPokemon, opponentPokemon);
Content = battle;
Battle is a page that I want to take as input two Pokémon, so I've created an additional constructor in Battle that looks like this:
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
userPokemon = userPkmn;
opponentPokemon = opponentPkmn;
}
This gives me the error "Page can only have Window or Frame as parent."
My question is, what is the right way to send values from one page to another?
I have looked everywhere and nothing I've tried has worked.
EDIT: The beginning of Battle.xaml.cs:
public partial class Battle : Page
{
Pokemon userPokemon;
Pokemon opponentPokemon;
public Battle()
{
InitializeComponent();
//Some code to hide some xaml stuff and start some music
}
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
userPokemon = userPkmn;
opponentPokemon = opponentPkmn;
}
c# wpf window
add a comment |
In my MainWindow.xaml.cs, I open a new Page (PkmnSelect) using this:
PkmnSelect pkmnSelect = new PkmnSelect();
Content = pkmnSelect;
Then, once the user has chosen their team of Pokémon in this page (PkmnSelect), they can click Start. The Start button has this as code:
Battle battle = new Battle(userPokemon, opponentPokemon);
Content = battle;
Battle is a page that I want to take as input two Pokémon, so I've created an additional constructor in Battle that looks like this:
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
userPokemon = userPkmn;
opponentPokemon = opponentPkmn;
}
This gives me the error "Page can only have Window or Frame as parent."
My question is, what is the right way to send values from one page to another?
I have looked everywhere and nothing I've tried has worked.
EDIT: The beginning of Battle.xaml.cs:
public partial class Battle : Page
{
Pokemon userPokemon;
Pokemon opponentPokemon;
public Battle()
{
InitializeComponent();
//Some code to hide some xaml stuff and start some music
}
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
userPokemon = userPkmn;
opponentPokemon = opponentPkmn;
}
c# wpf window
Can you show the code of Battle?
– Nikolaus
Nov 28 '18 at 23:39
Sure! @Nikolaus The following code is the beginning of Battle, everything but the rest of the methods. justpaste.it/37nlv
– MichaelReese's
Nov 28 '18 at 23:45
@Nikolaus I also posted it in the question (couldn't fit in the comment)
– MichaelReese's
Nov 28 '18 at 23:50
add a comment |
In my MainWindow.xaml.cs, I open a new Page (PkmnSelect) using this:
PkmnSelect pkmnSelect = new PkmnSelect();
Content = pkmnSelect;
Then, once the user has chosen their team of Pokémon in this page (PkmnSelect), they can click Start. The Start button has this as code:
Battle battle = new Battle(userPokemon, opponentPokemon);
Content = battle;
Battle is a page that I want to take as input two Pokémon, so I've created an additional constructor in Battle that looks like this:
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
userPokemon = userPkmn;
opponentPokemon = opponentPkmn;
}
This gives me the error "Page can only have Window or Frame as parent."
My question is, what is the right way to send values from one page to another?
I have looked everywhere and nothing I've tried has worked.
EDIT: The beginning of Battle.xaml.cs:
public partial class Battle : Page
{
Pokemon userPokemon;
Pokemon opponentPokemon;
public Battle()
{
InitializeComponent();
//Some code to hide some xaml stuff and start some music
}
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
userPokemon = userPkmn;
opponentPokemon = opponentPkmn;
}
c# wpf window
In my MainWindow.xaml.cs, I open a new Page (PkmnSelect) using this:
PkmnSelect pkmnSelect = new PkmnSelect();
Content = pkmnSelect;
Then, once the user has chosen their team of Pokémon in this page (PkmnSelect), they can click Start. The Start button has this as code:
Battle battle = new Battle(userPokemon, opponentPokemon);
Content = battle;
Battle is a page that I want to take as input two Pokémon, so I've created an additional constructor in Battle that looks like this:
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
userPokemon = userPkmn;
opponentPokemon = opponentPkmn;
}
This gives me the error "Page can only have Window or Frame as parent."
My question is, what is the right way to send values from one page to another?
I have looked everywhere and nothing I've tried has worked.
EDIT: The beginning of Battle.xaml.cs:
public partial class Battle : Page
{
Pokemon userPokemon;
Pokemon opponentPokemon;
public Battle()
{
InitializeComponent();
//Some code to hide some xaml stuff and start some music
}
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
userPokemon = userPkmn;
opponentPokemon = opponentPkmn;
}
c# wpf window
c# wpf window
edited Nov 28 '18 at 23:49
MichaelReese's
asked Nov 28 '18 at 23:07
MichaelReese'sMichaelReese's
134
134
Can you show the code of Battle?
– Nikolaus
Nov 28 '18 at 23:39
Sure! @Nikolaus The following code is the beginning of Battle, everything but the rest of the methods. justpaste.it/37nlv
– MichaelReese's
Nov 28 '18 at 23:45
@Nikolaus I also posted it in the question (couldn't fit in the comment)
– MichaelReese's
Nov 28 '18 at 23:50
add a comment |
Can you show the code of Battle?
– Nikolaus
Nov 28 '18 at 23:39
Sure! @Nikolaus The following code is the beginning of Battle, everything but the rest of the methods. justpaste.it/37nlv
– MichaelReese's
Nov 28 '18 at 23:45
@Nikolaus I also posted it in the question (couldn't fit in the comment)
– MichaelReese's
Nov 28 '18 at 23:50
Can you show the code of Battle?
– Nikolaus
Nov 28 '18 at 23:39
Can you show the code of Battle?
– Nikolaus
Nov 28 '18 at 23:39
Sure! @Nikolaus The following code is the beginning of Battle, everything but the rest of the methods. justpaste.it/37nlv
– MichaelReese's
Nov 28 '18 at 23:45
Sure! @Nikolaus The following code is the beginning of Battle, everything but the rest of the methods. justpaste.it/37nlv
– MichaelReese's
Nov 28 '18 at 23:45
@Nikolaus I also posted it in the question (couldn't fit in the comment)
– MichaelReese's
Nov 28 '18 at 23:50
@Nikolaus I also posted it in the question (couldn't fit in the comment)
– MichaelReese's
Nov 28 '18 at 23:50
add a comment |
1 Answer
1
active
oldest
votes
You can't have a Page
inside a Page
.
Instead you should let their common parent component handles the transition using events:
Pokemon
:
public class Pokemon
{
public string Name { get; set; }
}
PkmnSelect
<Page x:Class="WpfApp1.PkmnSelect"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="PkmnSelect">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Name="FirstPokemonList">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Label Grid.Column="1" VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold">VS</Label>
<ListBox Name="SecondPokemonList" Grid.Column="2" Margin="0,1,0,19" Grid.RowSpan="2">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Button Click="Button_Click" Grid.Row="1" Grid.ColumnSpan="3">FIGHT!</Button>
</Grid>
</Page>
public class PokemonsSelectedEventArgs : EventArgs
{
public Pokemon FirstPokemon { get; set; }
public Pokemon SecondPokemon { get; set; }
}
public partial class PkmnSelect : Page
{
public event EventHandler<PokemonsSelectedEventArgs> PokemonsSelected = delegate { };
public PkmnSelect()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PokemonsSelected(this, new PokemonsSelectedEventArgs
{
FirstPokemon = new Pokemon { Name = (string)FirstPokemonList.SelectedValue },
SecondPokemon = new Pokemon { Name = (string)SecondPokemonList.SelectedValue }
});
}
}
Battle
:
<Page x:Class="WpfApp1.Battle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Battle">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding UserPokemon.Name}" FontSize="40" FontWeight="ExtraBold"></Label>
<Label VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold" Grid.Column="1">VS</Label>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding OpponentPokemon.Name}" FontSize="40" FontWeight="ExtraBold" Grid.Column="2"></Label>
</Grid>
</Page>
public partial class Battle : Page
{
public Pokemon UserPokemon { get; set; }
public Pokemon OpponentPokemon { get; set; }
public Battle()
{
InitializeComponent();
DataContext = this;
}
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
UserPokemon = userPkmn;
OpponentPokemon = opponentPkmn;
}
}
MainWindow
:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Title="MainWindow" Height="450" Width="800">
<local:PkmnSelect PokemonsSelected="PkmnSelect_PokemonsSelected"></local:PkmnSelect>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void PkmnSelect_PokemonsSelected(object sender, PokemonsSelectedEventArgs e)
{
Content = new Battle(e.FirstPokemon, e.SecondPokemon);
}
}
1
I’ll try this as soon as possible! Thank you
– MichaelReese's
Nov 29 '18 at 0:07
I have a problem with this solution: my main window is giving me the following error: The member "PokemonSelected" is not recognized or is not accessible. I've been reading around and people are saying stuff about changing AnyCPU to x86... What should I do?
– MichaelReese's
Nov 29 '18 at 1:29
Is it a design time error only, or do you have the issue at runtime?
– Pragmateek
Nov 29 '18 at 1:36
This worked! The error was only design time, and it disappeared once I tried running it. Everything works now, I think. I will resume working on my code and let you know if everything works out! Thank you so much
– MichaelReese's
Nov 29 '18 at 2:27
I did not do exactly this because my classes work a little differently, but the idea of everything worked out. Thanks!
– MichaelReese's
Nov 29 '18 at 2:29
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53529467%2fcannot-go-from-one-page-to-a-new-page-while-sending-values-in-wpf-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't have a Page
inside a Page
.
Instead you should let their common parent component handles the transition using events:
Pokemon
:
public class Pokemon
{
public string Name { get; set; }
}
PkmnSelect
<Page x:Class="WpfApp1.PkmnSelect"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="PkmnSelect">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Name="FirstPokemonList">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Label Grid.Column="1" VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold">VS</Label>
<ListBox Name="SecondPokemonList" Grid.Column="2" Margin="0,1,0,19" Grid.RowSpan="2">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Button Click="Button_Click" Grid.Row="1" Grid.ColumnSpan="3">FIGHT!</Button>
</Grid>
</Page>
public class PokemonsSelectedEventArgs : EventArgs
{
public Pokemon FirstPokemon { get; set; }
public Pokemon SecondPokemon { get; set; }
}
public partial class PkmnSelect : Page
{
public event EventHandler<PokemonsSelectedEventArgs> PokemonsSelected = delegate { };
public PkmnSelect()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PokemonsSelected(this, new PokemonsSelectedEventArgs
{
FirstPokemon = new Pokemon { Name = (string)FirstPokemonList.SelectedValue },
SecondPokemon = new Pokemon { Name = (string)SecondPokemonList.SelectedValue }
});
}
}
Battle
:
<Page x:Class="WpfApp1.Battle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Battle">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding UserPokemon.Name}" FontSize="40" FontWeight="ExtraBold"></Label>
<Label VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold" Grid.Column="1">VS</Label>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding OpponentPokemon.Name}" FontSize="40" FontWeight="ExtraBold" Grid.Column="2"></Label>
</Grid>
</Page>
public partial class Battle : Page
{
public Pokemon UserPokemon { get; set; }
public Pokemon OpponentPokemon { get; set; }
public Battle()
{
InitializeComponent();
DataContext = this;
}
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
UserPokemon = userPkmn;
OpponentPokemon = opponentPkmn;
}
}
MainWindow
:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Title="MainWindow" Height="450" Width="800">
<local:PkmnSelect PokemonsSelected="PkmnSelect_PokemonsSelected"></local:PkmnSelect>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void PkmnSelect_PokemonsSelected(object sender, PokemonsSelectedEventArgs e)
{
Content = new Battle(e.FirstPokemon, e.SecondPokemon);
}
}
1
I’ll try this as soon as possible! Thank you
– MichaelReese's
Nov 29 '18 at 0:07
I have a problem with this solution: my main window is giving me the following error: The member "PokemonSelected" is not recognized or is not accessible. I've been reading around and people are saying stuff about changing AnyCPU to x86... What should I do?
– MichaelReese's
Nov 29 '18 at 1:29
Is it a design time error only, or do you have the issue at runtime?
– Pragmateek
Nov 29 '18 at 1:36
This worked! The error was only design time, and it disappeared once I tried running it. Everything works now, I think. I will resume working on my code and let you know if everything works out! Thank you so much
– MichaelReese's
Nov 29 '18 at 2:27
I did not do exactly this because my classes work a little differently, but the idea of everything worked out. Thanks!
– MichaelReese's
Nov 29 '18 at 2:29
|
show 1 more comment
You can't have a Page
inside a Page
.
Instead you should let their common parent component handles the transition using events:
Pokemon
:
public class Pokemon
{
public string Name { get; set; }
}
PkmnSelect
<Page x:Class="WpfApp1.PkmnSelect"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="PkmnSelect">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Name="FirstPokemonList">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Label Grid.Column="1" VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold">VS</Label>
<ListBox Name="SecondPokemonList" Grid.Column="2" Margin="0,1,0,19" Grid.RowSpan="2">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Button Click="Button_Click" Grid.Row="1" Grid.ColumnSpan="3">FIGHT!</Button>
</Grid>
</Page>
public class PokemonsSelectedEventArgs : EventArgs
{
public Pokemon FirstPokemon { get; set; }
public Pokemon SecondPokemon { get; set; }
}
public partial class PkmnSelect : Page
{
public event EventHandler<PokemonsSelectedEventArgs> PokemonsSelected = delegate { };
public PkmnSelect()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PokemonsSelected(this, new PokemonsSelectedEventArgs
{
FirstPokemon = new Pokemon { Name = (string)FirstPokemonList.SelectedValue },
SecondPokemon = new Pokemon { Name = (string)SecondPokemonList.SelectedValue }
});
}
}
Battle
:
<Page x:Class="WpfApp1.Battle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Battle">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding UserPokemon.Name}" FontSize="40" FontWeight="ExtraBold"></Label>
<Label VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold" Grid.Column="1">VS</Label>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding OpponentPokemon.Name}" FontSize="40" FontWeight="ExtraBold" Grid.Column="2"></Label>
</Grid>
</Page>
public partial class Battle : Page
{
public Pokemon UserPokemon { get; set; }
public Pokemon OpponentPokemon { get; set; }
public Battle()
{
InitializeComponent();
DataContext = this;
}
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
UserPokemon = userPkmn;
OpponentPokemon = opponentPkmn;
}
}
MainWindow
:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Title="MainWindow" Height="450" Width="800">
<local:PkmnSelect PokemonsSelected="PkmnSelect_PokemonsSelected"></local:PkmnSelect>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void PkmnSelect_PokemonsSelected(object sender, PokemonsSelectedEventArgs e)
{
Content = new Battle(e.FirstPokemon, e.SecondPokemon);
}
}
1
I’ll try this as soon as possible! Thank you
– MichaelReese's
Nov 29 '18 at 0:07
I have a problem with this solution: my main window is giving me the following error: The member "PokemonSelected" is not recognized or is not accessible. I've been reading around and people are saying stuff about changing AnyCPU to x86... What should I do?
– MichaelReese's
Nov 29 '18 at 1:29
Is it a design time error only, or do you have the issue at runtime?
– Pragmateek
Nov 29 '18 at 1:36
This worked! The error was only design time, and it disappeared once I tried running it. Everything works now, I think. I will resume working on my code and let you know if everything works out! Thank you so much
– MichaelReese's
Nov 29 '18 at 2:27
I did not do exactly this because my classes work a little differently, but the idea of everything worked out. Thanks!
– MichaelReese's
Nov 29 '18 at 2:29
|
show 1 more comment
You can't have a Page
inside a Page
.
Instead you should let their common parent component handles the transition using events:
Pokemon
:
public class Pokemon
{
public string Name { get; set; }
}
PkmnSelect
<Page x:Class="WpfApp1.PkmnSelect"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="PkmnSelect">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Name="FirstPokemonList">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Label Grid.Column="1" VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold">VS</Label>
<ListBox Name="SecondPokemonList" Grid.Column="2" Margin="0,1,0,19" Grid.RowSpan="2">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Button Click="Button_Click" Grid.Row="1" Grid.ColumnSpan="3">FIGHT!</Button>
</Grid>
</Page>
public class PokemonsSelectedEventArgs : EventArgs
{
public Pokemon FirstPokemon { get; set; }
public Pokemon SecondPokemon { get; set; }
}
public partial class PkmnSelect : Page
{
public event EventHandler<PokemonsSelectedEventArgs> PokemonsSelected = delegate { };
public PkmnSelect()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PokemonsSelected(this, new PokemonsSelectedEventArgs
{
FirstPokemon = new Pokemon { Name = (string)FirstPokemonList.SelectedValue },
SecondPokemon = new Pokemon { Name = (string)SecondPokemonList.SelectedValue }
});
}
}
Battle
:
<Page x:Class="WpfApp1.Battle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Battle">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding UserPokemon.Name}" FontSize="40" FontWeight="ExtraBold"></Label>
<Label VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold" Grid.Column="1">VS</Label>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding OpponentPokemon.Name}" FontSize="40" FontWeight="ExtraBold" Grid.Column="2"></Label>
</Grid>
</Page>
public partial class Battle : Page
{
public Pokemon UserPokemon { get; set; }
public Pokemon OpponentPokemon { get; set; }
public Battle()
{
InitializeComponent();
DataContext = this;
}
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
UserPokemon = userPkmn;
OpponentPokemon = opponentPkmn;
}
}
MainWindow
:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Title="MainWindow" Height="450" Width="800">
<local:PkmnSelect PokemonsSelected="PkmnSelect_PokemonsSelected"></local:PkmnSelect>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void PkmnSelect_PokemonsSelected(object sender, PokemonsSelectedEventArgs e)
{
Content = new Battle(e.FirstPokemon, e.SecondPokemon);
}
}
You can't have a Page
inside a Page
.
Instead you should let their common parent component handles the transition using events:
Pokemon
:
public class Pokemon
{
public string Name { get; set; }
}
PkmnSelect
<Page x:Class="WpfApp1.PkmnSelect"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="PkmnSelect">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Name="FirstPokemonList">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Label Grid.Column="1" VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold">VS</Label>
<ListBox Name="SecondPokemonList" Grid.Column="2" Margin="0,1,0,19" Grid.RowSpan="2">
<system:String>Pikachu</system:String>
<system:String>Raichu</system:String>
<system:String>Rattata</system:String>
</ListBox>
<Button Click="Button_Click" Grid.Row="1" Grid.ColumnSpan="3">FIGHT!</Button>
</Grid>
</Page>
public class PokemonsSelectedEventArgs : EventArgs
{
public Pokemon FirstPokemon { get; set; }
public Pokemon SecondPokemon { get; set; }
}
public partial class PkmnSelect : Page
{
public event EventHandler<PokemonsSelectedEventArgs> PokemonsSelected = delegate { };
public PkmnSelect()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PokemonsSelected(this, new PokemonsSelectedEventArgs
{
FirstPokemon = new Pokemon { Name = (string)FirstPokemonList.SelectedValue },
SecondPokemon = new Pokemon { Name = (string)SecondPokemonList.SelectedValue }
});
}
}
Battle
:
<Page x:Class="WpfApp1.Battle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Battle">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding UserPokemon.Name}" FontSize="40" FontWeight="ExtraBold"></Label>
<Label VerticalContentAlignment="Center" FontSize="20" FontWeight="ExtraBold" Grid.Column="1">VS</Label>
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{Binding OpponentPokemon.Name}" FontSize="40" FontWeight="ExtraBold" Grid.Column="2"></Label>
</Grid>
</Page>
public partial class Battle : Page
{
public Pokemon UserPokemon { get; set; }
public Pokemon OpponentPokemon { get; set; }
public Battle()
{
InitializeComponent();
DataContext = this;
}
public Battle(Pokemon userPkmn, Pokemon opponentPkmn) : this()
{
UserPokemon = userPkmn;
OpponentPokemon = opponentPkmn;
}
}
MainWindow
:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Title="MainWindow" Height="450" Width="800">
<local:PkmnSelect PokemonsSelected="PkmnSelect_PokemonsSelected"></local:PkmnSelect>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void PkmnSelect_PokemonsSelected(object sender, PokemonsSelectedEventArgs e)
{
Content = new Battle(e.FirstPokemon, e.SecondPokemon);
}
}
answered Nov 28 '18 at 23:58
PragmateekPragmateek
9,38185590
9,38185590
1
I’ll try this as soon as possible! Thank you
– MichaelReese's
Nov 29 '18 at 0:07
I have a problem with this solution: my main window is giving me the following error: The member "PokemonSelected" is not recognized or is not accessible. I've been reading around and people are saying stuff about changing AnyCPU to x86... What should I do?
– MichaelReese's
Nov 29 '18 at 1:29
Is it a design time error only, or do you have the issue at runtime?
– Pragmateek
Nov 29 '18 at 1:36
This worked! The error was only design time, and it disappeared once I tried running it. Everything works now, I think. I will resume working on my code and let you know if everything works out! Thank you so much
– MichaelReese's
Nov 29 '18 at 2:27
I did not do exactly this because my classes work a little differently, but the idea of everything worked out. Thanks!
– MichaelReese's
Nov 29 '18 at 2:29
|
show 1 more comment
1
I’ll try this as soon as possible! Thank you
– MichaelReese's
Nov 29 '18 at 0:07
I have a problem with this solution: my main window is giving me the following error: The member "PokemonSelected" is not recognized or is not accessible. I've been reading around and people are saying stuff about changing AnyCPU to x86... What should I do?
– MichaelReese's
Nov 29 '18 at 1:29
Is it a design time error only, or do you have the issue at runtime?
– Pragmateek
Nov 29 '18 at 1:36
This worked! The error was only design time, and it disappeared once I tried running it. Everything works now, I think. I will resume working on my code and let you know if everything works out! Thank you so much
– MichaelReese's
Nov 29 '18 at 2:27
I did not do exactly this because my classes work a little differently, but the idea of everything worked out. Thanks!
– MichaelReese's
Nov 29 '18 at 2:29
1
1
I’ll try this as soon as possible! Thank you
– MichaelReese's
Nov 29 '18 at 0:07
I’ll try this as soon as possible! Thank you
– MichaelReese's
Nov 29 '18 at 0:07
I have a problem with this solution: my main window is giving me the following error: The member "PokemonSelected" is not recognized or is not accessible. I've been reading around and people are saying stuff about changing AnyCPU to x86... What should I do?
– MichaelReese's
Nov 29 '18 at 1:29
I have a problem with this solution: my main window is giving me the following error: The member "PokemonSelected" is not recognized or is not accessible. I've been reading around and people are saying stuff about changing AnyCPU to x86... What should I do?
– MichaelReese's
Nov 29 '18 at 1:29
Is it a design time error only, or do you have the issue at runtime?
– Pragmateek
Nov 29 '18 at 1:36
Is it a design time error only, or do you have the issue at runtime?
– Pragmateek
Nov 29 '18 at 1:36
This worked! The error was only design time, and it disappeared once I tried running it. Everything works now, I think. I will resume working on my code and let you know if everything works out! Thank you so much
– MichaelReese's
Nov 29 '18 at 2:27
This worked! The error was only design time, and it disappeared once I tried running it. Everything works now, I think. I will resume working on my code and let you know if everything works out! Thank you so much
– MichaelReese's
Nov 29 '18 at 2:27
I did not do exactly this because my classes work a little differently, but the idea of everything worked out. Thanks!
– MichaelReese's
Nov 29 '18 at 2:29
I did not do exactly this because my classes work a little differently, but the idea of everything worked out. Thanks!
– MichaelReese's
Nov 29 '18 at 2:29
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53529467%2fcannot-go-from-one-page-to-a-new-page-while-sending-values-in-wpf-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you show the code of Battle?
– Nikolaus
Nov 28 '18 at 23:39
Sure! @Nikolaus The following code is the beginning of Battle, everything but the rest of the methods. justpaste.it/37nlv
– MichaelReese's
Nov 28 '18 at 23:45
@Nikolaus I also posted it in the question (couldn't fit in the comment)
– MichaelReese's
Nov 28 '18 at 23:50