我有一个 dotnet 8 MAUI IOS 应用程序,它有一个记录用户的视图。我最近将应用程序从 dotnet 7 更新到了 8,现在我在此页面上遇到了一些样式问题。暂停和重置按钮有时具有灰色背景。背景并不总是存在。它取决于选择的按钮和视图的状态。最初只有重置和暂停有奇怪的背景。当您单击“播放”时,它只会重置,而当您单击“停止”时,它只会暂停。
这是有问题的区域:
<Grid IsVisible="{Binding Path=BindingContext.IsControlsVisible, Source={x:Reference mainPage}}">
<Image Source="recording.png" ZIndex="1"/>
<FlexLayout WidthRequest="210" JustifyContent="SpaceBetween" AlignItems="Center" ZIndex="2">
<Button ImageSource="restart.png" BackgroundColor="Transparent" WidthRequest="80"
Clicked="ResetButtonClicked"
IsEnabled="{Binding Path=BindingContext.IsRestartButtonEnabled, Source={x:Reference mainPage}}"/>
<Button ImageSource="stop.png" BackgroundColor="Transparent" WidthRequest="80"
IsVisible="{Binding Path=BindingContext.IsStopButtonVisible, Source={x:Reference mainPage}}"
Command="{Binding Path=BindingContext.StopCommand, Source={x:Reference mainPage}}"/>
<Button ImageSource="start.png" BackgroundColor="Transparent" WidthRequest="80"
IsVisible="{Binding Path=BindingContext.IsRecordButtonVisible, Source={x:Reference mainPage}}"
Command="{Binding Path=BindingContext.RecordCommand, Source={x:Reference mainPage}}"/>
<Button ImageSource="pause.png" BackgroundColor="Transparent" WidthRequest="80"
Command="{Binding Path=BindingContext.PauseCommand, Source={x:Reference mainPage}}"
IsEnabled="{Binding Path=BindingContext.IsPauseButtonEnabled, Source={x:Reference mainPage}}"/>
</FlexLayout>
<Label ZIndex="2" Text="{Binding Path=BindingContext.TimerLabel, Source={x:Reference mainPage}}"
IsVisible="{Binding Path=BindingContext.IsCountupVisible, Source={x:Reference mainPage}}"
TextColor="{StaticResource AppGray}" FontSize="Large"
VerticalOptions="Center" HorizontalOptions="Center" Margin="6, 110, 0, 0"/>
<Label ZIndex="2" Text="{Binding Path=BindingContext.TimerRemainingLabel, Source={x:Reference mainPage}}"
IsVisible="{Binding Path=BindingContext.IsCountdownVisible, Source={x:Reference mainPage}}"
TextColor="Red" FontSize="Large"
VerticalOptions="Center" HorizontalOptions="Center" Margin="6, 110, 0, 0"/>
</Grid>
此代码位于视图模型中,并在单击按钮时进行处理:
public ICommand ResetCommand { get; set; }
public ICommand PauseCommand { get; set; }
...
ResetCommand = new Command(ResetRecording);
PauseCommand = new Command(PauseRecording);
...
private void PauseRecording()
{
if (recordAudioService != null)
{
isRecord = false;
IsPauseButtonEnabled = false;
IsStopButtonVisible = false;
IsResumeButtonVisible = true;
IsRecordButtonVisible = true;
IsRestartButtonEnabled = true;
recordAudioService.PauseRecord();
}
}
private void ResetRecording()
{
if (IsRecordingAudio && !isRecord)
{
recordAudioService.ResetRecord();
timerValue = new TimeSpan();
timerRemainingValue = new TimeSpan();
TimerLabel = string.Format("{0:mm\\:ss}", timerValue);
IsRecordingAudio = false;
IsPauseButtonEnabled = false;
IsResumeButtonVisible = false;
IsStopButtonVisible = false;
IsRestartButtonEnabled = false;
CreateTimer();
questionNumber = 1;
RecordingStatus = "";
IsAnswerRecorded = false;
}
}
我最近还更新了这些软件包:
<PackageReference Include="CommunityToolkit.Maui" Version="9.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Plugin.Maui.Audio" Version="3.0.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.82" />
那么也许其中之一与此有关?
这是相关问题的图片:
我不想要暂停按钮周围的灰色背景。这与重置按钮周围显示的背景相同。
我知道那是什么了,但现在我觉得有点傻。灰色背景是由于按钮上的
IsEnabled
属性造成的:
IsEnabled="{Binding Path=BindingContext.IsPauseButtonEnabled, Source={x:Reference mainPage}}"
禁用按钮时可见灰色背景。可能有一种更优雅的方法来避免这种情况,但就我而言,简单地删除
IsEnabled
属性更容易。
我希望这对将来的人有帮助。