【问题标题】:ToolbarItems count is coming as 0 in viewmodelToolbarItems 计数在视图模型中为 0
【发布时间】:2019-10-16 17:52:29
【问题描述】:

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

但每次 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 但结果相同。另一个与加载相关的问题是,我的应用程序需要更多时间才能打开,因为每个页面的视图模型都在命中。如何限制仅在应用打开时加载第一个选项卡详细信息页面

【问题讨论】:

  • @TheGeneral 你能给出解决方案吗
  • DependencyService.Get&lt;IToolbarItemBadgeService&gt;() ... 大错特错
  • @Haukinger 我在每次获取 ToolbarItems.Count 时都面临问题。每次它都为 0。我正在尝试完全像 link 但他没有指定如何从 viewmodel 访问工具栏项

标签: c# mvvm xamarin.forms prism tabbedpage


【解决方案1】:

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

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,这没有任何意义。

【讨论】:

  • 就像你说的我在每个页面中都需要相同的工具栏项目。我认为你的代码会为我工作。但是如果不继承 viewmodel 中的内容视图,我怎样才能实现这个工具栏服务。SetBadge(这个, tabbpedPage.ToolbarItems.FirstOrDefault(), "1", Color.Red, Color.White); .在此我收到错误,因为无法将视图模型转换为页面
  • @ebk 如果你想在你的界面中访问当前页面,你可以使用 tabbpedPage。 tabbpedPage 是你要通过的页面。
  • 您的代码正在运行。但是当我更改标签时,我失去了我的徽章计数。谢谢
  • @ebk 这是另一个问题,取决于您如何在界面中实现它。
  • @Land Lu - MSFT.ok..我有另一个与此相关的问题。我将我的根页面设置为这样的导航 await NavigationService.NavigateAsync("NavigationPage/MenuPage");而不是你在this.so 中为我提供的第二个选项卡(视图 B 和视图 C)如果我导航到另一个视图说 D 从 B 像这样 _navigationService.NavigateAsync(nameof(D) 我没有得到任何导航栏。谢谢
猜你喜欢
  • 2010-10-07
  • 1970-01-01
  • 2013-08-07
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 2014-10-04
  • 2013-09-01
相关资源
最近更新 更多