简体   繁体   中英

I can't use my label in codebehind?

I am trying to code an android app using Visual Studio .
I have looked through several tutorials but I just can't figure out why I can't edit my label within the codebehind file.

For example I want the label to disappear when I click the button . I declared an Eventhandler that works just fine, but I can't use my Label in my
C# Code:

<?xml version="1.0" encoding="utf-8" ?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App1"
         x:Class="App1.MainPage">
    <StackLayout>
        <Label x:Name="LblName1"  Text="Hallo"/>
        <Button x:Name="CmdButton1" Text="Folge mir zum Regelbuch" Margin="0,400,0,4" Clicked="CmdButton1_Click"/>
    </StackLayout>
</ContentPage>

I thought I would be able to use it since I declared it with x:name="" but I just can't reach it. I am a bloody beginner so I am sorry for the dump question...

Edit:

The Xaml-Code is contained in Mainpage.xaml and I want to access the label from Mainpage.xaml.cs if that is enough information. My simple C# Code lookes like that:

namespace App1
{
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void CmdRegelbuch_Click(object sender, EventArgs e)
    {
        LblName1.IsVisible=false;
    }
}
}

I hope this extended my question enough.

Hi and Welcome to Stackoverflow Maxam,

Have you set the DataContext correctly? Not the cleanest solution but a quick way to test it is to add DataContext = this; right after InitializeComponent(); .

Let's say the code provided is from MyUserControl.xaml , then you would have a CmdButton1_Click in your MyUserControl.xaml.cs (code behind) file.

There you should set the visiblity of the label.

private void CmdButton1_Click(object sender, RoutedEventArgs e)
{
    LblName1.Visibility = Visibility.Hidden;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM