[英]Hiding a tab in Xamarin.Forms using Prism framework
I am working on a Xamarin Forms project that uses an MVVM pattern and Prism. 我正在开发一个使用MVVM模式和Prism的Xamarin Forms项目。
My issue is, I need to be able to hide a tab on a tab page based off of a bool. 我的问题是,我需要能够根据bool隐藏标签页上的标签。 There is a bindable property in the XAML called "IsVisible", which I assumed would hide the tab from the user, but instead the tab still shows but displays a blank page when selected. 在XAML中有一个名为“IsVisible”的可绑定属性,我假设该属性会隐藏用户的选项卡,但选项卡仍会显示,但在选中时会显示空白页面。
Currently my XAML looks like this 目前我的XAML看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:local="clr-namespace:com.XXXXXXX"
prism:ViewModelLocator.AutowireViewModel="True"
BackgroundColor="White"
x:Class="com.XXXXXXXXXX.EditChargePage"
Title="{Binding Title}">
<local:EditChargeDetailsPage Title="Details" />
<local:EditChargeTrackingPage Title="Tracking" IsVisible="{Binding TabContext.HasTracking}" IsEnabled="{Binding TabContext.HasTracking}"/>
<local:EditChargeNotesPage Title="Notes" />
</TabbedPage>
Originally to solve the problem I just used "IsVisible", when that didn't work as expected I added "IsEnabled" which didn't seem to do anything. 最初是为了解决我刚刚使用“IsVisible”的问题,当它没有按预期工作时,我添加了“IsEnabled”,似乎没有做任何事情。
Is there a way to hide the Tab from the UI using a boolean value in Xamarin (or Prism for Xamarin) without breaking MVVM? 有没有办法在不破坏MVVM的情况下使用Xamarin(或Prism for Xamarin)中的布尔值从UI中隐藏Tab?
You might want to check out the Prism Samples . 您可能想查看Prism Samples 。 The TabbedNavigation
sample demonstrates how to initialize your tabbed children with INavigatingAware
or the IEventAggregator
, as well as dynamically adding the tabs at run time. TabbedNavigation
示例演示了如何使用INavigatingAware
或IEventAggregator
初始化选项卡式子IEventAggregator
,以及在运行时动态添加选项卡。 If you need even finer control, such as a value in a ViewModel of one of your Child Pages that determines whether some other page should be shown, then you could use this to publish an event with the IEventAggregator
and subscribe to that event in your TabbedPage to handle adding or removing the page. 如果您需要更精细的控制,例如您的某个子页面的ViewModel中的值,以确定是否应显示其他页面,那么您可以使用它来使用IEventAggregator
发布事件并在TabbedPage中订阅该事件处理添加或删除页面。
Unfortunately IsVisible
and IsEnabled
do not actually work within a TabbedPage the way you were hoping. 不幸的是, IsVisible
和IsEnabled
实际上并不像你希望的那样在TabbedPage中工作。 So the only way you can accomplish this is to actually manipulate the Children of the TabbedPage directly pushing or popping them from the collection. 因此,实现此目的的唯一方法是实际操作TabbedPage的Children,直接从集合中推送或弹出它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.