绑定到模型的XAML未更新UI

ego6inou  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(105)

我是毛伊岛.Net的新手。我看过James Montemagno的视频教程:https://www.youtube.com/watch?v=ddmZ6k1GIkM&list=PLdo4fOcmZ0oUBAdL2NwBpDs32zwGqb9DY&index=6。我可以成功地从一个页面导航到下一个页面,还可以发送一个复杂的数据对象作为参数。但是,在新页面上,我想使用参数通过ViewModel中的Web服务获取对象,并将xaml绑定到新获取的对象。首先,我在ViewModel的构造函数中尝试了这一点,但构造函数中的参数仍然为空。然后,我调用了一个事件,当绑定到的属性更改partial void OnLoginResultChanged(LoginResult value)时,该事件将被触发。在此,我调用了一个异步任务,以从Web服务获取对象,并尝试绑定和显示它们。不幸的是,我没有得到任何东西显示在xaml中。
下面是我的代码:视图模型:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MealPacApp.Model;

namespace MealPacApp.ViewModel
{
    [QueryProperty(nameof(Model.LoginResult), "LoginResult")]

    public partial class MainViewModel : ObservableObject
    {
        IConnectivity connectivity;
        Services.MealPacService mealPacService;

        [ObservableProperty]
        Model.User user = new();              

        [ObservableProperty]
        Model.LoginResult loginResult;

        partial void OnLoginResultChanged(LoginResult value)
        {
            this.GetDataForViewCommand.ExecuteAsync(null);
        }

        public MainViewModel(IConnectivity connectivity, Services.MealPacService mealPacService)
        {
            this.connectivity = connectivity;
            this.mealPacService = mealPacService;

            //Now check to see if there is internet connectivity
            if (connectivity.NetworkAccess != NetworkAccess.Internet)
            {
                Shell.Current.DisplayAlert("Oops!", "No internet is available. Please ensure you have an internet connection before continuing.", "OK");
                return;
            }
        }

        [RelayCommand]
        async Task GetDataForView()
        {
            try
            {
                if (loginResult != null)
                {
                    //Its not empty, so login here
                    var incmoingUser = await Services.MealPacService.GetUser(loginResult.Reference, loginResult.OrganisationId);
                    if (incmoingUser != null)
                    {
                        user = incmoingUser;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }   
    }
}

下面是视图(Xaml):

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MealPacApp.MainPage"
             Title="Home"
             xmlns:viewmodel="clr-namespace:MealPacApp.ViewModel"
             xmlns:model="clr-namespace:MealPacApp.Model"
             x:DataType="viewmodel:MainViewModel">

      
    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">          

            <Label
                Text="{Binding User.Name}"
                SemanticProperties.HeadingLevel="Level1"
                FontSize="32"
                HorizontalOptions="Center" />           

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

下面是我尝试绑定的模型:

namespace MealPacApp.Model
{

    public partial class User
    {
        public int Userid { get; set; }
        public string Reference { get; set; }
        public string Name { get; set; }
    }
}

下面是我的页面代码:

using MealPacApp.ViewModel;

namespace MealPacApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage(MainViewModel vm)
        {
            InitializeComponent();
            BindingContext = vm;
        }
    }
}

如前所述,导航工作正常,我只是不知道如何让属性更改反映在Xaml中。我保留了[ObservableProperty],而不是实现INotifyPropertyChanged,据我所知,它处理了锅炉板的东西。提前感谢。

np8igboo

np8igboo1#

这条线

user = incmoingUser;

正在更新私有字段user
您要更新公共属性User,该属性可观察

User = incmoingUser;

相关问题