TabbedPage的项目中的BindingContext被覆盖

问题描述 投票:0回答:2

我在应用程序中有一个TabbedPage,我想为每个选项卡使用单独的[View + ViewModel],并且所有选项卡都应在运行时创建。

这是我到目前为止所做的:

CategoryItem.cs

using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace BlankApp1.Models
{
    public class CategoryItem
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public string ObjectId { get; set; }
        public string Name { get; set; }
        public long Position { get; set; }
        public long ParentId { get; set; }
        public string Description { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }

        public override string ToString()
        {
            return this.Name;
        }
    }
}

ProductsPage.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:prism="http://prismlibrary.com"
            xmlns:Views="clr-namespace:BlankApp1.Views"
            x:Class="BlankApp1.Views.ProductsPage"
            ItemsSource="{Binding CategoryList}"
            CurrentPageChanged="TabbedPage_CurrentPageChanged"
            Title="{Binding Title}">
    <TabbedPage.Behaviors>
        <prism:EventToCommandBehavior EventName="CurrentPageChanged"
                                  Command="{Binding FilterProductsByCategoryName}"/>
    </TabbedPage.Behaviors>
    <TabbedPage.ItemTemplate>
        <DataTemplate>
            <Views:CategoryTab Title="{Binding Name}"/>
        </DataTemplate>
    </TabbedPage.ItemTemplate>
</TabbedPage>

我知道,事件CurrentPageChanged被使用了两次,但是EventToCommand中的一个不起作用。

ProductsPageViewModel.cs

using BlankApp1.Models;
using BlankApp1.Services;
using Prism;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlankApp1.ViewModels
{
    public class ProductsPageViewModel : ViewModelBase, IPageDialogService
    {
        IPageDialogService PageDialogService { get; }
        private IDatabaseService _databaseService { get; }
        private IParseService _parseService { get; }

        public DelegateCommand FilterProductsByCategoryNameCommand { get; private set; }


        private ObservableCollection<CategoryItem> _categoryList;
        public ObservableCollection<CategoryItem> CategoryList
        {
            get => _categoryList;
            set => SetProperty(ref _categoryList, value);
        }

        private string _currentCategoryTab = "All";
        public string CurrentCategoryTab
        {
            get => _currentCategoryTab;
            set
            {
                if (SetProperty(ref _currentCategoryTab, value))
                {
                    FilterProductList();
                }
            }

        }

        public ProductsPageViewModel(INavigationService navigationService, IPageDialogService pageDialogService, IDatabaseService databaseService, IParseService parseService)
            : base(navigationService)
        {
            PageDialogService = pageDialogService;
            _databaseService = databaseService;
            _parseService = parseService;

            FilterProductsByCategoryNameCommand = new DelegateCommand(FilterProductsByCategoryName);

            Title = "Products";
        }

        private void FilterProductsByCategoryName()
        {
            // Can't get this to work
        }

        public override void Initialize(INavigationParameters parameters)
        {
            if (CategoryList == null || CategoryList.Count == 0)
            {
                CategoryList = new ObservableCollection<CategoryItem>(GetCategoryListAsync().GetAwaiter().GetResult());
            }


        }
    }
}

CategoryTab.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="http://prismlibrary.com"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="BlankApp1.Views.CategoryTab"
             Title="{Binding Title}"
             Appearing="ContentPage_Appearing">
    <ContentPage.Behaviors>
        <prism:EventToCommandBehavior EventName="Appearing"
                                      Command="{Binding AppearingCommand}"/>
    </ContentPage.Behaviors>
    <ContentPage.Content>
        <StackLayout>
            <Label Text="AAAAAAAAAAAAAA"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

CategoryTab.xaml.cs

using BlankApp1.ViewModels;
using Xamarin.Forms;

namespace BlankApp1.Views
{
    public partial class CategoryTab : ContentPage
    {
        public CategoryTab()
        {
            InitializeComponent();
            var vm = BindingContext as CategoryTabViewModel;
            vm.Title = Title;
        }

        private void ContentPage_Appearing(object sender, System.EventArgs e)
        {
            ContentPage contentPage = sender as ContentPage;
            var x = BindingContext as CategoryTabViewModel;
            x.Title = contentPage.Title;
        }
    }
}

CategoryTabViewModel.cs

using BlankApp1.Models;
using BlankApp1.Services;
using Prism;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Services;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BlankApp1.ViewModels
{
    public class CategoryTabViewModel : ViewModelBase, IActiveAware
    {
        private IDatabaseService _databaseService { get; }
        public CategoryItem CategoryItem { get; set; }
        public DelegateCommand AppearingCommand { get; private set; }

        public event EventHandler IsActiveChanged;

        private bool _isActive;
        public bool IsActive {
            get { return _isActive; }
            set { SetProperty(ref _isActive, value, FilterProductsByCategoryName); }
        }

        public CategoryTabViewModel(INavigationService navigationService, IDatabaseService databaseService)
            : base(navigationService)
        {
            _databaseService = databaseService;
            AppearingCommand = new DelegateCommand(Appearing);

            CategoryItem = _databaseService.GetCategoryByNameAsync(Title).GetAwaiter().GetResult();

        }

        protected virtual void FilterProductsByCategoryName()
        {
            IsActiveChanged?.Invoke(this, EventArgs.Empty);
            //PageDialogService.DisplayAlertAsync("Test", "Some category is selected", "Cancel");
            var x = Title;
            var xx = 1;
        }

        private async void Appearing()
        {

        }


    }
}

问题是,通过使用TabbedPage中的ItemsSource:

ItemsSource="{Binding CategoryList}"

选项卡视图的BindingContext被覆盖,并且不再等于视图选项卡中CategoryTabViewModel的实例:

var x = BindingContext;

返回从ItemsSource =“ {Binding CategoryList}”]继承的CategoryItem对象。

我的问题是,Category.xaml

中的命令未在与其关联的ViewModel中捕获。绑定也无法正常工作。

我的问题:如何将CategoryItem

对象传递到选项卡视图,并能够使用Binding并将命令发送到ViewModel类(CategoryTab.xaml + CategoryTabViewModel.xaml

P.S。 :所有视图,ViewModels,服务...都在App.xaml.cs中注册]

我在应用程序中有一个TabbedPage,我想为每个选项卡使用单独的[View + ViewModel],并且所有选项卡都应在运行时创建。到目前为止,这是我所做的:CategoryItem.cs ...

c# xaml xamarin.forms prism
2个回答
0
投票

我认为这里有问题:


0
投票

我已经找到了解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.