Forum Xamarin.Forms

How to access ContentView objects (x:Name) when you include this contentview in a page?

voidstreamvoidstream FRMember ✭✭✭
edited December 2016 in Xamarin.Forms

Hello,

I got this page:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Manager.Pages.EstablishmentCreateUser"
             xmlns:local="clr-namespace:APP.Views;assembly=APP"
             Title="Create user">
    <local:UserInfos/>
</ContentPage>

And i got this ContentView:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="APP.Views.UserInfos">
    <ContentView.Content>
        <StackLayout Orientation="Vertical">
            <Entry x:Name="E_Firstname" Placeholder="Prénom"/>
            <Entry x:Name="E_Lastname" Placeholder="Nom"/>
            <Entry x:Name="E_RegistrationNumber" Placeholder="Matricule"/>
            <Entry x:Name="E_Phone" Placeholder="Téléphone fixe"/>
            <Entry x:Name="E_Mobile" Placeholder="Téléphone mobile"/>
            <Entry x:Name="E_Email" Placeholder="Adresse électronique"/>
            <Entry x:Name="E_Adress" Placeholder="Adresse du domicile"/>
            <Picker x:Name="P_Job"/>
            <Picker x:Name="P_WeeklyHours"/>
            <StackLayout Orientation="Horizontal">
                <Label x:Name="L_Intermittent" Text="Intermittent"/>
                <Switch x:Name="S_Intermittent"/>
            </StackLayout>
        </StackLayout>
    </ContentView.Content>
</ContentView>

My design works fine but i'm trying to access :xName entries from my ContentView when i'm in my Page.
How i can access it?

Best regards

Best Answers

  • AlexDunnAlexDunn US ✭✭✭
    edited December 2016 Accepted Answer

    Ideally you would do this through Bindable properties, but if you want to just get access to these controls as properties, you could add wrappers in your ContentView class.
    Example using your class names:

    public class UserInfos : ContentView
    {
        public Label FirstNameLabel
        {
            get 
            {
                return E_Firstname;
            }
        }
    }
    

    Then your parent page would need to apply an x:Name to your ContentView:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Manager.Pages.EstablishmentCreateUser"
                 xmlns:local="clr-namespace:APP.Views;assembly=APP"
                 Title="Create user">
        <local:UserInfos x:Name="UserInfos"/>
    </ContentPage>
    

    Then the code behind of your page can access it:

    public class EstablishmentCreateUser : ContentPage
    {
        public void DoSomething()
        {
            UserInfos.FirstNameLabel.Text = "Stuff";
        }
    }
    

Answers

  • AlexDunnAlexDunn USMember ✭✭✭
    edited December 2016 Accepted Answer

    Ideally you would do this through Bindable properties, but if you want to just get access to these controls as properties, you could add wrappers in your ContentView class.
    Example using your class names:

    public class UserInfos : ContentView
    {
        public Label FirstNameLabel
        {
            get 
            {
                return E_Firstname;
            }
        }
    }
    

    Then your parent page would need to apply an x:Name to your ContentView:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Manager.Pages.EstablishmentCreateUser"
                 xmlns:local="clr-namespace:APP.Views;assembly=APP"
                 Title="Create user">
        <local:UserInfos x:Name="UserInfos"/>
    </ContentPage>
    

    Then the code behind of your page can access it:

    public class EstablishmentCreateUser : ContentPage
    {
        public void DoSomething()
        {
            UserInfos.FirstNameLabel.Text = "Stuff";
        }
    }
    
  • this my contentview. xaml

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

    <ContentView.Content>

    <Grid.RowDefinitions>



    </Grid.RowDefinitions>

                <Grid Grid.Row="0" Margin="0, 50" VerticalOptions="Center">
                    <Image Source="location_guy" 
                       Style="{StaticResource OnboardingIconStyle}"/>
                </Grid>
            <StackLayout Grid.Row="1" VerticalOptions="Center" Padding="30,0">
                <Label Text="Not Available..." Style="{DynamicResource MapTitleStyle}" />
                <Label Text="Sorry, this feature is currently not available for this location"
                       Style="{DynamicResource MapTitleTextStyle}"
                       Margin="0,15,0,15" />
            </StackLayout>
        <Grid Grid.Row="2" Margin="20,50" VerticalOptions="Center">
            <Button  Text="See my site info"
                               Style="{DynamicResource MapTitleTextStyle}"
                               Command="{Binding ReportWorkspaceIssueCommand}"
                               Margin="0,40,0,0"
                               HorizontalOptions="FillAndExpand"/>
            <Label Grid.Column="4" 
                              Text="Change my location"
                              Margin="10,10,0,10"
                              Style="{DynamicResource MapTitleTextStyle}"
                              HorizontalOptions="CenterAndExpand"/>
    
        </Grid>
    </Grid>
    </ContentView.Content>
    

    using Aria.Helpers;
    using Aria.Managers;
    using Aria.Services;
    using Aria.ViewModels;
    using Common.Helpers;
    using Common.Services;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;

    namespace Aria.Pages.Map
    {
    [XamlCompilation(XamlCompilationOptions.Compile)]

    public partial class MapNotAvailableView : ContentView
    {     
        public MapNotAvailableView ()
        {
            InitializeComponent ();
    
        }
    }
    

    }
    how to add content view in content page below is my content page .in this page how to include content view .
    <?xml version="1.0" encoding="utf-8" ?>
    <common:BaseContentPage x:TypeArguments="viewModels:MapPageViewModel"
    xmlns:viewModels="clr-namespace:Aria.ViewModels"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             xmlns:cviews="clr-namespace:Aria.Views"                     
             xmlns:common="clr-namespace:Aria.Pages.Common"
                        xmlns:local="clr-namespace:Aria.Pages.Map"
                        x:Class="Aria.Pages.Home.MapPage"
             Title="Spaces"
             Icon="space.png"
             BackgroundColor="{DynamicResource BackgroundColor}">
    
    <RelativeLayout  x:Name="MapViewdata">
    
        <cviews:MapWebView
            x:Name="mapView" 
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" 
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}" 
            AutomationId="cnvsVwMap"/>
    
        <cviews:MapFilterView 
            x:Name="mapFilterView"
            BackgroundColor="Transparent"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" 
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-50}"
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X, Factor=1, Constant=0}" />
    </RelativeLayout>
    

    how to add content view in content page above is my content page .in this page how to include content view .

  • voidstreamvoidstream FRMember ✭✭✭

    I don't understand your request but, if you want import a customView (ContentView, StackLayout etc...) to another View/Page you should import the namespace to the main view/page.

    For example by XAML:

    <ContentPage  xmlns:common="clr-namespace:Aria.Pages.Common">
        <common:MyCustomContentView/>
        <common:MyCustomControl/>
        <common:MyCustomMap/>
    </ContentPage>
    
  • alebrosalebros Member

    I handled this editing the xaml.g.cs file (you can access in Visual Studio by expanding the xaml page in solution explorer and double clicking the page objects.

    I needed to access to the ResponseLabel object and I added the following lines (it is the same as if you drag the label from the toolbox to the design page).

       [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::Xamarin.Forms.Label ResponseLabel;
    
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private void InitializeComponent() {
            global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(FreightView));
            ResponseLabel = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.Label>(this, "ResponseLabel");
        }
    

    I don't know if it is the correct approach, but it works for me.

Sign In or Register to comment.