如何在所有页面的NavigationBar中添加图标? [英] How can I add an icon to the NavigationBar in all pages?

查看:72
本文介绍了如何在所有页面的NavigationBar中添加图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在我的NavigationBar中实现一个图标,以便可以在我的所有页面中看到它...我正在使用xamarin表单,因此我希望它能够在android和iOS中都可以使用...我我不确定如何执行此操作,但是我试图将其添加到MyCar.xaml

So I'm trying to implement an icon in my NavigationBar, so it can be visible in all my pages... I'm using xamarin forms so I want it to be able in both android and IOS... I'm not sure how to do this, but I was trying to add this in my MyCar.xaml

<customControls:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
    xmlns:views="clr-namespace:OficinaDigitalX.Views"   
    xmlns:customControls="clr-namespace:OficinaDigitalX.ViewModel"
    x:Name="MyCar">
<customControls:BasePage.Content>
    <AbsoluteLayout>



        <StackLayout Padding="10, 0, 10, 0">


            <ListView 
                ItemsSource="{Binding Path=CarList}"
                IsPullToRefreshEnabled="False"
                SelectedItem="{Binding Path=SelectedCar}">
                <ListView.Header>
                    <Label Text="Os Meus Carros" FontSize="Large" />
                </ListView.Header>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding VID}" 
                                  TextColor="Black" 
                                  Detail="{Binding LicensePlate}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </AbsoluteLayout>
</customControls:BasePage.Content>
</customControls:BasePage>  

这是我的MyCar.xaml.cs

This is my MyCar.xaml.cs

namespace OficinaDigitalX.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyCar : ViewModel.BasePage
{

    public MyCar()
    {
        Extensions.LoadFromXaml(this, typeof(MyCar));
        BindingContext = new MyCarViewModel(Navigation);

    }

这是我的MyCarViewModel.cs

this is my MyCarViewModel.cs

public class MyCarViewModel : ViewModelBase
{
    public MyCarViewModel()
    {
    }
    public MyCarViewModel(INavigation navigation)
    {
        this.Navigation = navigation;
        this.SelectedCar = null;
        GetClientCars();
    }

    private List<CarInfo> _CarList;

    public List<CarInfo> CarList
    {
        get
        {
            return _CarList;
        }

        set
        {
            _CarList = value;
            OnPropertyChanged("CarList");
        }
    }

    private CarInfo _SelectedCar;

    public CarInfo SelectedCar
    {
        get
        {
            return _SelectedCar;
        }

        set
        {
            _SelectedCar = value;
            OnPropertyChanged("SelectedCar");

            if (_SelectedCar != null)
                ChangeWindow(_SelectedCar);
        }
    }

    public INavigation Navigation { get; set; }

    private void ChangeWindow(CarInfo car)
    {

        Navigation.PushAsync(new Interactions(car));
        this.SelectedCar = null;

    }

    public void GetClientCars()
    {
        string command = "asdasd";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(MainPage.server + command));
        request.ContentType = "application/json";
        request.Method = "POST";
        //request.ContentLength = 999999;

        using (var stream = new StreamWriter(request.GetRequestStream()))
        {
            string postData = JsonConvert.SerializeObject(command);

            //stream.Write(postData);
            stream.Flush();
            stream.Close();
        }

        HttpWebResponse response = null;

        try
        {
            response = (HttpWebResponse)request.GetResponse();
            using (var responseString = new StreamReader(response.GetResponseStream()))
            {
                CarList = JsonConvert.DeserializeObject<List<CarInfo>>(responseString.ReadToEnd());
            }
        }
        catch (WebException ex)
        {
            using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
            {

            }
            throw;
        }
    }

}
}

任何人都可以帮忙吗?

推荐答案

在我的知识中,这样做的正确方法是扩展内容页面:

The correct way of doing this in my Knowledge is extending the content page:

 public class BasePage : ContentPage
 {

    public ICommand CartItemCommand { get; set; }
    public ICommand NotificationPageCommand { get; set; }
    public BasePage() : base()
    {
        CartItemCommand = new Command(async () => await GoCartItemCommand());
        NotificationPageCommand = new Command(GoNotificationPage);

        Init();
    }

    private void Init()
    {
        this.ToolbarItems.Add(new ToolbarItem()
        {
            Icon = "nav_notification_btn",
            Command = NotificationPageCommand,
        });
        this.ToolbarItems.Add(new ToolbarItem()
        {
            Icon = "nav_cart_btn",
            Command = CartItemCommand
        });
    }

    }

}

完成此操作后,只需在位置ContentPage的任何地方使用此BasePage

And once you are done with that just use this BasePage everywhere in Place of ContentPage

在您的XAML中

<customControls:Basepage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
        xmlns:views="clr-namespace:OficinaDigitalX.Views"
        x:Class="OficinaDigitalX.MainPage"    
        xmlns:customControls="Your Control NameSpace"
        x:Name="Main"
        NavigationPage.HasNavigationBar="True">


</customControls:Basepage> 

并在您的Xaml.cs文件中

public partial class MainPage: BasePage

请确保这两个部分类都继承自一个基类,即根据您的需要从BasePage或ContentPage继承.

See to it that both partial classes inherit from one base class i.e BasePage or ContentPage as per your need.

当您不想使用NavBar控件时,只需继承XAML类 从正常的ContentPage中获取.

And when you do not want to use the NavBar controls just inherit your XAML classes from a Normal ContentPage.

在查询的情况下恢复好运!

Goodluck revert in case of queries!

这篇关于如何在所有页面的NavigationBar中添加图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆