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

前端之家收集整理的这篇文章主要介绍了c# – 更改Xamarin Forms XAML按钮的isVisible属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在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");
        }
    }
原文链接:https://www.f2er.com/csharp/244417.html

猜你在找的C#相关文章