Cant reach x:Name from xaml file in c# back-end.

kylebrkylebr Member

Hey guys, I am new to xamarin cross-platform app development and I am stuck with a problem.

I am using visual studio 2017 15.9.9 (which is the latest version up till now)

As you see in the code below i gave the image the label 'image' to use in back-end c#




I can't reach image in c# code:

public partial class MainPage : ContentPage
{
    public MainPage()
    {

        InitializeComponent();

        image.         //I cant reach image

    }
}

It keeps saying 'the name 'image' does not exist in the current context.

Can someone please help me out with this?

Answers

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @kylebr said:
    Hey guys, I am new to xamarin cross-platform app development and I am stuck with a problem.

    I am using visual studio 2017 15.9.9 (which is the latest version up till now)

    As you see in the code below i gave the image the label 'image' to use in back-end c#




    I can't reach image in c# code:

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
           
            InitializeComponent();
          
            image.         //I cant reach image
    
        }
    }
    

    It keeps saying 'the name 'image' does not exist in the current context.

    Can someone please help me out with this?

    Show your XAML too...

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    Where did you declare the Image. If you add the Image in DataTemplate, the error will occur. The views inside of the DataTemplate are not available or even created until you assign a source that resolves to the corresponding view.

    Could you post the code of declaring Image in page.xaml, it'll be helpful to find a solution.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The fact that you're even trying to reach the UI from the code behind is probably a hint of a problem.
    99% of the time when UI elements from XAML are trying to be reached from anyplace other than other XAML UI elements via x:reference for the purpose of referencial binding... Its because the developer doesn't know any better.

    So let's go after the problem and not the symptom - let's teach you the right way to acheive what you're trying to do - instead of figuring out how to enable you to do something badly.

    What is it you are trying to do? Why do you think you need to reach the UI element from the C#?

Sign In or Register to comment.