字典 Xamarin 表单中不存在给定键 [英] Given Key Not Present in Dictionary Xamarin Forms
问题描述
我正在使用 Xaml 为 UI 处理 Xamarin 表单项目的数据绑定.到目前为止,它非常简单:
Im working on data binding for a Xamarin forms project using Xaml for the UI. So far its very simple:
XAML:
<?xml version="1.0" encoding="utf-8" ?>
xmlns:viewModels="clr-namespace:Watson.ViewModels;assembly=Watson"
x:Class="Watson.Views.DeviceCheck">
<ContentPage.BindingContext>
<viewModels:DeviceCheckViewModel/>
</ContentPage.BindingContext>
<!--<ActivityIndicator Color="Red" IsRunning="True"
x:Name="loadingScreen"/>-->
<StackLayout>
<Label Text="Checking Device.."/>
<Button Text="page nav"
Command="{Binding NextButton}"></Button>
</StackLayout>
背后的代码:
namespace Watson.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DeviceCheck:ContentPage
{
private DeviceCheckViewModel viewModel;
public DeviceCheck() {
InitializeComponent();
BindingContext = viewModel = new DeviceCheckViewModel(this.Navigation);
}// end of constructor
}// end of class
}// end of namespace
这是尝试绑定到视图模型并使用绑定命令在单击按钮时转到另一个页面.我得到的错误是字典中不存在给定的键",这是在尝试构建时.我已将问题隔离到以下行:<viewModels:DeviceCheckViewModel/>
This is trying to bind to a view model and use a binding command to go to another page on a button click.
The error I am getting is 'The given key was not present in the dictionary', this is when attempting a build. I have isolated the problem to the line: <viewModels:DeviceCheckViewModel/>
我不知道为什么会出现这个错误.
I have no idea why this error is occurring.
这是视图模型:
namespace Watson.ViewModels
{
public class DeviceCheckViewModel: INotifyPropertyChanged
{
public INavigation Navigation { get; set; }
public ICommand NextButton { protected set; get; }
public DeviceCheckViewModel(INavigation navigation)
{
this.Navigation = navigation;
this.NextButton = new Command(async () => await GotoNextPage());
}
public async Task GotoNextPage()
{
await Navigation.PushAsync(new RegisterDevice());
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
我走这条路的原因是坚持MVVM架构.所以将页面导航放在后面的代码中并不是一个真正的选择.还要注意的是,所有模型、视图模型和视图都在同名的文件夹结构中.
The reason I am pursuing this route is to adhere to the MVVM architecture. So putting page navigation in the code behind is not really an option. Also just a note, all the models, view models and views are in a folder structure with the same names.
任何帮助将不胜感激.
推荐答案
您在后面的代码中设置了绑定上下文:
You set the binding context in your code behind:
BindingContext = viewModel = new DeviceCheckViewModel(this.Navigation);
所以也没有必要采用 xaml 方式.这还会创建一个 DeviceCheckViewModel
实例并将其设置为绑定上下文:
So there's no need to do the xaml-way, too. This also creates an instance of DeviceCheckViewModel
and sets it as binding context:
<ContentPage.BindingContext>
<viewModels:DeviceCheckViewModel/>
</ContentPage.BindingContext>
但是,因此它需要有一个无参数的构造函数.这可能就是您获得异常的原因.
But, therefore it needs to have a parameterless constructor. This may be the reason for the excepetion you get.
这篇关于字典 Xamarin 表单中不存在给定键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!