[英]Hide/Unhide Button based on Condition in C#
我在 Xamarin.Forms 工作。 在 CredentialPage.xml 頁面中,有一個按鈕,我想根據 CredentialViewMode.cs 頁面中的憑據狀態隱藏和取消隱藏。
CredentialPage.xml
<Button x:Name="Button_Round"
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" />
CredentialViewModel.cs
#region Bindable Command
[Obsolete]
public ICommand ProcessOffer => new Command(async () =>
{
var RegisteredPIN = await SecureStorage.GetAsync("RegisteredPIN");
string PIN = await App.Current.MainPage.DisplayPromptAsync("Enter PIN", null, "Ok", "Cancel", null, 6, Keyboard.Numeric);
if (PIN == RegisteredPIN)
{
try
{
//await _poolConfigurator.ConfigurePoolsAsync();
var agentContext = await _agentContextProvider.GetContextAsync();
var credentialRecord = await _credentialService.GetAsync(agentContext, _credential.Id);
var connectionId = credentialRecord.ConnectionId;
var connectionRecord = await _connectionService.GetAsync(agentContext, connectionId);
(var request, _) = await _credentialService.CreateRequestAsync(agentContext, _credential.Id);
await _messageService.SendAsync(agentContext.Wallet, request, connectionRecord);
await DialogService.AlertAsync("Request has been sent to the issuer.", "Success", "Ok");
}
catch (Exception e)
{
await DialogService.AlertAsync(e.Message, "Error", "Ok");
}
}
else if (PIN != RegisteredPIN && PIN != null)
{
DialogService.Alert("Provided PIN is not correct");
}
});
#endregion
我想隱藏/取消隱藏按鈕的條件
if(_credentialStatus == "Offered")
{
// Button should be Visible
}
else
{
// Hide the Button
}
使用 IsVisible 屬性:
<Button x:Name="Button_Round"
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}"
IsVisible="{Binding IsOfferButtonVisible}"
/>
然后在你的代碼后面
if(_credentialStatus == "Offered")
{
IsOfferButtonVisible = true;
}
else
{
// Hide the Button
IsOfferButtonVisible = false;
}
向您的 ViewModel 添加一個 bool 屬性,例如:
private bool _credentialVisible;
public bool CredentialVisible
{
get
{
return _credentialVisible;
}
set
{
if (_credentialVisible != value)
{
_credentialVisible = value;
NotifyPropertyChanged();
}
}
}
您的視圖 model 應該實現接口INotifyPropertyChanged
:
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
然后在您的視圖中綁定到IsVisible
屬性:
<Button x:Name="Button_Round"
IsVisible={Binding CredentialVisible}
WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
BorderWidth="2"
TextColor="White"
BorderColor="Teal"
BackgroundColor="Teal"
Text="Accept Offer"
Command="{Binding ProcessOffer}" />
您沒有指定何時需要檢查按鈕是否應該可見,但我想您在加載頁面時需要它,在這種情況下,您需要一種行為來在頁面出現時觸發命令,而不是自己編寫您可以使用以下 NuGet: https://www.nuget.org/packages/Behaviors.Forms/
安裝后,將命名空間添加到您的ContentPage
:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:behaviors="clr-namespace:Behaviors;assembly=Behaviors"
然后你可以按如下方式使用它:
<ContentPage.Behaviors>
<behaviors:EventHandlerBehavior EventName="Appearing">
<behaviors:InvokeCommandAction Command="{Binding AppearingCommand}" />
</behaviors:EventHandlerBehavior>
</ContentPage.Behaviors>
這將在頁面出現時調用AppearingCommand
方法,您可以在那里設置按鈕的可見性:
public ICommand AppearingCommand => new Command(() =>
{
if(_credentialStatus == "Offered")
{
CredentialsVisible = true;
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.