【发布时间】: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