简体   繁体   English

视图模型中的数据未绑定到视图 - Xamarin 表单,反应式 UI

[英]Data from view model not binding to view - Xamarin Forms, Reactive UI

Welcome, I'm trying to bind array of strings to collection view item source, but it seems like my view model can't pass data, or isn't activated at all.欢迎,我正在尝试将字符串数组绑定到集合视图项源,但我的视图模型似乎无法传递数据,或者根本没有激活。 I'm stuck at this moment.我被困在这一刻。 I tried to search on the internet and couldnt find any solution, also tried exacly as shown in the reactive ui sample, and stil same results, or should I say, no results.我尝试在互联网上搜索并找不到任何解决方案,也尝试了反应式 ui 示例中所示的精确,但仍然得到相同的结果,或者我应该说没有结果。

View:看法:

<?xml version="1.0" encoding="utf-8"?>

<xamForms:ReactiveContentPage x:TypeArguments="dashboard:DashboardViewModel" xmlns="http://xamarin.com/schemas/2014/forms"
                              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                              xmlns:xamForms="clr-namespace:ReactiveUI.XamForms;assembly=ReactiveUI.XamForms"
                              xmlns:dashboard="clr-namespace:RoutinesTracker.Pages.Dashboard;assembly=RoutinesTracker"
                              x:Class="RoutinesTracker.Pages.Dashboard.DashboardView"
                              Title="Home"
                              BackgroundColor="{DynamicResource PageBackgroundColor}">
    <ContentPage.Content>
        <StackLayout Padding="15, 20">
            
            <CollectionView x:Name="ExampleCollectionView">
                <CollectionView.ItemTemplate>
                    <DataTemplate x:DataType="x:String">
                        <StackLayout>
                            <Frame 
                                CornerRadius="15" 
                                HasShadow="False"
                                BackgroundColor="{DynamicResource PageElementColor}"
                                Margin="0, 5">
                                <StackLayout>
                                    <Label Text="{Binding}"
                                           VerticalOptions="CenterAndExpand"
                                           HorizontalOptions="CenterAndExpand" 
                                           TextColor="{DynamicResource PrimaryTextColor}"/>
                                </StackLayout>
                            </Frame>
                        </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </StackLayout>
    </ContentPage.Content>
</xamForms:ReactiveContentPage>

View.cs查看.cs

using ReactiveUI;
using Xamarin.Forms.Xaml;

namespace RoutinesTracker.Pages.Dashboard
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DashboardView
    {
        public DashboardView()
        {
            InitializeComponent();

            this.WhenActivated(disposable =>
            {
                this.OneWayBind(ViewModel, vm => vm.ExampleCollectionList, v => v.ExampleCollectionView.ItemsSource);
            });
        }
    }
}

ViewModel视图模型

using Prism.Navigation;
using RoutinesTracker.Core.Layout.ViewModels;

namespace RoutinesTracker.Pages.Dashboard
{
    public abstract class DashboardViewModel : ViewModelBase
    {
        public string[] ExampleCollectionList { get; set; }

        protected DashboardViewModel(INavigationService navigationService) : base(navigationService)
        {
            ExampleCollectionList = new[]
            {
                "Item 1",
                "Item 2",
                "Item 3",
                "Item 4",
                "Item 5"
            };
        }
    }
}

And if anyone need, ViewModelBase class:如果有人需要,ViewModelBase 类:

using Prism.Navigation;
using ReactiveUI;

namespace RoutinesTracker.Core.Layout.ViewModels
{
    public abstract class ViewModelBase : ReactiveObject
    {
        private readonly INavigationService _navigationService;

        protected ViewModelBase(INavigationService navigationService) => _navigationService = navigationService;
    }
}

Looks like you missed add ItemSource on CollectionView.看起来您错过了在 CollectionView 上添加 ItemSource。 Try this尝试这个

<CollectionView ItemsSource="{Binding ExampleCollectionList }" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM