我在 WP8 中实现了 MVVM,但是从 ViewModel 类调用方法时遇到问题,这是我的 App.xaml.cs 类的一部分,我从 ViewModel 类调用方法:
private static PrasanjaViewModel viewModel=null;
/// <summary>
/// A static ViewModel used by the views to bind against.
/// </summary>
/// <returns>The MainViewModel object.</returns>
public static PrasanjaViewModel ViewModel
{
get
{
// Delay creation of the view model until necessary
if (viewModel == null)
{
viewModel = new PrasanjaViewModel();
viewModel.LoadData();
}
return viewModel;
}
}
这是我的 PrasanjaViewModel 类中实现的 LoadData() 方法:
public void LoadData()
{
PrasanjaViewModel prasanje1 = GetPrasanje();
IsDataLoaded = true;
}
public PrasanjaViewModel GetPrasanje()
{
var prasanje = new PrasanjaViewModel();
SQLiteConnection db = new SQLiteConnection(App.DB_PATH);
var query = db.Table<Prasanja>().Where(x => x.id == 3);
var result = query.ToList();
foreach (var item in result)
{
prasanje.id = item.id;
prasanje.Tekst = item.Tekst;
}
return prasanje;
}
因此方法 GetPrasanje() 正确返回对象prasanje。并且prasanje1正确返回。但是当 * return viewModel *被执行时,它返回 NULL。我在这里找不到问题所在。