C# 更改 Xamarin Forms XAML 按钮的 isVisible 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29823619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing isVisible property of Xamarin Forms XAML buttons
提问by dease
I am trying to dynamically show/hide button inside Xamarin Forms ContentPage. I have two buttons in my XAML code:
我正在尝试在 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>
Corresponding C# code:
对应的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
});
}
}
When I set isVisible property in XAML, it doesn't react for any property change in event method (startPanic). How can I fix it?
当我在 XAML 中设置 isVisible 属性时,它不会对事件方法 (startPanic) 中的任何属性更改做出反应。我该如何解决?
采纳答案by Drw
Change your code in xmal file and write properties for start and stop button
更改 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>
In ViewModel write following property and similar for start button and set IsStopVisible =true/false based on your logic
在 ViewModel 中为开始按钮编写以下属性和类似内容,并根据您的逻辑设置 IsStopVisible =true/false
private bool _isStopVisible;
public bool IsStopVisible{
get {
return _isStopVisible;
}
set {
_isStopVisible= value;
RaisePropertyChanged ("IsStopVisible");
}
}
回答by Sten Petrov
It should work just fine. I copied your code and cleaned it up a bit, it shows the STOP button, then I
它应该工作得很好。我复制了你的代码并清理了一下,它显示了停止按钮,然后我
A few remarks:
几点说明:
- use the short property where possible
<Button Text="X"/>, it's easier to read - when you add a XAML page the IDE adds a .xaml.cs file next to it and generates another .g.cs that you don't see. The .g.cs file contains generated code that finds all the x:Name'd elements and defines placeholders for them, no need to find them by name yourself
- all UI-initiated events are executed on the UI thread, no need to do that explicitly
- 尽可能使用 short 属性
<Button Text="X"/>,这样更容易阅读 - 当您添加 XAML 页面时,IDE 会在它旁边添加一个 .xaml.cs 文件并生成另一个您看不到的 .g.cs。.g.cs 文件包含生成的代码,用于查找所有 x:Name'd 元素并为它们定义占位符,无需自己按名称查找它们
- 所有 UI 启动的事件都在 UI 线程上执行,无需显式执行
Here's the XAML, same as yours just tighter and added Margin so the button is visible
这是 XAML,与您的相同,只是更紧并添加了 Margin,因此按钮可见
<StackLayout Orientation="Vertical" Margin="20">
<Button x:Name="start_btn" Clicked="startPanic" Text="START" />
<Button x:Name="stop_btn" Text="STOP" IsVisible="false" />
</StackLayout>
And the code behind:
以及背后的代码:
public partial class TestPage : ContentPage
{
public TestPage ()
{
InitializeComponent ();
}
private void startPanic(object sender, EventArgs args){
Device.BeginInvokeOnMainThread (() => {
start_btn.IsVisible = false;
stop_btn.IsVisible = true;
});
}
}
回答by vebs
Use the Visibility property of view.
使用视图的可见性属性。
for example if u want to make your button invisible you can do
例如,如果你想让你的按钮不可见,你可以这样做
if(condition)
{
button.Visibility=ViewStates.Invisible;
}
else
{
button.Visibility=ViewStates.Visible;
}
回答by atapi19
Maybe I'm late but I was searching this too without success. This may be useful for someone.
也许我迟到了,但我也在搜索这个没有成功。这可能对某人有用。
objectView.SetValue(IsVisibleProperty, false); // the view is GONE, not invisible
objectView.SetValue(IsVisibleProperty, true);

