Xamarin - 工具栏未显示在表单上

时间:2016-04-12 17:35:12

标签: c# xamarin

我遇到了在表单上显示工具栏的问题。表单是ConfigurationPage.cs。我包括导航到ConfigurationPage和页面本身的页面。知道工具栏没有显示的原因吗?

App部分

public App()
{
    // The root page of your application
    MainPage = new NavigationPage(new MainPage());
}

主页

public class MainPage : ContentPage
{
    public MainPage()
    {
        BackgroundColor = Color.White;
        var setup = new Button
        {
            Text = "Lane Configuration",
            TextColor = Color.Black
        };

        setup.Clicked += (sender, args) =>
        {
            Navigation.PushModalAsync(new ConfigurationPage());
        };

        var gateGridLayout = new Grid
        {
            Padding = new Thickness(5),
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            RowDefinitions = {new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) }},
            ColumnDefinitions = {new ColumnDefinition{ Width = GridLength.Auto }}
        };

        gateGridLayout.Children.Add(setup, 0, 0);
        Content = gateGridLayout;
    }
}

配置页面 - 工具栏未在此处显示

public class ConfigurationPage : ContentPage
{
    public event EventHandler SaveToDatabaseCompleted;
    public ConfigurationPage()
    {
        BackgroundColor = Color.White;

        var viewModel = new ConfigurationViewModel(this);
        BindingContext = viewModel;

        var lblIPAddress = new Label
        {
            Text = "IP Address",
            TextColor = Color.Black
        };

        var IPAddress = new Entry
        {
            Text = string.Empty,
            TextColor = Color.White,
            BackgroundColor = Color.Blue,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        IPAddress.SetBinding(Entry.TextProperty, "IPAddress");

        var lblUserName = new Label
        {
            Text = "UserName",
            TextColor = Color.Black
        };

        var UserName = new Entry
        {
            Text = string.Empty,
            TextColor = Color.White,
            BackgroundColor = Color.Blue,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        UserName.SetBinding(Entry.TextProperty, "UserName");

        var lblPassword = new Label
        {
            Text = "Password",
            TextColor = Color.Black
        };

        var Password = new Entry
        {
            Text = string.Empty,
            TextColor = Color.White,
            BackgroundColor = Color.Blue,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        Password.SetBinding(Entry.TextProperty, "Password");

        var lblXml = new Label
        {
            Text = "XML Page",
            TextColor = Color.Black,
        };

        Picker picker = new Picker
        {
            Title = "XML Settings",
            BackgroundColor = Color.Blue,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        var options = new List<string> { "val1.xml", "val2.xml" };

        foreach (string optionName in options)
        {
            picker.Items.Add(optionName);
        }

        string selected = string.Empty;

        var lblXMLEntry = new Label
        {
            Text = "Selected XML Value",
            TextColor = Color.Black
        };

        var XML = new Entry
        {
            IsEnabled = false,
            Text = selected,
            TextColor = Color.White,
            BackgroundColor = Color.Blue,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        XML.SetBinding(Entry.TextProperty, "XML");

        picker.SelectedIndexChanged += (sender, args) =>
        {
            if (picker.SelectedIndex == 0)
                selected = picker.Items[0];
            else if (picker.SelectedIndex == 1)
                selected = picker.Items[1];

            XML.Text = selected;
        };

        var IPAddressLblStack = new StackLayout
        {
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Orientation = StackOrientation.Horizontal,

            Children = {
                lblIPAddress
            }
        };

        var UserNameLblStack = new StackLayout
        {
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Orientation = StackOrientation.Horizontal,

            Children = {
                lblUserName
            }
        };

        var PasswordLblStack = new StackLayout
        {
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Orientation = StackOrientation.Horizontal,

            Children = {
                lblPassword
            }
        };

        var XMLLblStack = new StackLayout
        {
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Orientation = StackOrientation.Horizontal,

            Children = {
                lblXml
            }
        };

        var PickerStack = new StackLayout
        {
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Orientation = StackOrientation.Horizontal,

            Children = {
                picker
            }
        };

        var XMLLblEntry = new StackLayout
        {
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Orientation = StackOrientation.Horizontal,

            Children = {
                lblXMLEntry
            }
        };

        var gateGridLayout = new Grid
        {
            Padding = new Thickness(5),
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.CenterAndExpand,

            RowDefinitions = {
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) },
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) },
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) },
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) },
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) },
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) },
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) },
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) },
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) },
                new RowDefinition{ Height = new GridLength (1, GridUnitType.Auto) }
            },
            ColumnDefinitions = {
                new ColumnDefinition{ Width = GridLength.Auto }//,
            }
        };

        gateGridLayout.Children.Add(IPAddressLblStack, 0, 1);
        gateGridLayout.Children.Add(IPAddress, 0, 2);
        gateGridLayout.Children.Add(UserNameLblStack, 0, 3);
        gateGridLayout.Children.Add(UserName, 0, 4);
        gateGridLayout.Children.Add(PasswordLblStack, 0, 5);
        gateGridLayout.Children.Add(Password, 0, 6);
        gateGridLayout.Children.Add(XMLLblStack, 0, 7);
        gateGridLayout.Children.Add(PickerStack, 0, 8);
        gateGridLayout.Children.Add(XMLLblEntry, 0, 9);
        gateGridLayout.Children.Add(XML, 0, 10);

        var saveButtonToolbar = new ToolbarItem();
        saveButtonToolbar.Text = "Save";
        saveButtonToolbar.SetBinding(ToolbarItem.CommandProperty, "SaveButtonTapped");
        saveButtonToolbar.Priority = 0;
        ToolbarItems.Add(saveButtonToolbar);

        var cancelButtonToolbar = new ToolbarItem();
        cancelButtonToolbar.Text = "Cancel";
        cancelButtonToolbar.Command = new Command(async () => await PopModalAsync(true));
        cancelButtonToolbar.Priority = 1;
        ToolbarItems.Add(cancelButtonToolbar);

        Content = gateGridLayout;
    }

    public void HandleSaveToDatabaseCompleted(object sender, EventArgs e)
    {
        if (SaveToDatabaseCompleted != null)
            SaveToDatabaseCompleted(this, new EventArgs());
    }

    public async Task PopModalAsync(bool isAnimated)
    {
        await Navigation.PopModalAsync(true);
    }
}

1 个答案:

答案 0 :(得分:1)

想出来。 MainPage setup.clicked事件需要更改为以下代码。

setup.Clicked += async (sender, args) =>
{
    await Navigation.PushModalAsync(new NavigationPage(new  ConfigurationPage()));
};