【问题标题】:Update the Counter in Badge, Xamarin Forms app更新 Badge、Xamarin Forms 应用程序中的计数器
【发布时间】:2020-08-04 11:44:28
【问题描述】:

我正在使用此 Plugin.Badge 在 Xamarin Forms 应用程序的选项卡式视图中显示购物车项目数。

这是我的标签页 xaml 代码 MainPage.xaml

<?xml version="1.0" encoding="utf-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:d="http://xamarin.com/schemas/2014/forms/design"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
            xmlns:plugin="clr-namespace:Plugin.Badge.Abstractions;assembly=Plugin.Badge.Abstractions"
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core" android:TabbedPage.ToolbarPlacement="Bottom"
            SelectedTabColor="{StaticResource HighlightText}"  BarBackgroundColor="{StaticResource HighlightBg}" UnselectedTabColor="Gray" 
            xmlns:views="clr-namespace:Projectname.Views" x:Class="Projectname.Views.MainPage" >
   <TabbedPage.Children>
        <NavigationPage Title="Home" IconImageSource="home.png">
                  <x:Arguments>
                <views:HomePage />
            </x:Arguments>
        </NavigationPage>

         <NavigationPage Title="Search" IconImageSource="search.png">
            
            <x:Arguments>
                <views:AboutPage />
            </x:Arguments>
        </NavigationPage>
         
         <NavigationPage Title="Cart" IconImageSource="cart.png" 
             plugin:TabBadge.BadgeText= "{Binding Counter}"  
             plugin:TabBadge.BadgeColor="Red"
              plugin:TabBadge.BadgeTextColor="White"   plugin:TabBadge.BadgePosition="PositionTopRight" >
           
            <x:Arguments>
              
                <views:AboutPage />
            </x:Arguments>
        </NavigationPage>


        <NavigationPage Title="Account" IconImageSource="account.png">
            
            <x:Arguments>
                <views:AccountPage />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
</TabbedPage>

下面是 MainPage.xaml.cs

中的代码
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.Essentials;
using Plugin.Badge.Abstractions;
using System.Collections.ObjectModel;

//using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;


namespace Projectname.Views
{
    
    [DesignTimeVisible(false)]

    public partial class MainPage : TabbedPage
    {
        int Counter;
        public MainPage()
        {
            InitializeComponent();
            Counter = 2;
         }
     }
 }

在您可以看到的其中一个标签页子项中,徽章设置为 plugin:TabBadge.BadgeText= ""{绑定计数器}"

但不起作用。

我想将徽章计数器的值设置为页面后面代码中变量的值MainPage.xaml.cs

为此,要在代码中进行所有更改。请帮我解决这个问题。

【问题讨论】:

  • 您好,您需要在子页面中绑定值,而不仅仅是在MainPage 中。你可以看看sample code。我不知道为什么这在 MainPage 中不起作用,也许这是这个 NugetPackage 的问题。

标签: xaml xamarin xamarin.forms xamarin-forms-4


【解决方案1】:

这行不通。您正在创建局部变量,甚至没有设置绑定上下文。

首先我建议创建 ViewModel 例如:

public class MainPageViewModel : INotifyPropertyChanged
{
    private int counter = 0;
    public MainPageViewModel()
    {
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public int Counter
    {
        set { counter = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Counter))); }
        get { return counter; }
    }

}

然后在您的 MainPage 中为 viewmodel 创建绑定上下文

public partial class MainPage : TabbedPage
{
    private MainPageViewModel viewModel;
    public MainPage()
    {
        InitializeComponent();
        viewModel = new MainPageViewModel();
        BindingContext = viewModel;
        viewModel.Counter = 2;
     }
 }

【讨论】:

  • 谢谢@adlorem,我正在尝试实现你的代码
猜你喜欢
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
相关资源
最近更新 更多