It should work fine. I copied your code and cleared it a bit, it shows the STOP button, then I
A few notes:
- use a short property where
<Button Text="X"/> possible, it's easier to read - when you add a XAML page, the IDE adds a .xaml.cs file to it and generates another .g.cs that you donβt see. The .g.cs file contains the generated code that finds all the x: Name'd elements and determines the placeholders for them, no need to search for them by name yourself
- all events triggered by the user interface are executed in the user interface thread, you do not need to do this explicitly
Here XAML, the same as yours, is even stronger and added margin so that the button is visible
<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; }); } }
source share