在我的Windows Phone 8应用程序中的一个页面上,我正在尝试将特定记录加载到DataContext中。因此,它搜索图像以查找其列(ImageName)等于strVal1的记录。问题是我在存储DataContext的行中不断收到以下错误:
'System.ArgumentNullException'
strVal1不为null,所以我很确定我的DataContext是null,我不明白为什么。
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string strVal1 = this.NavigationContext.QueryString["value1"];
DataContext = App.ViewModel.Images.Where(b => b.ImageName == strVal1);
}
在另一个页面上,我做了类似的事情,它运作得很好:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (DataContext == null)
{
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.Images[index];
}
}
}
我想知道这是否是我搜索特定记录的方式。
如果你需要我的MainViewModel.cs,那就是:
public class MainViewModel : INotifyPropertyChanged
{
// This is the URI of the public, read-only Northwind data service.
// To make updates and save changes, replace this URI
// with your own Northwind service implementation.
private static readonly Uri _rootUri =
new Uri("http://localhost:49198/ImageDataServ.svc/");
// Define the typed DataServiceContext.
private ImageDBEntities _context;
// Define the binding collection for Customers.
private DataServiceCollection<Image> _images;
// Gets and sets the collection of Customer objects from the feed.
// This collection is used to bind to the UI (View).
public DataServiceCollection<Image> Images
{
get { return _images; }
private set
{
// Set the Titles collection.
_images = value;
// Register a handler for the LoadCompleted callback.
_images.LoadCompleted += OnImagesLoaded;
// Raise the PropertyChanged events.
NotifyPropertyChanged("Images");
}
}
// Used to determine whether the data is loaded.
public bool IsDataLoaded { get; private set; }
// Loads data when the application is initialized.
public void LoadData()
{
// Instantiate the context and binding collection.
_context = new ImageDBEntities(_rootUri);
Images = new DataServiceCollection<Image>(_context);
// Specify an OData query that returns all customers.
var query = from ImageData in _context.Images
select ImageData;
// Load the customer data.
Images.LoadAsync(query);
}
// Displays data from the stored data context and binding collection
public void LoadData(ImageDBEntities context,
DataServiceCollection<Image> _images)
{
_context = context;
Images = _images;
IsDataLoaded = true;
}
// Handles the DataServiceCollection<T>.LoadCompleted event.
private void OnImagesLoaded(object sender, LoadCompletedEventArgs e)
{
// Make sure that we load all pages of the Customers feed.
if (Images.Continuation != null)
{
Images.LoadNextPartialSetAsync();
}
IsDataLoaded = true;
}
// Declare a PropertyChanged for the UI to register
// to get updates from the ViewModel.
public event PropertyChangedEventHandler PropertyChanged;
// Notifies the binding about a changed property value.
private void NotifyPropertyChanged(string propertyName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
// Raise the PropertyChanged event.
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
答案 0 :(得分:0)
应始终检查null并处理错误情况;但很可能比较失败并且没有结果,请确保它不是案例问题。
更改为
if (( App.ViewModel.Images != null ) &&
( App.ViewModel.Images.Any()))
{
var images = App.ViewModel
.Images.Where(b => b.ImageName.ToLower() == strVal1.ToLower())
.ToList();
if (images.Any())
DataContext = images;
}