0

在有一个项目中,我在屏幕上有三个 Entry 控件。它们都绑定到所有包含文本的支持属性。当我运行应用程序时,只有具有焦点的入口控件才会显示其文本。其他输入控件在获得焦点之前不会显示其文本。我已将问题报告给 Microsoft。我只是好奇是否有其他人遇到过这个问题并找到了解决方法,直到它被修复。(我作为 UWP 项目运行)

复制的步骤。创建一个名为 EntryControlError 的新 Xamarin 表单应用程序。请在解决方案中包含一个 UWP 项目。

创建项目后,打开 MainPage.xaml 文件并将所有内容替换为以下 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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             BackgroundColor="#D0E1F9"
             x:Class="EntryControlError.MainPage">

    <ContentPage.Content>
        <StackLayout x:Name="slFullPage" Orientation="Vertical" HorizontalOptions="Fill" VerticalOptions="FillAndExpand" Margin="0,32,0,0" Padding="0,0,0,0">
            <ScrollView HorizontalOptions="Fill" VerticalOptions="FillAndExpand">
                <StackLayout Orientation="Vertical" HorizontalOptions="Fill" VerticalOptions="FillAndExpand">
                    <StackLayout x:Name="slTextHeader" Orientation="Vertical" HorizontalOptions="Center" VerticalOptions="Start">
                        <Label x:Name="lblTextHeaderInfo" Text="Enter Text - Max Three Lines" HorizontalOptions="Center" VerticalOptions="Start"
                               TextColor="#283655" FontAttributes="Bold" FontSize="Small" />

                        <Entry x:Name="txtHeaderText1" HorizontalOptions="Fill" VerticalOptions="Start" Text="{Binding KioskHeaderText1, Mode=TwoWay}" 
                               TextColor="#283655" HorizontalTextAlignment="Center" FontAttributes="Bold" FontSize="Medium"  />
                        <Entry x:Name="txtHeaderText2" HorizontalOptions="Fill" VerticalOptions="Start" Text="{Binding KioskHeaderText2, Mode=TwoWay}" 
                               TextColor="#283655" HorizontalTextAlignment="Center" Margin="0,-8,0,0" FontAttributes="Bold" FontSize="Medium" />
                        <Entry x:Name="txtHeaderText3" HorizontalOptions="Fill" VerticalOptions="Start" Text="{Binding KioskHeaderText3, Mode=TwoWay}" 
                               TextColor="#283655" HorizontalTextAlignment="Center" Margin="0,-8,0,0" FontAttributes="Bold" FontSize="Medium" />
                    </StackLayout>
                </StackLayout>
            </ScrollView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

将 MainPage.xaml.cs 文件中的所有内容替换为以下内容:

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Xamarin.Forms;

namespace EntryControlError
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _KioskHeaderText1;
        public string KioskHeaderText1
        {
            get { return _KioskHeaderText1; }
            set
            {
                _KioskHeaderText1 = value;
                OnPropertyChanged();
            }
        }
        private string _KioskHeaderText2;
        public string KioskHeaderText2
        {
            get { return _KioskHeaderText2; }
            set
            {
                _KioskHeaderText2 = value;
                OnPropertyChanged();
            }
        }
        private string _KioskHeaderText3;
        public string KioskHeaderText3
        {
            get { return _KioskHeaderText3; }
            set
            {
                _KioskHeaderText3 = value;
                OnPropertyChanged();
            }
        }

        public MainPage()
        {
            try
            {

                InitializeComponent();
                NavigationPage.SetHasNavigationBar(this, false);
                NavigationPage.SetHasBackButton(this, false);

                KioskHeaderText1 = "Line 1 Text";
                KioskHeaderText2 = "Line 2 Text";
                KioskHeaderText3 = "Line 3 Text";

                txtHeaderText1.BindingContext = this;
                txtHeaderText2.BindingContext = this;
                txtHeaderText3.BindingContext = this;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error: " + ex.Message);
            }
        }

        /// <summary>
        /// Manual Notification to subscribers that a property has changed. 
        /// </summary>

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }

}

运行应用程序,屏幕将如下所示:

在此处输入图像描述

如果您在不输入任何内容的情况下单击第二个或第三个条目控件,它们的绑定文本将自动出现。

4

2 回答 2

0

按照 Jason 的看法,不需要设置每个条目的 BindingContext,只需要设置当前的 Page BindingContext。

然后你创建三个属性,不实现INotifyPropertyChanged接口,所以当这些属性改变时,你不能更新 UI。

我修改了你关于绑定部分的代码,你可以看看:

 public partial class Page6 : ContentPage, INotifyPropertyChanged
{

    private string _KioskHeaderText1;
    public string KioskHeaderText1
    {
        get { return _KioskHeaderText1; }
        set
        {
            _KioskHeaderText1 = value;
            RaisePropertyChanged("KioskHeaderText1");
        }
    }
    private string _KioskHeaderText2;
    public string KioskHeaderText2
    {
        get { return _KioskHeaderText2; }
        set
        {
            _KioskHeaderText2 = value;
            RaisePropertyChanged("KioskHeaderText2");
        }
    }
    private string _KioskHeaderText3;
    public string KioskHeaderText3
    {
        get { return _KioskHeaderText3; }
        set
        {
            _KioskHeaderText3 = value;
            RaisePropertyChanged("KioskHeaderText3");
        }
    }
    public Page6()
    {
        InitializeComponent();

        KioskHeaderText1 = "Line 1 Text";
        KioskHeaderText2 = "Line 2 Text";
        KioskHeaderText3 = "Line 3 Text";


        this.BindingContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;     
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }


}

最后,由于您使用了Entry绑定,所以我不建议您直接更改Entry文本,您可以更改 KioskHeaderText1 ,KioskHeaderText2 ,KioskHeaderText3 属性来更改Entry文本。

于 2020-04-16T01:09:14.333 回答
0

似乎 Xamarin Forms 的更高版本已经解决了这些问题。如果您仍然遇到挑战,请确保您的 Xamarin Forms 是最新的。

于 2021-02-03T15:13:02.420 回答