Xamarin Forms 输入控件不显示绑定文本
Xamarin Forms entry control does not show bound text
我有一个项目,屏幕上有三个 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));
}
}
}
运行 应用程序和屏幕将如下所示:
如果你点击进入第二个或第三个输入控件而不输入任何内容,它们的绑定文本将自动出现。
按照Jason的意见,不需要设置每个entry的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文本。
Xamarin Forms 的更高版本似乎已经解决了这些问题。如果您仍然遇到挑战,请确保您的 Xamarin Forms 是最新的。
我有一个项目,屏幕上有三个 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));
}
}
}
运行 应用程序和屏幕将如下所示:
如果你点击进入第二个或第三个输入控件而不输入任何内容,它们的绑定文本将自动出现。
按照Jason的意见,不需要设置每个entry的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文本。
Xamarin Forms 的更高版本似乎已经解决了这些问题。如果您仍然遇到挑战,请确保您的 Xamarin Forms 是最新的。