绑定来自不同视图的值 Xamarin

问题描述

我是 Xamarin 的新手,这是一次非常令人沮丧的经历。经过一段时间尝试学习基础知识并观看一些教程后,我终于设法掌握了绑定对象,似乎我终于准备好继续前进了。

好吧,我错了。我正在尝试练习如何绑定来自不同视图的对象(我认为这应该很简单),但我所做的一切似乎都不起作用。

我有一个带有绑定对象的视图,我希望将其反映到下一个视图中。我认为只需将它们绑定到相同的变量并使用相同的视图模型就足以将第一页上的输入条目打印在第二页标签中。但这并没有发生。任何人都可以提供帮助,或参考我可以从中学习的优秀初学者材料吗?

第一次查看 XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Conferencia.Views.AddBagPage"
             xmlns:viewmodels="clr-namespace:Conferencia.viewmodels"
             x:DataType="viewmodels:AddBagviewmodel">
    <ContentPage.BindingContext>
        <viewmodels:AddBagviewmodel/>
    </ContentPage.BindingContext>
    <Grid RowDeFinitions="*,*,Auto,*">
        <Entry
                Text="{Binding RegCodeText}"
                Grid.Row="2"
                HorizontalOptions="Center"
                Placeholder="00000000000000000"/>
        <Label 
                x:Name="RegLabel"
                Text="{Binding RegCodeText}"
                FontSize="Medium"
                Grid.Row="3"
                HorizontalOptions="CenterandExpand" />
        <Button
            Command="{Binding NewRegister}"
            Grid.Row="5"
            Text="Registrar"
            HorizontalOptions="Center"
            />
    </Grid>
</ContentPage>

第一次查看 cs

using Conferencia.viewmodels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Conferencia.Views
{
    [XamlCompilation(XamlCompilationoptions.Compile)]
    public partial class AddBagPage : ContentPage
    {
        public AddBagPage()
        {
            InitializeComponent();
            BindingContext =  new AddBagviewmodel();

        }

    }
}

第二页 XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Conferencia.Views.CreateBagListPage"
             xmlns:viewmodels="clr-namespace:Conferencia.viewmodels"
             x:DataType="viewmodels:AddBagviewmodel">
    <ContentPage.BindingContext>
        <viewmodels:AddBagviewmodel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <Label Text="{Binding RegCodeText}"
                VerticalOptions="CenterandExpand" 
                HorizontalOptions="CenterandExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

第二页 cs

using Conferencia.viewmodels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Conferencia.Views
{
    [XamlCompilation(XamlCompilationoptions.Compile)]
    public partial class CreateBagListPage : ContentPage
    {
        public CreateBagListPage()
        {
            InitializeComponent();
            BindingContext = new AddBagviewmodel();
        }
    }
}

查看模型

using MvvmHelpers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;

namespace Conferencia.viewmodels
{
    public class AddBagviewmodel : ObservableObject
    {

        public AddBagviewmodel()
        {
            NewRegister = new Command(OnNewRegister);
        }
        public ICommand NewRegister { get; }
        async void OnNewRegister()
        {
            await Shell.Current.GoToAsync("CreateBagListPage");
        }

        public string regCode = "";
        public string RegCodeText
        {
            get => regCode;
            set => SetProperty(ref regCode,value);
        }

    }
}

解决方法

正如 Jason 所说,在 app.xml 上实例化 VM 成功

<Application.Resources>
   <viewmodels:AddBagViewModel x:Key="AddBagViewModel" />
<Application.Resources>

像这样调用它

BindingContext="{StaticResource AddBagViewModel}"