Is it possible to dynamically change a ContentView Content?

HarardinHarardin Member ✭✭

Hi I am trying to dynamically set ContentView of a ContentPage from code. Everything can be changed besides this one. For now I found only how to change ContentView directly from xaml file xmlns:views="clr-namespace:Your.Name.Space" -> <views:NameOfView /> But this is not actualy dynamic.
Here is some excample.
<StackLayout><ContentView x:Name="ViewTestName"/></StackLayout>
And the background code.

public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            ViewTestName = (ContentView)Activator.CreateInstance(FindByName<View1>);
        }
    }
}

Can someone please give me a qlue how to do it from backgroud cs. file?

Best Answers

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @Hararin

    In answer to your question "Is it possible to dynamically change a ContentView Content?", the answer it yes.

    As to the code that you have shown, it's unclear quite what you are trying to do. However, as an example, if you want the ContentView to contain a Label, you would do:

    ViewTestName.Content = new Label {};
    

    Your code appears to be trying to replace the ContentView, not the ContentView.Content

  • HarardinHarardin Member ✭✭

    @JohnHardman said:
    @Hararin

    In answer to your question "Is it possible to dynamically change a ContentView Content?", the answer it yes.

    As to the code that you have shown, it's unclear quite what you are trying to do. However, as an example, if you want the ContentView to contain a Label, you would do:

    ViewTestName.Content = new Label {};
    

    Your code appears to be trying to replace the ContentView, not the ContentView.Content

    I want to, one ContentView contain another ContentView, like if user pushes a button the another ContentView loads on the screen.

    View1 is actually ContentView file.

  • HarardinHarardin Member ✭✭
    edited June 11

    @LeonLu said:
    You could achieve it like this, firstly, you could set content of ViewTestName to a stacklayout, then add the button to the stacklayout, if click the button, then add another ContentView to show the content.

    Thank you you gave me a few ideas

    Here is what I got from it but page is blank and still but compile without errors.

    This is the ContentView file inside (had to remove the standard schema links):

    <ContentView
                 mc:Ignorable="d"
                 x:Class="App.Views.View1">
      <ContentView.Content>
          <Label Text="This is test text" />
      </ContentView.Content>
    </ContentView>
    

    Here is the ContentPage xaml file:

    <ContentPage
                 mc:Ignorable="d"
                 x:Class="App.MainPage">
        <StackLayout>
            <ContentView x:Name="CView"/>
        </StackLayout>
    </ContentPage>
    

    And the actual code that I executing:

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            ContentView content = new Views.View1();
            CView = content;
        }
    }
    

    I also tried your code but gave me the same blank page result. Looks like ContentView xaml is loading but not showing anything.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

  • HarardinHarardin Member ✭✭

    @LeonLu said:
    @Hararin Please change ContentView content = new Views.View1(); CView = content; to CView.Content = new Views.View1();

    Thank you very much, this worked.

    I marked your answer as right.

  • JohnHardmanJohnHardman GBUniversity mod

    @Hararin said:

    @JohnHardman said:
    @Hararin

    In answer to your question "Is it possible to dynamically change a ContentView Content?", the answer it yes.

    As to the code that you have shown, it's unclear quite what you are trying to do. However, as an example, if you want the ContentView to contain a Label, you would do:

    ViewTestName.Content = new Label {};
    

    Your code appears to be trying to replace the ContentView, not the ContentView.Content

    I want to, one ContentView contain another ContentView, like if user pushes a button the another ContentView loads on the screen.

    View1 is actually ContentView file.

    In which case, you would just change the Label in the above line of code to ContentView

    ViewTestName.Content = new ContentView { /* other stuff here */};
    
Sign In or Register to comment.