【问题标题】:Xamarin Forms Grid IsVisbile binding only works in one wayXamarin Forms Grid IsVisbile 绑定仅以一种方式工作
【发布时间】:2020-11-12 04:58:11
【问题描述】:

我注意到我的 XAML 页面有一个奇怪的行为。 这是我的页面内容。我正在 iOS 模拟器中运行应用程序。

<Grid>
<Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid IsVisible="{Binding IsBusy, Converter={StaticResource BooleanReverseVisibilityConverter}}">
            <!-- Main content -->
        </Grid>

        <Grid IsVisible="{Binding IsBusy}">
            <!-- Busy indicator -->
            <Label Text="Doing some stuff..."/>
        </Grid>
    </Grid>

我的主要内容位于上方的网格中。当页面在做事时,主网格应该是不可见的,而繁忙的网格应该变得可见。

要知道,只有主要内容变得不可见,但是繁忙的网格也保持不可见。

这是我的绑定属性。

private bool _isBusy = false;
        public bool IsBusy
        {
            get => _isBusy;
            set
            {
                if (_isBusy == value) return;
                _isBusy = value;
                OnPropertyChanged();
            }
        }

我确实在AsyncCommand 中设置了IsBusy 变量(也尝试了普通的Command)。示例如下所示。

命令

CalculateCommand = new AsyncCommand(async () => await CalculateAction(), (t) => 
CalculateCommand_CanExcecute(t));

任务

private async Task CalculateAction()
        {
            IsBusy= true;
            try
            {
                IncrementalNumber++;
                //Do some calculation

                await ShellNavigationManager.GoToAsync(string.Format("{0}?cid={1}", ShellRoute.CalculationResultPage.ToString(), culc.Id), false);
                IsCalculating = false;

            }
            catch (Exception exc)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Strings.DialogUnexpectedErrorOccurredHeadline,
                    string.Format(Strings.DialogUnexpectedErrorOccurredFormatedContent, exc.Message),
                    Strings.Close
                    );

                IsBusy= false;

            }
        }

我想这与线程有关。我尝试在主线程中调用IsBusy 属性。

Device.BeginInvokeOnMainThread(async () =>
                {
                    {
                        IsBusy = true;
                    }
                });

这似乎有效,但这需要一段时间。计算几乎完成,直到调用将可视 UI 设置为当前状态。 我有点迷失在这里。 谢谢!

【问题讨论】:

  • 您提供的代码中没有提到如何与CalculateCommand 绑定。 “绑定只能以一种方式工作”是什么意思?
  • 嗨,CalculateCommand 绑定到一个按钮。单击按钮后,主网格将变得不可见。这可行,但是当前不可见的“繁忙网格”将不会设置为可见。两个网格都绑定到“IsBusy”布尔属性。所以换句话说,绑定可以将主网格设置为不可见,但不能将繁忙的网格设置为可见。希望这会有所帮助。

标签: c# ios xaml xamarin.forms binding


【解决方案1】:

我想通了。我似乎是 Visual Studio 中的一个错误。我只是剪切 (ctrl + x) 并再次粘贴整个 XAML 代码。从那以后,它按预期工作。没有代码更改.. 奇怪,但很高兴它现在可以工作了。

【讨论】:

  • 感谢分享。不要忘记接受答案。
【解决方案2】:

尝试使用触发器。

<Grid >
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid IsVisible="false">
        <Label Text="MainContent"/>
        <Grid.Triggers>
            <DataTrigger TargetType="Grid" Binding="{Binding Source={x:Reference IsBusyGrid}, Path=IsVisible}" Value="false">
                <Setter Property="IsVisible" Value="True"/>
            </DataTrigger>
        </Grid.Triggers>
    </Grid>

    <Grid IsVisible="{Binding IsBusy}"
          x:Name="IsBusyGrid">
        <!-- Busy indicator -->
        <Label Text="Doing some stuff..."/>
    </Grid>
</Grid>

【讨论】:

  • 我已经尝试过使用“DataTrigger”来做到这一点。也没有为我工作。我想这与“任务”有关。当我在主线程上设置 IsBusy 属性时,我似乎可以工作,但是延迟了......
【解决方案3】:

就我而言,问题出在bool _isBusy 属性中,缺少{get; set;}

bool _isBusy // not working

bool _isBusy {get; set;} // solved

【讨论】:

    猜你喜欢
    • 2021-04-16
    • 1970-01-01
    • 2017-08-28
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2020-02-25
    • 1970-01-01
    相关资源
    最近更新 更多