首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ToolbarItems计数在视图模型中为0

ToolbarItems计数在视图模型中为0
EN

Stack Overflow用户
提问于 2019-05-31 13:14:13
回答 1查看 321关注 0票数 0

我正在尝试从我的视图模型中绑定工具栏项中的徽章计数。

但每次工具栏项目计数在我的viewmodel.when中为0时,我会在后端c#中尝试相同的代码,我得到的计数为1。

Menupage.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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我的视图是加载后视图模型。我也尝试了棱镜OnNavigatedFrom,但结果是same.one更多有关加载的问题是,我的应用程序花了更多的时间打开,因为每个页面的视图模型是hitting.how,以限制只加载第一个标签页细节页面的应用程序打开

EN

回答 1

Stack Overflow用户

发布于 2019-06-03 11:02:11

我猜你的项目架构一定是这样的:

代码语言:javascript
复制
await NavigationService.NavigateAsync("NavigationPage/MenuPage");

使您的根选项卡式页面由基本导航页面包装。我不建议您使用此层次结构。它将使所有页面显示与您在根选项卡页上创建的相同的ToolbarItems

但是如果你确实需要使用它,你可以让你的视图模型实现IPageLifecycleAware并在那里获得计数:

代码语言:javascript
复制
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,这没有任何意义。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56389008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档