I have myViewModelIn which I have called folder picker
public MyViewModel(IFolderPicker folderPicker)
{
_folderPicker = folderPicker;
}
I want to bind it with child view. Which I am not able to do via view because of it's parameterized Constructor. So I have bind this context in xaml.cs of child view
public ChildView(MyViewModel vm )
{
InitializeComponent();
BindingContext= vm;
}
This is how I am adding child view in parent view
<view:ChildView Grid.Column="0" Grid.RowSpan="4"></view:ChildView>
Now I have one parent view In which I have to set this child view . While adding this child view in parent view it is giving me an error
Severity Code Description Project File Line Suppression State
Error XFC0004 Missing default constructor for "DemoApp.MVVM.View.ChildView". DemoApp D:\Priti\Samples\DemoApp\DemoApp\MVVM\View\ParentView.xaml 45
How I can achieve this ?
CodePudding user response:
When you instantiate objects in xaml like this
<view:ChildView Grid.Column="0" Grid.RowSpan="4"></view:ChildView>
then your class ChildView
needs the default constructor (the one without any arguments)! Otherwise the .NET framework does not know how to instantiate your object.
A possible solution for your problem could be the approach to set your view model (which is hopefully set as DataContext
of your parent control) also as DataContext
of your child control. Nevertheless, you have to remove the ChildView constructor with the argument (and thus the default constructor will be used again)
<view:ChildView DataContext="{Binding DataContext}" Grid.Column="0" Grid.RowSpan="4"></view:ChildView>
Hope, my explanation was understandable and will work :)