更改Xamarin Forms XAML按钮的isVisible属性

dea*_*ase 6 c# xaml xamarin.ios xamarin xamarin.forms

我想在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>
Run Code Online (Sandbox Code Playgroud)

对应的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
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Dny*_*esh 9

在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>
Run Code Online (Sandbox Code Playgroud)

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

私人布尔_isStopVisible;

   private bool _isStopVisible;

    public bool IsStopVisible{
        get {
            return _isStopVisible;
        }
        set {
            _isStopVisible= value;
            RaisePropertyChanged ("IsStopVisible");
        }
    }
Run Code Online (Sandbox Code Playgroud)