Xamarin - 如何使用 MVVM 和数据库绑定标签

问题描述

我正在使用 Xamarin Forms MVVM。

如何使用 MVVM 绑定设置标签?我能够绑定 ProductID,但 ProductName 未绑定到标签。请参阅下文了解更多详情。

我认为问题是

public string ProductName

正在运行

public async Task OnAppearing()

ProductDetailView 简单的标签 UI 代码

    <StackLayout Orientation="Horizontal">
        <Label Text="{Binding ProductId}"></Label>
        <Label Text="{Binding ProductName}"></Label>
    </StackLayout>

ProductDetailviewmodel 我从上一页获取 ProductId 并使用 GetProduct() 方法从本地数据库返回一条记录。一旦我得到匹配的记录,我想用它来绑定 ProductName。出于某种原因,它没有绑定 ProductName

[QueryProperty(nameof(ProductId),nameof(ProductId))]
class ProductDetailviewmodel : INotifyPropertyChanged
{
        public async Task OnAppearing()
        {
            int TempId = int.Parse(ProductId);
            //get data from database
            ProductModel product = await ProductServices.GetProduct(TempId);
            ProductName = product.ProductName;
        }

        // Get Product ID from Prev Page 
        public string productId;
        public string ProductId
        {
            get { return productId; }
            set
            {
                productId = value;
                OnPropertyChanged();
            }
        }

        public string productName;
        public string ProductName
        {
            get { return productName; }
            set
            {
                productName = value;
                OnPropertyChanged();
            }
        }
}

ProductServices.cs GetProduct() 从 id 匹配的数据库中返回 1 条记录:

        public static async Task<ProductModel> GetProduct(int id)
        {
            await Init();

            // Get a specific note.
            var GetProduct = await db.Table<ProductModel>()
                                .Where(i => i.ProductId == id)
                                .FirstOrDefaultAsync();
            return GetProduct;
        }

解决方法

我已经将默认值设置为“hello”,但它仍然需要“Hello world”,因为这是从 OnAppearing() 设置的。

MainPage.xaml

 <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
     <Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36" />
 </Frame>
 <StackLayout>
     <Label Text="{Binding sample}" FontSize="Title" Padding="30,10,30,10" />
 </StackLayout>

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    MainPageViewModel viewModel = new MainPageViewModel();

    public MainPage()
    {
        InitializeComponent();
        this.BindingContext = viewModel;
    }

    protected override void OnAppearing()
    {
        viewModel.OnAppearing();
    }
}

MainPageViewModel

public class MainPageViewModel : INotifyPropertyChanged
{
    public MainPageViewModel()
    {
    }

    public void OnAppearing()
    {
        sample = "Hello world";
    }

    private string _sample = "hello";
    public string sample
    {
        get { return _sample; }
        set { _sample = value; OnPropertyChanged("sample"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this,new PropertyChangedEventArgs(propertyName));
        }
    }
}

输出

enter image description here