0

我对 TabView 有一个奇怪的行为。看法:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="PLUTester.Test.View.Home"
             xmlns:view="clr-namespace:PLUTester.Test.View"
             xmlns:controls2="clr-namespace:MarcTron.Plugin.Controls;assembly=Plugin.MtAdmob"
             xmlns:local="clr-namespace:PLUTester;assembly=PLUTester"
             xmlns:xtc="http://xamarin.com/schemas/2020/toolkit"
             xmlns:localfonts="clr-namespace:PLUTester.AppFonts"
             BackgroundColor="White">
    <ContentPage.Resources>
        <Style TargetType="xtc:TabViewItem">
            <Setter Property="VerticalOptions" Value="Center"/>
            <Setter Property="FontSize" Value="30" />
            <Setter Property="FontSizeSelected" Value="35"/>
            <Setter Property="FontFamily" Value="MyIcon"/>
            <Setter Property="VerticalOptions" Value="FillAndExpand" />
            <Setter Property="TextColor" Value="Gray" />
            <Setter Property="TextColorSelected" Value="Black" />
            <Setter Property="FontAttributesSelected" Value="Bold" />
            <Setter Property="BadgeTextColor" Value="Black"/>
        </Style>

        <Style x:Key="CustomTabStyle" TargetType="xtc:TabView">
            <!--<Setter Property="IsTabTransitionEnabled" Value="True" />-->
            <Setter Property="TabStripHeight" Value="50" />
            <Setter Property="TabContentBackgroundColor" Value="White" />
            <Setter Property="TabIndicatorPlacement" Value="Bottom"/>
            <Setter Property="TabIndicatorColor" Value="Black"/>
            <Setter Property="TabStripPlacement" Value="Bottom" />
        </Style>
    </ContentPage.Resources>
    <ContentPage.Content>
        <Grid>
            <xtc:TabView Style="{StaticResource CustomTabStyle}" IsSwipeEnabled="False">
                <xtc:TabView.TabStripBackgroundView>
                    <Frame BackgroundColor="White" BorderColor="Gray" CornerRadius="0" Margin="-1,0,-1,0"/>
                </xtc:TabView.TabStripBackgroundView>
                <xtc:TabViewItem Text="{x:Static localfonts:FontIcon.SchoolOutline}">
                    <Grid>
                        <ContentView Content="{Binding TestView}"/>
                    </Grid>
                </xtc:TabViewItem>
                <xtc:TabViewItem Text="{x:Static localfonts:FontIcon.CardsOutline}">
                    <Grid>
                        <ContentView Content="{Binding LernView}"/>
                    </Grid>
                </xtc:TabViewItem>
                <xtc:TabViewItem Text="{x:Static localfonts:FontIcon.ChartLine}">
                    <Grid>
                        <ContentView Content="{Binding StatisticsView}"/>
                    </Grid>
                </xtc:TabViewItem>

                <xtc:TabViewItem Text="{x:Static localfonts:FontIcon.Podium}">
                    <Grid>
                        <ContentView Content="{Binding HighscoreView}"/>
                    </Grid>
                </xtc:TabViewItem>

                <xtc:TabViewItem Text="{x:Static localfonts:FontIcon.Tune}">
                    <Grid>
                        <ContentView>
                            <view:Settings/>
                        </ContentView>
                    </Grid>
                </xtc:TabViewItem>
            </xtc:TabView>           
        </Grid>        
  </ContentPage.Content>
</ContentPage>

视图模型:

        private ContentView _TestView;

        public ContentView TestView
        {
            get { return _TestView; }
            set { _TestView = value;
                OnPropertyChanged(nameof(TestView));
            }
        }

        private ContentView _LernView;

        public ContentView LernView
        {
            get { return _LernView; }
            set { _LernView = value;
                OnPropertyChanged(nameof(LernView));
            }
        }

        private ContentView _StatisticsView;

        public ContentView StatisticsView
        {
            get { return _StatisticsView; }
            set { _StatisticsView = value;
                OnPropertyChanged(nameof(StatisticsView));
            }
        }

        private ContentView _HighscoreView;

        public ContentView HighscoreView
        {
            get { return _HighscoreView; }
            set { _HighscoreView = value;
                OnPropertyChanged(nameof(HighscoreView));
            }
        }

第二页(LernView)包含一些条目,当我点击一个条目时,tabview 切换回第一页,因此无法输入一些数据。奇怪的是,当我更改页面的位置时,“LernView”在 TabView 中的“TestView”之前,然后“LernView”中的条目按预期工作,但“TestView”中的条目让我回到第一个站点。选择器的行为相同。

这是调试窗口的输出,当点击一个条目时:

[ViewRootImpl@b8ac428[MainActivity]] ViewPostIme pointer 0
[ViewRootImpl@b8ac428[MainActivity]] ViewPostIme pointer 1
[InputMethodManager] prepareNavigationBarInfo() DecorView@add6a19[MainActivity]
[InputMethodManager] getNavigationBarColor() -16711423
[InputMethodManager] Starting input: tba=com.modev.plutester ic=com.android.internal.widget.EditableInputConnection@4a24c5d mNaviBarColor -16711423 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
[InputMethodManager] startInputInner - Id : 0
[InputMethodManager] startInputInner - mService.startInputOrWindowGainedFocus
[InputTransport] Input channel constructed: 'ClientS', fd=231
[InputTransport] Input channel destroyed: 'ClientS', fd=226
[InputMethodManager] SSI - flag : 0 Pid : 28689 view : com.modev.plutester
[InputMethodManager] prepareNavigationBarInfo() DecorView@add6a19[MainActivity]
[InputMethodManager] getNavigationBarColor() -16711423
[InputMethodManager] prepareNavigationBarInfo() DecorView@add6a19[MainActivity]
[InputMethodManager] getNavigationBarColor() -16711423
[InputMethodManager] Starting input: tba=com.modev.plutester ic=null mNaviBarColor -16711423 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
[InputMethodManager] startInputInner - Id : 0
[InputMethodManager] startInputInner - mService.startInputOrWindowGainedFocus
[InputTransport] Input channel constructed: 'ClientS', fd=230
[InputTransport] Input channel destroyed: 'ClientS', fd=231
[IInputConnectionWrapper] getExtractedText on inactive InputConnection
[IInputConnectionWrapper] requestCursorAnchorInfo on inactive InputConnection
[InsetsSourceConsumer] setRequestedVisible: visible=true, type=13, host=com.modev.plutester/crc64abfcbdcb04412aea.MainActivity, from=android.view.InsetsSourceConsumer.show:229 android.view.InsetsController.showDirectly:1437 android.view.InsetsController.controlAnimationUnchecked:1110 android.view.InsetsController.applyAnimation:1417 android.view.InsetsController.show:962 android.view.ViewRootImpl$ViewRootHandler.handleMessage:6098 android.os.Handler.dispatchMessage:106 android.os.Looper.loop:246 android.app.ActivityThread.main:8506 java.lang.reflect.Method.invoke:-2 
[SurfaceControl] hide : mNativeObject = 480531777920 - sc.mNativeObject = 480531879648 - Surface(name=Surface(name=5fb0fa1 InputMethod)/@0x49804dd - animation-leash)/@0x1b84285
[SurfaceControl] nativeSetFlags Done : Surface(name=Surface(name=5fb0fa1 InputMethod)/@0x49804dd - animation-leash)/@0x1b84285
[SurfaceControl] hide : mNativeObject = 480531777920 - sc.mNativeObject = 480531879648 - Surface(name=Surface(name=5fb0fa1 InputMethod)/@0x49804dd - animation-leash)/@0x1b84285
[SurfaceControl] nativeSetFlags Done : Surface(name=Surface(name=5fb0fa1 InputMethod)/@0x49804dd - animation-leash)/@0x1b84285
[SurfaceControl] release : mNativeObject = 480531879648 - Surface(name=Surface(name=5fb0fa1 InputMethod)/@0x49804dd - animation-leash)/@0x1b84285 / android.view.-$$Lambda$Rl1VZmNJ0VZDLK0BAbaVGis0rrA.accept:2 android.view.InsetsSourceControl.release:170 android.view.InsetsAnimationThreadControlRunner.releaseControls:119 android.view.InsetsAnimationThreadControlRunner.access$200:40 
[SurfaceControl] nativeRelease nativeObject s[480531879648]
[SurfaceControl] nativeRelease nativeObject e[480531879648]

提到的条目:

<Entry Text="{Binding MaxPLU}" Keyboard="Numeric" FontSize="Medium" VerticalOptions="EndAndExpand" WidthRequest="80" HorizontalTextAlignment="Center" HorizontalOptions="Start">
                                            <Entry.Behaviors>
                                                <controls:NumberValidationBehavior/>
                                            </Entry.Behaviors>
                                        </Entry>

NumberValidationBehavior:

public class NumberValidationBehavior : Behavior<Entry>
    {
        protected override void OnAttachedTo(Entry entry)
        {
            entry.TextChanged += OnEntryTextChanged;
            base.OnAttachedTo(entry);
        }

        protected override void OnDetachingFrom(Entry entry)
        {
            entry.TextChanged -= OnEntryTextChanged;
            base.OnDetachingFrom(entry);
        }

        private static void OnEntryTextChanged(object sender, TextChangedEventArgs args)
        {

            if (!string.IsNullOrWhiteSpace(args.NewTextValue))
            {
                bool isValid = args.NewTextValue.ToCharArray().All(x => char.IsDigit(x)); //Make sure all characters are numbers

                ((Entry)sender).Text = isValid ? args.NewTextValue : args.NewTextValue.Remove(args.NewTextValue.Length - 1);
            }
        }
    }

一些澄清:TabView

  1. MyPage1 -> 一切都按预期工作
  2. MyPage2 -> 点击一个条目将我踢回 MyPage1

现在我切换页面:TabView

  1. MyPage2 -> 一切都按预期工作
  2. MyPage1 -> 点击一个条目将我踢回 MyPage2

我究竟做错了什么?

更新:如果我设置条目的 Horizo​​ntalTextAlignment,就会发生这种奇怪的行为。如果未设置,则该条目按预期工作。

4

0 回答 0