c# - ToolbarItems 计数在视图模型中为 0
问题描述
我正在尝试从我的视图模型中绑定工具栏项中的徽章计数。
但是每次 Toolbaritem 计数在我的视图模型中为 0。当我在后端 c# 中尝试相同的代码时,我的计数为 1。
菜单页.xml
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Views.MenuPage"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
xmlns:views="clr-namespace:MyApp.Views"
BarBackgroundColor="White"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarSelectedItemColor=" #388e3c"
android:TabbedPage.IsSwipePagingEnabled="False">
<TabbedPage.ToolbarItems>
<ToolbarItem Name="MenuItem1" Order="Primary" Icon="ic_notifications_active_white.png" Text="Item 1" Priority="0" Command="{Binding NotificationsNavigateCommand}" />
</TabbedPage.ToolbarItems>
<NavigationPage.TitleView>
<StackLayout>
<Image Source="dropdown.png" VerticalOptions="Center" HeightRequest="25"/>
</StackLayout>
</NavigationPage.TitleView>
<views:A Title="A" Icon="outline_home_black_18dp.png" />
<TabbedPage Title="status" Icon="icons8_bar_chart.png">
<views:B Title="B"></views:B>
<views:C Title="C"></views:C>
</TabbedPage>
</TabbedPage>
menupageviewmodel.cs
public class MenuPageViewModel : ContentPage, INavigatedAware
{
public MenuPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
LoadCount();
}
private async void LoadCount()
{
if (ToolbarItems.Count > 0)
try
{
var toolbarservice = DependencyService.Get<IToolbarItemBadgeService>();
toolbarservice.SetBadge(this, ToolbarItems.First(), "1", Color.Red, Color.White);
}
catch (Exception ex)
{
throw ex;
}
}
public void OnNavigatedFrom(INavigationParameters parameters)
{
throw new NotImplementedException();
}
public void OnNavigatedTo(INavigationParameters parameters)
{
//if (ToolbarItems.Count > 0)
//{
// try
// {
// //ToolbarItems =new MenuPage.ToolbarItems();
// var toolbarservice = DependencyService.Get<IToolbarItemBadgeService>();
// toolbarservice.SetBadge(this, ToolbarItems.First(), "1", Color.Red, Color.White);
// }
// catch (Exception ex)
// {
// throw ex;
// }
//}
}
}
我认为它的加载 pblm 我的视图是在 viewmodel 之后加载的。我也尝试了 prism OnNavigatedFrom 但结果相同。与加载相关的另一个问题是,我的应用程序需要更多时间打开,因为每个页面的视图模型都在命中。如何仅限制在应用打开时加载第一个选项卡详细信息页面
解决方案
我猜你项目的架构一定是这样的:
await NavigationService.NavigateAsync("NavigationPage/MenuPage");
使您的根选项卡式页面被基本导航页面包裹。我不建议您使用此层次结构。它将使您的所有页面显示与ToolbarItems
您在根选项卡式页面上创建的相同。
但是如果你确实需要使用它,你可以让你的视图模型实现IPageLifecycleAware
并在那里得到计数:
public class MenuPageViewModel : ViewModelBase, IPageLifecycleAware
{
public MenuPageViewModel(INavigationService navigationService)
: base(navigationService)
{
//LoadCount();
}
private async void LoadCount()
{
NavigationPage navigationPage = Application.Current.MainPage as NavigationPage;
var tabbpedPage = navigationPage.Navigation.NavigationStack.FirstOrDefault();
var count = tabbpedPage.ToolbarItems.Count;
if (count > 0)
try
{
var toolbarservice = DependencyService.Get<IToolbarItemBadgeService>();
toolbarservice.SetBadge(tabbpedPage, tabbpedPage.ToolbarItems.FirstOrDefault(), "1", Color.Red, Color.White);
}
catch (Exception ex)
{
throw ex;
}
}
public void OnAppearing()
{
LoadCount();
}
public void OnDisappearing()
{
}
}
但是,我仍然不建议您在视图模型中访问与视图相关的内容。此外,不要让您的视图模型从 ContentView 继承,这是没有意义的。
推荐阅读
- vba - 如果查询已经存在,则删除整个查询
- .net - 令人困惑的“找不到服务器或无法访问服务器。” 错误,但仅适用于某些用户
- sql - 行级安全性:相同的查询在测试环境中运行的时间更短,并且在 PROD 中执行的时间很长
- angular - 为 Cloud Functions 执行函数部署时出错
- python-3.x - 使用熊猫不改变格式的平均时间
- react-admin -
:如何根据 react-admin 的 Edit 组件中的记录数据填充选项? - api - 使用 Docker API 创建容器时限制 CPU 使用率
- sql - 将 SAP txt 文件导入 SQL Server Express 2017
- ruamel.yaml - 如何在 buildroot 环境中安装 ruamel.yaml
- r - 创建 ifelse 函数时出错