ContentView Xamarin 表单

我仍在学习 Xamarin Forms 和 C#。所以在我的 MainPage.xaml 我有这个:


    <StackLayout>

        <Button Text="bttn1" Clicked="Button_Clicked"/>

        <Button Text="bttn2" Clicked="Button_Clicked_1"/>


        <ContentView x:Name="DisplayCustomContentView">


        </ContentView>

    </StackLayout>

和两个内容视图:


视图 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"}

            }

        };

    }

}

视图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"}

            }

        };

    }

}

所以当我点击按钮时,我想在视图之间交换。我试过这个:


   private void Button_Clicked(object sender, EventArgs e)

    {

        DisplayCustomContentView = new View1();

    }

我如何从Entry字段中获取值?


我很确定我做的方法不对。我可以使用我能得到的所有帮助!


千巷猫影
浏览 176回答 2
2回答

慕姐8265434

回答你的问题:以及如何从 Entry 字段中获取值?有简单的答案,也有不那么简单的答案。简单的答案,因为您在 C#(而不是 XAML)中构建 View1 和 View2,是存储对 Entry 对象的引用:public class View1 : ContentView{&nbsp; &nbsp; public Entry Entry1 { get; private set; }&nbsp; &nbsp; public Entry Entry2 { get; private set; }&nbsp; &nbsp; public View1 ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Entry1 = new Entry { Placeholder = "entry1 View 1" };&nbsp; &nbsp; &nbsp; &nbsp; Entry2 = new Entry { Placeholder = "entry1 View 2" };&nbsp; &nbsp; &nbsp; &nbsp; Content = new StackLayout {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Children = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Label { Text = "View 1" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Entry1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Entry2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }}然后,在某些按钮回调或命令中(或每当您的程序获得控制权时),您可以查看 Entry1.Text 和 Entry2.Text 属性以查看用户输入的内容。现在介绍不那么简单的方法,MVVM 和模型绑定。虽然此代码涉及更多,但它是编写 Xamarin.Forms 应用程序的一种非常流行的方式,因为它使您能够更好地将视图代码与逻辑的其余部分分开。这有助于分离应用程序中的关注点,以提高可测试性、可维护性等。您可以使用 Xamarin 内置功能来做到这一点,但很多人喜欢使用各种 MVVM 包来进一步支持 MVVM 原则。要使用 View1(仅使用内置的 Xamarin.Forms 功能)给出一个简单的说明,您还需要创建一个 View1ViewModel 类,如下所示:public class View1ViewModel{&nbsp; &nbsp; public string Entry1 { get; set; }&nbsp; &nbsp; public string Entry2 { get; set; }}public class View1 : ContentView{&nbsp; &nbsp; public View1ViewModel ViewModel { get; private set; }&nbsp; &nbsp; public View1 ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ViewModel = BindingContext = new View1ViewModel();&nbsp; &nbsp; &nbsp; &nbsp; var entry1 = new Entry { Placeholder = "entry1 View 1" };&nbsp; &nbsp; &nbsp; &nbsp; var entry2 = new Entry { Placeholder = "entry1 View 2" };&nbsp; &nbsp; &nbsp; &nbsp; entry1.SetBinding(Entry.TextProperty, "Entry1");&nbsp; &nbsp; &nbsp; &nbsp; entry2.SetBinding(Entry.TextProperty, "Entry2");&nbsp; &nbsp; &nbsp; &nbsp; Content = new StackLayout {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Children = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Label { Text = "View 1" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }}在此模型中,其他代码可以查看视图模型,而不是直接查看底层 UI 元素(Entry 对象)上的属性,因为这些属性会在条目值更改时自动更新。虽然此示例代码会随着模型绑定的添加而变得更大,但在使用 XAML 时它往往会更简洁一些。当然,还有 MVVM 的其他方面(INotifyPropertyChanged 等),我鼓励您了解更多信息,但这有点超出了原始问题的范围。

慕工程0101907

您需要设置 Content property。private void Button_Clicked(object sender, EventArgs e){&nbsp; &nbsp; DisplayCustomContentView.Content = new View1();}
打开App,查看更多内容
随时随地看视频慕课网APP