簡體   English   中英

如何僅使用 c# 導航到 xamarin 表單中的新頁面

[英]How to navigate to a new page in xamarin forms using ONLY c#

我正在開發一個簡單的應用程序,它可以跟蹤紙牌游戲的生活。 我對 xamarin 很陌生,所以我從小處着手,然后慢慢添加更多功能。 目前,我有兩頁; 一頁(它開始的頁面(根頁面?)只有一個生命總數,兩個用於增加和減少的按鈕,一個用於切換到雙人游戲布局的按鈕,然后是帶有兩個生命總數和 4 個按鈕的第二頁(每個生命總數的增量和減量)。我正在用 C# 編寫所有這些,我想保持這種方式,但是,我無法找到一種方法來實現它,以便切換到兩個播放器布局的按鈕將呈現第二頁。我用谷歌搜索的所有內容似乎都指向我想避免的 xml。誰能幫我理解如何做到這一點?

我正在構建我的好友為了解 xamarin 的工作原理而制作的應用程序,所以這就是所有奇怪的評論

代碼:(我需要填寫的代表在底部,稱為moreplayers)

namespace SampleApp
 {
//contentpage is the base class for all pages.
//You should make a base class for this page that isn't contentpage, but inherits from content page, then you can add custom methods that extend across all pages.
//Like adding a progress spinner, or disabling all UI elements.
public class MainPage : ContentPage
{

    public MainPage()
    {
        CreateUI();
    }

    private void CreateUI()
    {
        Stats Player1 = new Stats();

        Player1.LifeTotal = 20;

        //abstracting out a function to build UI is good, but breaking this down further is better.
        var MainGrid = new Grid()//grids are the bread and butter of xamarin forms, the documentation has lots of good examples I won't try to replicate here.
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,//these are on all UI elements, gotta specify them or the default values will probably screw up.
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        //I usually make a bunch of nice extensions on the Grid to add rows and columns easily
        MainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
        MainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });

        MainGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

        //grid where life total label will live
        var GridForLifeTotal = new Grid()
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };
        GridForLifeTotal.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
        GridForLifeTotal.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });

        GridForLifeTotal.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

        //grid where buttons will live
        var GridForButtons = new Grid()
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };
        GridForButtons.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
        GridForButtons.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });

        GridForButtons.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

        MainGrid.Children.Add(GridForLifeTotal, 0, 0); //add items to the grid based on position
        MainGrid.Children.Add(GridForButtons, 0, 1);

        //Add labels
        var lifeLabel = new Label()
        {
            Text = Player1.LifeTotal.ToString(),
            FontAttributes = FontAttributes.Bold,
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center,
            FontSize = 60
        };

        GridForLifeTotal.Children.Add(lifeLabel, 0, 0);

        //Add buttons
        var UpButton = new Button()
        {
            Text = "+",
            FontAttributes = FontAttributes.Bold,
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center,
            FontSize = 30
        };

        UpButton.Clicked += delegate {
            //delegates are bad form but it's late and I'm tired you should put this login in a view model class and have that view model be a private property on this view.
            //View (this), View Model (the logic layer) then a Model to hold the life total and any other user data?
            Player1.LifeTotal += 1;
            lifeLabel.Text = Player1.LifeTotal.ToString();
        };

        var DownButton = new Button()
        {
            Text = "-",
            FontAttributes = FontAttributes.Bold,
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center,
            FontSize = 30
        };

        DownButton.Clicked += delegate {
            //delegates are bad form but it's late and I'm tired
            Player1.LifeTotal -= 1;
            lifeLabel.Text = Player1.LifeTotal.ToString();
        };

        var MorePlayers = new Button()
        {
            Text = "2 Player Game",
            FontAttributes = FontAttributes.Bold,
            HorizontalOptions = LayoutOptions.End,
            VerticalOptions = LayoutOptions.End,
            FontSize = 30
        };

        MorePlayers.Clicked += delegate
        {
            //need to figure out what goes here
        };

        GridForButtons.Children.Add(UpButton, 0, 0);
        GridForButtons.Children.Add(DownButton, 1, 0);
        GridForButtons.Children.Add(MorePlayers, 0, 1);

        Content = MainGrid;//very important, otherwise you don't actually see anything you've built

    }

}

}

首先,當您第一次在App.xaml.cs分配MainPage時,您需要將MainPage包裝在NavigationPage

MainPage = new NavigationPage(new MainPage());

然后,導航到您的委托中的下一頁

this.Navigation.PushAsync(new Page2());

暫無
暫無

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

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