[英]ToolbarItems count is coming as 0 in viewmodel
我正在嘗試從我的viewmodel綁定工具欄項中的徽章計數。
但是每次Toolbaritem的計數在我的viewmodel中變為0時,當我在后端C#中嘗試相同的代碼時,我的計數為1。
Menupage.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之后加載的。我也在棱鏡OnNavigatedFrom上嘗試過,但結果是相同的。另一個與加載有關的問題是,我的應用程序需要花費更多的時間才能打開,因為每個頁面的viewmodel都在命中。如何僅限制在應用打開時加載第一個標簽詳細信息頁面
我想您的項目的架構必須是這樣的:
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繼承,這沒有任何意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.