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

查看:248
本文介绍了更改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天全站免登陆