为什么ViewModel Binding仅在调试时起作用,而在设备上部署和安装后却不起作用?

时间:2019-03-27 22:04:32

标签: c# xamarin.forms xamarin.android

我正在创建一个xamarin表单应用程序,并使用bindingContext将viewmodel绑定到视图...在调试和Visual Studio Android模拟器上工作正常,但是当 我将其部署并安装在无法运行的Android设备上,并显示空标签

这是MainPage.xaml.cs:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        var model = new GoodViewModel();
        BindingContext = model;
    }
}

这是MainPage.xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Good"
             x:Class="QuranProTst.MainPage">

    <StackLayout>
        <Label Text="{Binding Good.Title}" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" 
           TextColor="BlueViolet"
               />
    </StackLayout>

</ContentPage>

这是GoodViewModel:

private readonly DataContext _db;
public GoodViewModel()
{
    _db = new DataContext();
    Good = _db.Goods.First(g => g.Id == 1);
    Goods = _db.Goods.ToList();
}

public List<Good> Goods { get; set; }
public Good Good { get; set; }

这是DataContext:

public class DataContext
    {
        public DataContext()
        {
            FillData();
        }
        public IList<Good> Goods { get; set; }  

        public void FillData()
        {
            var goodlist = new List<Good>();
            for (int i = 0; i < 5; i++)
            {
                var good = new Good
                {
                    Id = i,
                    Title = $"GoodNumber{i}",
                    Price = i + 100

                };

                goodlist.Add(good);
            }

            Goods = goodlist;
        }
    }

0 个答案:

没有答案