XAML 虽然在ViewModel中设置了绑定值,但标签未显示该值

d6kp6zgx  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(157)

问题
我的视图中只显示了两个值FansNoDescription。我已经尝试过修复了。我确实注意到,当我更改其中一个Forms9Patch Label中的值时,XAML热重新加载导致所有项目正确显示。我还没有在Xamarin.iOS上测试过这个,但在Xamarin.Android上,这个特定的页面给我带来了相当多的麻烦。我的目标是使用C#版本8的.NET标准2.1。
如有任何帮助,我们将不胜感激。
我最后的尝试将是卸载需要.NET v2.1的模块,并将目标降级到v2.0。我会让你知道这是否有效-我不抱太大希望。

调试信息

这些是从ViewModel中打印的控制台中的一些行-它们证明数据被正确接收,并且可绑定的值(例如Title)被设置。拉取以刷新(调用PropertyChanged事件)不会导致值显示。

User Object: {"Artist":{"Name":"Matt Band","Description":"The best band in the area performing electronic stuffs","Genres":["Electro","D&B","Piano"],"MaxDistance":25,"MinNotice":3},"Venue":{"Name":null,"Description":null,"MinNotice":0},"FirstName":"Matthew","LastName":"Main","DOB":"2001-01-26T00:00:00.391Z","Followers":250,"Devices":["fgfafgadfgdafga"]}
Name: Matt Band

工作/非工作值

我已经使用Console.WriteLine()测试了ViewModel端的所有值,它们都设置正确(参见上面的命令行块)。
有效值为:

  • FansNo
  • Description
  • 所有CollectionView模板值(即:(一月七日至十日、一月八日至十日、一月九日至十日)
  • 用于调整大小的所有数值(即:PostSpacing)和命令(IE:(一月一日)

不工作的有:

  • Title
  • NameGenre
  • Location

代码

视图模型:

using System;
using AV.Resources.Structures;
using System.Collections.ObjectModel;
using Xamarin.Essentials;
using Xamarin.Forms;
using Plugin.CloudFirestore;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Input;

namespace AV.ViewModels
{
    public class ProfilePage : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<User> UsersList { get; set; }
        public ObservableCollection<Post> Posts { get; set; }

        public double RowWidth { get; set; }
        public double RowSpacing { get; set; }
        public double RowStart { get; set; }

        public double RowHeight { get; set; }
        public double PostSpacing { get; set; }
        public Thickness ContentPadding { get; set; }

        public double TopLayoutHeight { get; set; }

        public string Title { get; set; }
        public string NameGenre { get; set; }
        public string Location { get; set; }
        public int FansNo { get; set; }
        public int RatingNo { get; set; }
        public string Description { get; set; }

        public ICommand comm
        {
            get
            {
                return new Command(() =>
                {
                    Console.WriteLine("Property Changed! Title is: " + Title);
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs(null));
                });
            }
        }

        public ProfilePage(string userID)
        {

            Init();
            LoadProfile(userID);

        }

        void Init()
        {

            PostSpacing = (DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density) * 0.023758;
            RowHeight = (DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density) * 0.058315;
            ContentPadding = new Thickness((DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density) * 0.086449, 0, (DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density) * 0.037383, (DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density) * 0.017279);

            RowWidth = (DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density) * 0.189252;
            RowSpacing = (DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density) * 0.046729;
            RowStart = (DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density) * 0.086449;

            TopLayoutHeight = (DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density) * 0.763499;

            UsersList = new ObservableCollection<User>()
            {
                new User
                {
                    FirstName = "User"
                },
                new User
                {
                    FirstName = "User"
                },
                new User
                {
                    FirstName = "User"
                },
                new User
                {
                    FirstName = "User"
                },
                new User
                {
                    FirstName = "User"
                },
                new User
                {
                    FirstName = "User"
                }
            };

        }

        async void LoadProfile(string userID)
        {

            Console.WriteLine("UserID is: " + userID);

            Posts = new ObservableCollection<Post>();

            IDocumentSnapshot doc = await App.Firestore.Collection("users").Document(userID).GetAsync();
            User user = doc.ToObject<User>();

            Console.WriteLine("User Object: " + JsonConvert.SerializeObject(user));

            //IQuery relatedQuery;
            if (!user.Artist.Equals(default(User.artist)))
            {
                Console.WriteLine("This user is an artist");
                Title = user.Artist.Name;
                NameGenre = user.Artist.Genres[0];
                Location = "Location";
                FansNo = user.Followers;
                RatingNo = 5;
                Description = user.Artist.Description;
                Console.WriteLine("Name: " + Title);
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(null));
                //relatedQuery = App.Firestore.Collection("users").WhereArrayContainsAny("Artist.Genres", user.artist.Genres);
            }
            else if (!user.Venue.Equals(default(User.venue)))
            {
                Console.WriteLine("This user is a venue");
                Title = "Venue";
                NameGenre = user.Venue.Name;
                Location = "Location";
                FansNo = user.Followers;
                RatingNo = 5;
                Description = user.Venue.Description;
            }
            else
            {
                Console.WriteLine("This user is neither");
                Console.WriteLine("Name is: " + user.Artist.Name);
                //regular user
            }

            IQuerySnapshot posts = await App.Firestore.Collection("users").Document(userID).Collection("posts").OrderBy("TimeStamp").LimitTo(8).GetAsync();
            foreach(Post post in posts.ToObjects<Post>())
            {
                Posts.Add(post);
            }

            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(null));

        }

    }
}

视图:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage x:Name="contentPage" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:Forms9Patch="clr-namespace:Forms9Patch;assembly=Forms9Patch" xmlns:ViewModels="clr-namespace:AV.ViewModels" xmlns:ext="clr-namespace:AV.Resources.Extensions" x:Class="AV.ProfilePage" NavigationPage.HasNavigationBar="False">
    <ContentPage.Resources>

        <DataTemplate x:Key="textTemplate">

            <StackLayout BackgroundColor="#F7F7F7">

                <RelativeLayout HeightRequest="{Binding Source={x:Reference contentPage}, Path=BindingContext.RowHeight}">

                    <Image Source="logo2" Aspect="AspectFit" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.077}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.077}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.031}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.231}" />
                    <Forms9Patch:Label Text="{Binding AuthorID}" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.287}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.333}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.138}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.276}" />
                    <Forms9Patch:Label Text="{Binding TimeStamp}" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.098}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.222}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.138}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.646}" />
                    <Forms9Patch:Label Text="{Binding Likes.Length}" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.047}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.333}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.847}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.426}" />
                    <Image Source="LikeButton" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.037}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.315}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.925}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.37}" />

                </RelativeLayout>
                <Label Text="{Binding Content}" FontFamily="Syne" Margin="{Binding Source={x:Reference contentPage}, Path=BindingContext.ContentPadding}" />

            </StackLayout>

        </DataTemplate>

        <DataTemplate x:Key="imageTemplate">

            <StackLayout BackgroundColor="#F7F7F7">

                <RelativeLayout HeightRequest="{Binding Source={x:Reference contentPage}, Path=BindingContext.RowHeight}">

                    <Image Source="logo2" Aspect="AspectFit" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.077}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.077}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.031}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.231}" />
                    <Forms9Patch:Label Text="{Binding AuthorID}" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.287}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.333}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.138}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.276}" />
                    <Forms9Patch:Label Text="{Binding TimeStamp}" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.098}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.222}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.138}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.646}" />
                    <Forms9Patch:Label Text="{Binding Likes.Length}" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.047}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.333}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.847}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.426}" />
                    <Image Source="LikeButton" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.037}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.315}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.925}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.37}" />

                </RelativeLayout>
                <Label Text="{Binding Content}" FontFamily="Syne" Margin="{Binding Source={x:Reference contentPage}, Path=BindingContext.ContentPadding}" />
                <Image Source="SampleImage" Aspect="AspectFit" BackgroundColor="Red" HorizontalOptions="CenterAndExpand" />

            </StackLayout>

        </DataTemplate>

        <ext:RowTypeSelector x:Key="selector" TextTemplate="{StaticResource textTemplate}" ImageTemplate="{StaticResource imageTemplate}" />

    </ContentPage.Resources>

    <RefreshView Command="{Binding comm}">
        <ScrollView>
            <StackLayout BackgroundColor="White" Spacing="{Binding PostSpacing}">

                <RelativeLayout HeightRequest="{Binding TopLayoutHeight}">

                    <Image BackgroundColor="Red" Aspect="AspectFit" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1.0}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.402}" />
                    <Forms9Patch:Label Text="{Binding Path=BindingContext.Title, Source={x:Reference contentPage}}" TextColor="Black" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.43}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.034}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.086}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.436}" />

                    <Forms9Patch:Label Text="Fans" TextColor="Black" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" HorizontalTextAlignment="Center" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.189}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.025}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.512}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.475}" />
                    <Forms9Patch:Label Text="{Binding FansNo}" TextColor="Black" FontSize="100" Lines="0" AutoFit="Width" HorizontalTextAlignment="Center" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.189}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.025}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.512}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.443}" />
                    <Forms9Patch:Label Text="Rating" TextColor="Black" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" HorizontalTextAlignment="Center" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.189}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.025}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.724}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.475}" />

                    <Forms9Patch:Label Text="{Binding NameGenre}" TextColor="Black" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.402}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.025}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.086}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.501}" />
                    <Forms9Patch:Label Text="{Binding Location}" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" FontSize="100" Lines="0" AutoFit="Width" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.827}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.023}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.086}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.53}" />
                    <Label Text="{Binding Description}" TextColor="Black" FontFamily="Syne" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.827}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.115}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.086}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.569}" />

                    <Frame BackgroundColor="Black" Padding="3" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.402}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.048}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.086}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.69}">
                        <Frame BackgroundColor="White" Padding="1" CornerRadius="0" InputTransparent="True">
                            <Forms9Patch:Label Text="Follow" TextColor="Black" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" InputTransparent="True" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Lines="0" AutoFit="Width" FontSize="100" />
                        </Frame>
                    </Frame>
                    <Frame BackgroundColor="Blue" Padding="4" CornerRadius="0" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.292}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.048}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.512}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.69}">
                        <Forms9Patch:Label Text="Chat" TextColor="White" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" InputTransparent="True" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Lines="0" AutoFit="Width" FontSize="100" />
                    </Frame>
                    <Frame BackgroundColor="Blue" Padding="6" CornerRadius="0" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.086}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.048}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.827}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.69}">
                        <Image Source="CalendarIcon" Aspect="AspectFit" />
                    </Frame>

                    <BoxView BackgroundColor="Black" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.827}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.004}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.086}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.766}" />

                    <Forms9Patch:Label Text="Artists" Lines="0" AutoFit="Width" FontSize="100" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.827}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.023}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.086}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.803}" />
                    <ScrollView Orientation="Horizontal" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=2}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.16}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.84}">

                        <CollectionView ItemsSource="{Binding UsersList}" TranslationX="{Binding RowStart}">

                            <CollectionView.ItemsLayout>

                                <LinearItemsLayout Orientation="Horizontal" ItemSpacing="{Binding Path=BindingContext.RowSpacing, Source={x:Reference contentPage}}" />

                            </CollectionView.ItemsLayout>

                            <CollectionView.ItemTemplate>

                                <DataTemplate>

                                    <RelativeLayout WidthRequest="{Binding Path=BindingContext.RowWidth, Source={x:Reference contentPage}}">

                                        <Image Source="logo2" Aspect="AspectFit" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1.0}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1.0}" />
                                        <Forms9Patch:Label Text="{Binding FirstName}" BackgroundColor="White" FontFamily="AV.Resources.Fonts.Syne-Bold.otf" TextColor="Black" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" Lines="0" AutoFit="Width" FontSize="100" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1.0}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.159}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.841}" />

                                    </RelativeLayout>

                                </DataTemplate>

                            </CollectionView.ItemTemplate>

                        </CollectionView>

                    </ScrollView>

                </RelativeLayout>

                <RelativeLayout>

                    <CollectionView ItemsSource="{Binding Posts}" ItemTemplate="{StaticResource selector}">

                        <CollectionView.ItemsLayout>

                            <LinearItemsLayout Orientation="Vertical" ItemSpacing="{Binding PostSpacing}" />

                        </CollectionView.ItemsLayout>

                    </CollectionView>

                </RelativeLayout>

            </StackLayout>
        </ScrollView>
    </RefreshView>
</ContentPage>
0kjbasz6

0kjbasz61#

我看到您在ViewModel中定义了属性,但在设置其值时,它们没有引发属性更改。
在您的视图模型中,您可以定义完整的属性详细信息,并在它们更新时引发更改,如下例所示:

private string  _title;
    public string Title
    {
        get 
        { 
            return _title; 
        }
        set 
        { 
            _title = value;
            OnPropertyChanged("Title");}
        }

另外,我看到你已经从INotifyPropertyChanged继承了,请确保你已经在ViewModel中正确实现了该接口!示例:

public void OnPropertyChanged(string propertyName)
 {
     if (PropertyChanged != null)
     {
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }
 }

PS:不确定这是否能解决你的问题。但至少这会让你更容易开发!

xsuvu9jc

xsuvu9jc2#

这是可行的

<Forms9Patch:Label Text="{Binding FansNo}" ...

这不

<Forms9Patch:Label Text="{Binding Path=BindingContext.Title, Source={x:Reference contentPage}}" ...

这两个属性都是在VM中的同一个位置定义的,因此,修改不工作的绑定以匹配工作的绑定可以解决这个问题

<Forms9Patch:Label Text="{Binding Title}" ...
bpsygsoo

bpsygsoo3#

我已经得出结论,这是Forms9Patch库的问题。当我将元素更改为常规标签时,它们都显示得很好。

编辑

我已经解决了这个问题。这是Forms9Patch使用绑定的方式的问题。似乎是因为从数据库加载项需要一两秒钟,所以ViewModel中的值在这段时间内保持为空。在此之前,Label尝试绑定到此,并且绑定通道关闭,因为值为空。我通过为ViewModel构造函数开头的值添加字符串值来修复这个问题,以确保在打开绑定通道之前设置了一些内容。用来自数据库的正确字符串覆盖这些临时字符串并将它们显示在屏幕上。

相关问题