我正在 MAUI .NET 中使用 MVVM 构建一个应用程序,但是当我尝试隐藏
Image
或 ImageButton
时,IsVisible
属性似乎不起作用(或者我不知道如何使用它)。它适用于其他元素,例如 Buttons
:
这是我的代码,XAML:
<ImageButton x:Name="previewImage"
HeightRequest="48"
WidthRequest="48"
Aspect="AspectFill"
Source="{Binding PreviewImageSource}"
IsVisible="{Binding IsPreviewVisible}"
Clicked="OnDeleteClicked"
BackgroundColor="Transparent" />
<Button ImageSource="trash_can.png"
WidthRequest="48"
HeightRequest="48"
Clicked="OnDeleteClicked"
IsVisible="{Binding IsDeleteButtonVisible}"/>
这是我的 C# 代码的一部分。:
private bool _isPreviewVisible;
public bool IsPreviewVisible
{
get => _isPreviewVisible;
set
{
_isPreviewVisible = value;
OnPropertyChanged(nameof(IsPreviewVisible));
}
}
private bool _isDeleteButtonVisible;
public bool IsDeleteButtonVisible
{
get => _isDeleteButtonVisible;
set
{
_isDeleteButtonVisible = value;
OnPropertyChanged(nameof(IsDeleteButtonVisible));
}
}
public void UpdateDeleteButtonVisibility()
{
// Check if gallery has any images
IsDeleteButtonVisible = Images.Any(); // _images is the ObservableCollection used for the gallery
}
public void UpdatePreviewImage()
{
IsPreviewVisible = false;
/*string folderPath = Path.Combine(FileSystem.AppDataDirectory, "Downloads");
var imagePaths = Directory.GetFiles(folderPath, "*.jpg").ToList();
if (imagePaths.Count > 0)
{
PreviewImageSource = imagePaths.First(); // Set the latest image
IsPreviewVisible = true;
}
else
{
PreviewImageSource = null; // Clear the image source
IsPreviewVisible = false; // Hide the button
}*/
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
在示例函数
UpdatePreviewImage
中,正如您可能已经看到的,我有意将其设置为 false
,但 ImageButton
仍然可见。我不确定我做错了什么,因为对于按钮来说它可以工作并且我可以隐藏它。有什么建议吗?
使用提供的代码片段无法重现该问题:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="MauiMvvmTest.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiMvvmTest"
x:Name="this"
x:DataType="local:MainPage"
BindingContext="{Reference this}">
<ScrollView>
<VerticalStackLayout Padding="30,0" Spacing="25">
<ImageButton
x:Name="previewImage"
Aspect="AspectFill"
BackgroundColor="Transparent"
Clicked="OnDeleteClicked"
HeightRequest="48"
IsVisible="{Binding IsPreviewVisible}"
Source="{Binding PreviewImageSource}"
WidthRequest="48" />
<Button
Clicked="OnDeleteClicked"
HeightRequest="48"
ImageSource="trash_can.png"
IsVisible="{Binding IsDeleteButtonVisible}"
WidthRequest="48" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
namespace MauiMvvmTest;
public partial class MainPage : ContentPage
{
private bool _isPreviewVisible = false;
public bool IsPreviewVisible
{
get => _isPreviewVisible;
set
{
_isPreviewVisible = value;
OnPropertyChanged(nameof(IsPreviewVisible));
}
}
private bool _isDeleteButtonVisible = true;
public bool IsDeleteButtonVisible
{
get => _isDeleteButtonVisible;
set
{
_isDeleteButtonVisible = value;
OnPropertyChanged(nameof(IsDeleteButtonVisible));
}
}
public MainPage()
{
InitializeComponent();
}
private void OnDeleteClicked(object sender, EventArgs e)
{
}
}