在WPF C#中发送值时无法从一页转到新页

时间:2018-11-28 23:07:10

标签: c# wpf window

在MainWindow.xaml.cs中,使用以下命令打开一个新页面(PkmnSelect):

PkmnSelect pkmnSelect = new PkmnSelect(); 
Content = pkmnSelect;

然后,一旦用户在此页面(PkmnSelect)中选择了神奇宝贝团队,他们就可以单击“开始”。 “开始”按钮具有以下代码:

Battle battle = new Battle(userPokemon, opponentPokemon);
Content = battle;

战斗是我想将两个Pokémon[]作为输入的页面,因此我在Battle中创建了一个额外的构造函数,如下所示:

public Battle(Pokemon[] userPkmn, Pokemon[] opponentPkmn) : this()
{
    userPokemon = userPkmn;
    opponentPokemon = opponentPkmn;
}

这给了我错误“页面只能将Window或Frame作为父级。”

我的问题是,将值从一页发送到另一页的正确方法是什么? 我到处都看过,没有尝试过。

编辑: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;
    }

1 个答案:

答案 0 :(得分:0)

$(document).ready(function() { $('#members').DataTable( { "scrollX": true } ); } ); 中不能有Page

相反,您应该让它们的公共父组件使用事件

Page

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