更改 Xamarin Forms XAML 按钮的 isVisible 属性 [英] Changing isVisible property of Xamarin Forms XAML buttons

查看:36
本文介绍了更改 Xamarin Forms XAML 按钮的 isVisible 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Xamarin Forms ContentPage 中动态显示/隐藏按钮.我的 XAML 代码中有两个按钮:

I am trying to dynamically show/hide button inside Xamarin Forms ContentPage. I have two buttons in my XAML code:

<StackLayout Orientation="Vertical">

    <Button x:Name="start_btn" Clicked="startPanic">
        <Button.Text>START</Button.Text>
    </Button>

    <Button x:Name="stop_btn" IsVisible="false">
        <Button.Text>STOP</Button.Text>
    </Button>

</StackLayout>

对应的C#代码:

public partial class PanicPage : ContentPage
{
    private Button startBtn;
    private Button stopBtn;

    public PanicPage ()
    {
        InitializeComponent ();
        startBtn = this.FindByName<Button> ("start_btn");
        stopBtn = this.FindByName<Button> ("stop_btn");
    }

    private void startPanic(object sender, EventArgs args){
        Device.BeginInvokeOnMainThread (() => {
            startBtn.IsVisible = false;
            stopBtn.IsVisible = true; //  DOESN'T WORK, button still will be hidden
        });
    }
}

当我在 XAML 中设置 isVisible 属性时,它不会对事件方法 (startPanic) 中的任何属性更改做出反应.我该如何解决?

When I set isVisible property in XAML, it doesn't react for any property change in event method (startPanic). How can I fix it?

推荐答案

更改 xmal 文件中的代码并编写开始和停止按钮的属性

Change your code in xmal file and write properties for start and stop button

<Button x:Name="start_btn" Clicked="startPanic" IsVisible="{Binding IsStartVisible}">
    <Button.Text>START</Button.Text>
</Button>

<Button x:Name="stop_btn" IsVisible="{Binding IsStopVisible}">
    <Button.Text>STOP</Button.Text>
</Button>

在 ViewModel 中为开始按钮编写以下属性和类似内容,并根据您的逻辑设置 IsStopVisible =true/false

In ViewModel write following property and similar for start button and set IsStopVisible =true/false based on your logic

   private bool _isStopVisible;

    public bool IsStopVisible{
        get {
            return _isStopVisible;
        }
        set {
            _isStopVisible= value;
            RaisePropertyChanged ("IsStopVisible");
        }
    }

这篇关于更改 Xamarin Forms XAML 按钮的 isVisible 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆