简体   繁体   中英

ContentView Xamarin Forms

I'm still learning Xamarin Forms and C# in general. So in my MainPage.xaml i have this:

    <StackLayout>
        <Button Text="bttn1" Clicked="Button_Clicked"/>
        <Button Text="bttn2" Clicked="Button_Clicked_1"/>

        <ContentView x:Name="DisplayCustomContentView">

        </ContentView>
    </StackLayout>

and two ContentViews:

View 1:

public class View1 : ContentView
{
    public View1 ()
    {
        Content = new StackLayout {
            Children = {
                new Label { Text = "View 1" },
                new Entry {Placeholder = "entry1 View 1"},
                new Entry {Placeholder = "entry2 View 1"}
            }
        };
    }
}

View 2:

public class View2 : ContentView
{
    public View2 ()
    {
        Content = new StackLayout {
            Children = {
                new Label { Text = "View 2" },
                new Entry {Placeholder = "entry2 View 1"},
                new Entry {Placeholder = "entry2 View 2"}
            }
        };
    }
}

So I want to swap between the views when I click on the buttons. I have tried this:

   private void Button_Clicked(object sender, EventArgs e)
    {
        DisplayCustomContentView = new View1();
    }

And how can i get the values from the Entry fields?

I'm mostly certain that I'm doing not the right way. And I could use all the help I can get!

You need to set the Content property .

private void Button_Clicked(object sender, EventArgs e)
{
    DisplayCustomContentView.Content = new View1();
}

To answer your question:

And how can i get the values from the Entry fields?

There's the simple answer, and the not-so-simple answer.

The simple answer, since you're building View1 and View2 in C# (rather than XAML), is to store a reference to the Entry objects:

public class View1 : ContentView
{
    public Entry Entry1 { get; private set; }
    public Entry Entry2 { get; private set; }

    public View1 ()
    {
        Entry1 = new Entry { Placeholder = "entry1 View 1" };
        Entry2 = new Entry { Placeholder = "entry1 View 2" };

        Content = new StackLayout {
            Children = {
                new Label { Text = "View 1" },
                Entry1,
                Entry2
            }
        };
    }
}

Then, in some button callback or Command (or whenever your program gets control), you can look at the Entry1.Text and Entry2.Text properties to see what the user entered.

Now on to the not so simple way, MVVM and model binding. While the code for this gets a bit more involved, it's a very popular way to write Xamarin.Forms applications, as it gives you better ability to separate your view code from the rest of the logic. This can help separate concerns within your application to improve testability, maintainability, etc. You can do this with Xamarin built-in features, but a lot of people like to use various MVVM packages to further support MVVM principles.

To give a simple illustration using View1 (using just built-in Xamarin.Forms features), you would also create a View1ViewModel class, as such:

public class View1ViewModel
{
    public string Entry1 { get; set; }
    public string Entry2 { get; set; }
}

public class View1 : ContentView
{
    public View1ViewModel ViewModel { get; private set; }

    public View1 ()
    {
        ViewModel = BindingContext = new View1ViewModel();
        var entry1 = new Entry { Placeholder = "entry1 View 1" };
        var entry2 = new Entry { Placeholder = "entry1 View 2" };

        entry1.SetBinding(Entry.TextProperty, "Entry1");
        entry2.SetBinding(Entry.TextProperty, "Entry2");

        Content = new StackLayout {
            Children = {
                new Label { Text = "View 1" },
                entry1,
                entry2
            }
        };
    }
}

In this model, rather than directly looking at properties on the underlying UI elements (the Entry objects), other code can look at the view model, as those properties get updated automatically when the entry's value changes.

While this sample code does get bigger with the addition of model binding, it tends to be a bit more concise when using XAML.

There are, of course, additional aspects of MVVM (INotifyPropertyChanged, etc.), and I encourage you to learn more, but that goes a bit beyond the scope of the original question.

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