Xamarin Forms 中条目的水平放置是不是存在任何已知问题?

Posted

技术标签:

【中文标题】Xamarin Forms 中条目的水平放置是不是存在任何已知问题?【英文标题】:Is there any known issue with horizontal placement of entries in Xamarin Forms?Xamarin Forms 中条目的水平放置是否存在任何已知问题? 【发布时间】:2020-03-29 11:59:18 【问题描述】:

在我的Xamarin 应用程序中进行设置页面的布局时,我注意到Entries 中的文本无法显示,除非我单击Entry。 我进行了不同的测试,重新创建了整个结构并得出结论,当两个条目非垂直显示时,就会发生这种情况。 也许我做错了什么,我只是看不到它(可能是因为我在互联网上做了很多研究但没有找到任何东西),所以这是我重现错误的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"
             xmlns:vm="clr-namespace:MyApp.ViewModels"
             mc:Ignorable="d"
             x:Class="MyApp.Views.SettingsPage">
    <ContentPage.BindingContext>
        <vm:SettingsViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <Label  Text="User information"
                    FontSize="Large"/>
            <StackLayout Orientation="Horizontal">
                <Label Text="Name*"/>
                <Entry Text="Binding Name"/>
                <Label Text="Name*"/>
                <Entry Text="Binding Name"/>
                <Label Text="Name*"/>
                <Entry Text="Binding Name"/>
                <Label Text="Name*"/>
                <Entry Text="Binding Name"/>
            </StackLayout>
            <StackLayout Orientation="Vertical">
                <Label Text="Name*"/>
                <Entry Text="Binding Name"/>
                <Label Text="Name*"/>
                <Entry Text="Binding Name"/>
                <Label Text="Name*"/>
                <Entry Text="Binding Name"/>
                <Label Text="Name*"/>
                <Entry Text="Binding Name"/>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

如您所见,两个StackLayouts 完全相同,只是第一个Orientation设置为Horizontal,第二个设置为Vertical。 所有Entries 都将它们的数据绑定设置为相同的公共属性,这意味着它们中的任何一个的值都应该相同。现在,当我进入我的设置页面时,第一个 StackLayout 在其中显示空条目。 第二个StackLayout 显示其中包含正确字符串的条目。 如果我单击第一个 StackLayout 内的 Entry 以在其中输入文本,则会显示其内容。 我使用Grid 尝试了同样的事情,但问题仍然存在。 我正在UWP 上执行此操作,但我不知道androidios 上是否也会发生这种情况。 这有什么已知问题吗?有没有人尝试水平显示条目?因为我找不到任何以正确方式执行此操作的示例,如果有的话。

我还提供ViewModel的代码,以防万一。

using MyApp.Models;
using Xamarin.Essentials;

namespace MyApp.ViewModels

    class SettingsViewModel : BaseViewModel
    
        public SettingsViewModel()
        

        
        public string Name
        
            get  return Preferences.Get(Consts.PREF_NAME, ""); 
            set
            
                Preferences.Set(Consts.PREF_NAME, value);
                OnPropertyChanged(nameof(Name));
            
        
    

【问题讨论】:

对于第一个水平堆栈布局,将堆栈布局放在滚动视图中,以便您的所有条目和标签都可以容纳它需要的空间,并且您可以滚动浏览它们。 @NirmalSubedi 谢谢,我确定我错过了什么。 【参考方案1】:

下面的代码对我来说很好。我刚刚将您的代码包含在 ScrollView 中。

<?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"
         xmlns:vm="clr-namespace:MyApp.ViewModels"
         mc:Ignorable="d"
         x:Class="MyApp.Views.SettingsPage">
    <ContentPage.BindingContext>
        <vm:SettingsViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <ScrollView>
            <StackLayout>
                <Label  Text="User information"
                FontSize="Large"/>
                <StackLayout Orientation="Horizontal">
                    <Label Text="Name*"/>
                    <Entry Text="Binding Name"/>
                    <Label Text="Name*"/>
                    <Entry Text="Binding Name"/>
                    <Label Text="Name*"/>
                    <Entry Text="Binding Name"/>
                    <Label Text="Name*"/>
                    <Entry Text="Binding Name"/>
                </StackLayout>
                <StackLayout Orientation="Vertical">
                    <Label Text="Name*"/>
                    <Entry Text="Binding Name"/>
                    <Label Text="Name*"/>
                    <Entry Text="Binding Name"/>
                    <Label Text="Name*"/>
                    <Entry Text="Binding Name"/>
                    <Label Text="Name*"/>
                    <Entry Text="Binding Name"/>
                </StackLayout>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

【讨论】:

【参考方案2】:

我在 iOS 和 UWP 端都对其进行了测试,一切正常。我认为您应该注意将值分配给SettingsViewModel.Name 的代码。以下是一些您可以参考的代码:

public partial class MainPage : ContentPage

    public MainPage()
    
        InitializeComponent();

        model m = new model();
        m.Name = "name_1";
        BindingContext = m;
    


class model : INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    public model()
    
    

    public string Name
    

        get  return Preferences.Get("Consts.PREF_NAME", ""); 
        set
        
            Preferences.Set("Consts.PREF_NAME", value);
            if (PropertyChanged != null)
               
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                           
        
    

结果:

【讨论】:

以上是关于Xamarin Forms 中条目的水平放置是不是存在任何已知问题?的主要内容,如果未能解决你的问题,请参考以下文章

PageRenderer中的Xamarin.Forms UINavigationBar

如何在 Xamarin Forms 中的数据绑定指定的位置放置文本

将 TapGestureRecognizer 应用于水平列表中的标签 - Xamarin Forms

Xamarin Forms 将不同字体大小的标签垂直对齐到同一基线

Xamarin Forms:如何更改 flowlistview 中所选项目的背景颜色?

Xamarin.Forms - 摇篮 FAB