【问题标题】:Xamarin - How to Bind Label using MVVM and DatabaseXamarin - 如何使用 MVVM 和数据库绑定标签
【发布时间】:2021-05-07 22:24:31
【问题描述】:

我正在使用 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;
        }

【问题讨论】:

  • OnAppearing 是页面的生命周期事件,将在页面中自动调用。您在 ViewModel 中使用它,它只是一个必须显式调用的普通方法。
  • 是的,OnAppearing(方法在 ProductDeatilView 页面中。我将其与 ViewModel OnAppearining 方法绑定。换句话说,View 中的 OnAppearing 方法被调用到 ViewModel OnAppearining
  • 您确定 ProductName 有值吗?您是否在页面中设置了 BindingContext?
  • 您确定要将 BindingContext 设置为 ProductDetailViewModel 吗?
  • 为了测试,将ProductName = "test"; 放入OnAppearing。并发布您的绑定代码

标签: c# xamarin xamarin.forms


【解决方案1】:

我已将默认值设置为“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));
        }
    }
}

输出

【讨论】:

    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多