如何在xamarin中隐藏导航工具栏图标?
我想在xamarin中隐藏导航栏按钮。如何使用绑定来实现这一点。工具栏项没有“IsVisible”属性 下面是我的xaml代码如何在xamarin中隐藏导航工具栏图标?,xamarin,xamarin.forms,xamarin.ios,xamarin.android,Xamarin,Xamarin.forms,Xamarin.ios,Xamarin.android,我想在xamarin中隐藏导航栏按钮。如何使用绑定来实现这一点。工具栏项没有“IsVisible”属性 下面是我的xaml代码 请帮我解决这个问题。正如您发现的那样,是不可见的。因此,如果您仍然需要,您必须自己实现这样的功能 另一种方法是在页面的代码隐藏中处理它,并在需要时删除或添加工具栏项 添加和删除很简单,只需将项目添加和删除到ToolbarItems集合:ToolbarItems.RemoveAt(0)将删除第一个工具栏项。我建议构建一个可绑定的ToolBoxItem。这样,可以通过视图
请帮我解决这个问题。正如您发现的那样,
是不可见的。因此,如果您仍然需要,您必须自己实现这样的功能
另一种方法是在页面的代码隐藏中处理它,并在需要时删除或添加工具栏项
添加和删除很简单,只需将项目添加和删除到ToolbarItems
集合:ToolbarItems.RemoveAt(0)例如,代码>将删除第一个工具栏项。我建议构建一个可绑定的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); });
}
}
}
我们需要前端的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);
}
};
将@Gerald answer付诸行动,可以这样做:
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);
}
}
这更简洁,可以从代码后面工作。我使用重载构造函数轻松实现了这一点。下面是一个例子:
查看(添加名称属性):
上述伪代码将在没有参数的情况下实例化类时添加“保存”工具栏项,或者在提供参数时添加“更新”和“删除”
这并不像IsEnabled/IsVisible booleans那样优雅,但这是朝着正确方向迈出的一步。按照这种思路,您可以在运行时将工具栏的子项修改为“显示”和“隐藏”,方法是将它们作为子项添加和删除
祝你好运 更新了我的答案。这不是火箭科学,甚至不是Xamarin特有的ToolbarItems
只是一个你可以访问的数组。对不起,我不知道你的意思是什么,我在xaml中创建了x:name=“toolbaritem”,然后访问cs文件,但没有像“RemoveAt”这样的属性。如何从模型中隐藏工具栏项。在模型中,我得到一个Web服务响应,然后我想根据响应隐藏编辑按钮。我想使用iVisable={binding showItems}之类的绑定隐藏工具栏按钮。是否要在某些条件下删除工具栏项,并在其他情况下显示?是…@G.hakim非常感谢!这是一个非常好的解决方案。但是,请记住,默认可见性为“True”,这意味着您可以使用类似IsVisible=“{binding ShowToolbarItem}”的绑定将其从可见切换到隐藏。使用这种方法,您不能从页面生命周期的一开始就隐藏工具栏项(至少不会闪烁)。
<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
}