簡體   English   中英

在WPF C#中發送值時無法從一頁轉到新頁

[英]Cannot go from one Page to a new Page while sending values in WPF C#

在我的MainWindow.xaml.cs中,使用以下命令打開一個新頁面(PkmnSelect):

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

然后,一旦用戶在此頁面(PkmnSelect)中選擇了神奇寶貝團隊,他們就可以單擊“開始”。 “開始”按鈕具有以下代碼:

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

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;
    }

Page內不能有Page

相反,您應該讓它們的公共父組件使用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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM