从后面的代码来看,IsVisible 属性不适用于 MAUI.NET for Android 中的图像或 ImageButtons

问题描述 投票:0回答:1

我正在 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
仍然可见。我不确定我做错了什么,因为对于按钮来说它可以工作并且我可以隐藏它。有什么建议吗?

c# android maui imagebutton maui-android
1个回答
0
投票

使用提供的代码片段无法重现该问题:

  1. 启动了新的 .NET MAUI 应用程序
  2. 用您的代码片段替换 MainPage.xaml
  3. 用您的视图模型替换 MainPage.xaml.cs
  4. 设置 BindingContext
  5. 设置 x:DataType 进行编译绑定
<?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)
    {

    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.