加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 更改Xamarin Forms XAML按钮的isVisible属性

发布时间:2020-12-15 08:40:04 所属栏目:百科 来源:网络整理
导读:我想在Xamarin Forms ContentPage中动态显示/隐藏按钮. 我的XAML代码中有两个按钮: StackLayout Orientation="Vertical" Button x:Name="start_btn" Clicked="startPanic" Button.TextSTART/Button.Text /Button Button x:Name="stop_btn" IsVisible="false
我想在Xamarin Forms ContentPage中动态显示/隐藏按钮.
我的XAML代码中有两个按钮:
<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)中的任何属性更改做出反应.我该如何解决?

解决方法

在xmal文件中更改代码并写入启动和停止按钮的属性
<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

私人布尔_isStopVisible;

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读