如何在xamarin中隐藏导航工具栏图标?

时间:2018-05-31 07:55:24

标签: xamarin xamarin.forms xamarin.ios xamarin.android

我想在xamarin中隐藏导航栏按钮。我怎么能用绑定做到这一点。工具栏项目没有“IsVisible”属性。

以下是我的xaml代码

Xaml code of navigation bar button

请帮我解决这个问题。

6 个答案:

答案 0 :(得分:5)

我建议构建一个可绑定的ToolBoxItem。这样,您就可以通过视图模型属性控制可见性。

实现可能如下所示:

public class BindableToolbarItem : ToolbarItem
{
    public static readonly BindableProperty IsVisibleProperty = BindableProperty.Create(nameof(IsVisible), typeof(bool), typeof(BindableToolbarItem), true, BindingMode.TwoWay, propertyChanged: OnIsVisibleChanged);

    public bool IsVisible
    {
        get => (bool)GetValue(IsVisibleProperty);
        set => SetValue(IsVisibleProperty, value);
    }

    private static void OnIsVisibleChanged(BindableObject bindable, object oldvalue, object newvalue)
    {
        var item = bindable as BindableToolbarItem;

        if (item == null || item.Parent == null)
            return;

        var toolbarItems = ((ContentPage)item.Parent).ToolbarItems;

        if ((bool)newvalue && !toolbarItems.Contains(item))
        {
            Device.BeginInvokeOnMainThread(() => { toolbarItems.Add(item); });
        }
        else if (!(bool)newvalue && toolbarItems.Contains(item))
        {
            Device.BeginInvokeOnMainThread(() => { toolbarItems.Remove(item); });
        }
    }
}

答案 1 :(得分:2)

正如您发现自己没有IsVisible。因此,如果您仍然需要,您必须自己实现这样的功能。

另一种方法是在页面中处理它。代码隐藏,并在需要时删除或添加工具栏项。

添加和删除很简单,只需在ToolbarItems集合中添加和删除项目:ToolbarItems.RemoveAt(0);例如将删除第一个工具栏项。

答案 2 :(得分:1)

将@Gerald答案放入行动中,将以这种方式完成:

void Done_Clicked(System.Object sender, System.EventArgs e)
{
    //Do somthing and hide the done item
    ShowDoneToolbarItem(false, (ToolbarItem)sender);
}

void Entry_Focused(System.Object sender, Xamarin.Forms.FocusEventArgs e)
{
    //Show the done item
    ShowDoneToolbarItem(true);
}

void ShowDoneToolbarItem(bool show, ToolbarItem item = null)
{
    if(show)
    {
        ToolbarItem done = new ToolbarItem();
        done.Text = "Done";
        done.Clicked += Done_Clicked;
        ToolbarItems.Add(done);
    }
    else if(item != null)
    {
        ToolbarItems.Remove(item);
    }
}

这是更干净的,并且可以从后面的代码中使用。

答案 3 :(得分:0)

通过阅读Microsoft文档,我发现了一种更快,更轻松的方法! 转到app uwp,找到app.xaml并输入以下代码行:

xvalues(j) = [xvalues(j) ,value(i)];

更多信息: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.appbarbutton https://docs.microsoft.com/pt-br/windows/uwp/design/controls-and-patterns/app-bars

答案 4 :(得分:0)

好吧,我们需要前端的IsVisible属性,因为xamarin没有它,您可以使用Device.RuntimePlatform实时检查应用程序正在运行的设备。由于我的代码位于XAML文件的.cs中,因此我们可以使用xaml .cs在屏幕上插入项目。我使用if()进行逻辑并检查我的设备是否在哪个平台上,因为我不想它可以在UWP中显示工具栏。 该代码位于XAML文件的.cs中:

public kingTest()
{
InitializeComponent();
if((Device.RuntimePlatform == "Android")||(Device.RuntimePlatform == "iOS"))
{
ToolbarItem toolbar = new ToolbarItem();
toolbar.IconImageSource = "ic_ToolBar.png";
this.ToolbarItems.Add(toolbar);
}

        };

答案 5 :(得分:0)

我已经使用重载的构造函数轻松实现了这一点。这是一个示例:

查看(添加名称属性):

<ContentPage x:Name="ContentPage"
    <!-- rest of the tag -->
    />

隐藏代码(添加工具栏项):

public partial class ExamplePage : ContentPage
{
    public ExamplePage()
    {
        InitializeComponent();
        BindingContext = this;

        var saveToolbarItem = new ToolbarItem { Text = "Save" };
        saveToolbarItem.Clicked += YourMethodToBeRan;

        ContentPage.ToolbarItems.Add(saveToolbarItem);
    }

    public ExamplePage(Object object)
    {
        InitializeComponent();
        BindingContext = this;

        var updateToolbarItem = new ToolbarItem { Text = "Update" };
        updateToolbarItem.Clicked += YourMethodToBeRan;

        var deleteToolbarItem = new ToolbarItem { Text = "Delete" };
        deleteToolbarItem.Clicked += YourMethodToBeRan;

        ContentPage.ToolbarItems.Add(updateToolbarItem);
        ContentPage.ToolbarItems.Add(deleteToolbarItem);
    }

    // rest of the class
}

上面的伪代码将在没有参数的情况下实例化类时添加“保存”工具栏项,或者在提供参数时添加“更新”和“删除”。

这不如IsEnabled / IsVisible布尔值那么优雅,但这是朝着正确方向迈出的一步。按照这种思路,您可以在运行时将工具栏的子级修改为“显示”和“隐藏”,方法是将其添加和删除为子级。

祝你好运!