You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How to prevent my application from switching tabs if a certain condition is not met in .NET Maui?
0
How to prevent my application from switching tabs if a certain condition is not met in .NET Maui?
I need to verifiy a condition before tab navigates to the clicked tab. If the condition is not met, then stop the navigation.
Below is my Tab bar xaml code along with the on property changed C# code. I cannot get the
GetDeferral();
from my C# code
private void WorkOrderTabBar_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (sender is TabBar listener && listener.CurrentItem?.Navigation.NavigationStack.Count > 1)
{
//listener
var navigation = listener.CurrentItem.Navigation;
var pages = navigation.NavigationStack;
for (var i = pages.Count - 1; i >= 1; i--)
{
navigation.RemovePage(pages[i]);
}
}
}
public partial class AppShell : Shell
{
ShellViewModel shellVm;
public AppShell()
{
InitializeComponent();
BindingContext = shellVm = new ShellViewModel();
shellVm.IsEnabled1 = true;
shellVm.IsEnabled2 = true;
}
protected override void OnNavigating(ShellNavigatingEventArgs args)
{
base.OnNavigating(args);
if (args.Source == ShellNavigationSource.ShellSectionChanged)
{
var aCondition = true;
if (aCondition)
{
shellVm.IsEnabled1 = false;
}
}
}
}
This is the view model
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace dap111 //put in your namespace
{
public class ShellViewModel : INotifyPropertyChanged
{
public bool _IsEnabled1;
public bool IsEnabled1
{
get => _IsEnabled1;
set
{
if (_IsEnabled1 != value)
{
_IsEnabled1 = value;
OnPropertyChanged(); // reports this property
}
}
}
public bool _IsEnabled2;
public bool IsEnabled2
{
get => _IsEnabled2;
set
{
if (_IsEnabled2 != value)
{
_IsEnabled2 = value;
OnPropertyChanged(); // reports this property
}
}
}
public ShellViewModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
This discussion was converted from issue #24040 on August 06, 2024 16:22.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
How to prevent my application from switching tabs if a certain condition is not met in .NET Maui?
0
How to prevent my application from switching tabs if a certain condition is not met in .NET Maui?
I need to verifiy a condition before tab navigates to the clicked tab. If the condition is not met, then stop the navigation.
Below is my Tab bar xaml code along with the on property changed C# code. I cannot get the
GetDeferral();
from my C# code
private void WorkOrderTabBar_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (sender is TabBar listener && listener.CurrentItem?.Navigation.NavigationStack.Count > 1)
{
//listener
var navigation = listener.CurrentItem.Navigation;
var pages = navigation.NavigationStack;
This is the App Shell XAML:
This is the App Shell code behind:
namespace dap111; //put in your namespace
public partial class AppShell : Shell
{
ShellViewModel shellVm;
}
This is the view model
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace dap111 //put in your namespace
{
public class ShellViewModel : INotifyPropertyChanged
{
public bool _IsEnabled1;
public bool IsEnabled1
{
get => _IsEnabled1;
set
{
if (_IsEnabled1 != value)
{
_IsEnabled1 = value;
OnPropertyChanged(); // reports this property
}
}
}
}
Beta Was this translation helpful? Give feedback.
All reactions