如何使用MVVM Xamarin表单处理图像可见性



我有一个图像我可以绑定到XAML的图像属性,问题是,当我弹出我的视图我希望图像保持不可见,然后当我点击我的按钮再次出现到目前为止,我在m ViewModel中得到了这个:

private string imageSource;

public string ImageSource
{
    get { return imageSource; }
    set { imageSource = value; Notify("ImageSource"); }
}
public bool State { get { return false; }}

private Image visibleImage;
public Image VisibleImage
{
    get { return new Image {IsVisible = State,Source = ImageSource }; }
    set { visibleImage = value; Notify("VisibleImage"); Notify("State"); }
}

在我的BindingContext属性IsVisible被设置为false,但不工作!

您可以通过完全避免在视图模型中保留Image引用来简化示例。像这样修改你的State标志:

private boolean _state;
public boolean State { 
    get { return _state; } 
    set { _state = value; Notify("State"); } 
}

在XAML中声明Image及其源和可见性绑定:

<Image Source="{Binding ImageSource}" IsVisible="{Binding State}" />

初始化视图模型时,将State设置为false。然后单击按钮需要将标志设置为true以使图像可见。

最新更新