Xamarin Community Toolkit选项卡点击条目时查看切换选项卡

j2cgzkjk  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(120)

我在使用TabView时出现了奇怪的行为。View:

<?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)包含一些条目,当我点击一个条目时,标签视图切换回第一页,因此无法输入一些数据。奇怪的是,当我改变页面的位置时,"LernView"在标签视图中位于"TestView"之前,然后"LernView"中的条目按预期工作,但是"TestView"中的条目把我踢回了第一个站点。与Picker的行为相同。
以下是点击条目时调试窗口的输出:

[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>

编号验证行为:

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);
            }
        }
    }

一些澄清:选项卡视图

  1. MyPage1-〉一切按预期运行
  2. MyPage2-〉点击一个条目会将我踢回MyPage1
    现在我切换页面:选项卡视图
  3. MyPage2-〉一切按预期运行
  4. MyPage1-〉点击条目会将我踢回MyPage2
    我哪里做错了?
    • 更新:如果我设置了条目的HorizontalTextAlignment,就会发生这种奇怪的行为。如果没有设置,条目会按预期工作。**
t5fffqht

t5fffqht1#

另一种解决方法是禁用离开选项卡时打开虚拟键盘的所有控件(输入/编辑器),并在其处于活动选项卡时启用。
奇怪的是,当您聚焦在一个条目/编辑器上并通过选项卡导航时,以及当您在控件聚焦后向后导航一页时,它将触发Focused事件,选项卡将调整为至少在条目或编辑器上显示光标。

private bool _isLernViewActive;

    public bool IsLernViewActive
    {
        get { return _isLernViewActive; }
        set { _isLernViewActive = value;
            OnPropertyChanged(nameof(IsLernViewActive));
        }
    }

XAML:

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

相关问题