Hi Brian,
Thank you for using Syncfusion products.
We have checked the reported query “ViewModel is null in ContentPage OnAttached method” from our end. We would like to let you know that you cannot obtain ViewModel using FindByName, since it only returns the element in the UI that has the specified name. You can get the ViewModel in behaviour class from BindingContext. Please refer the following code snippet for more reference,
Code Snippet C#: Obtain viewModel from ContentPage
|
public class DogBehaviorClass : Behavior<ContentPage>
{
SfListView listView;
DogViewModel viewModel;
protected override void OnAttachedTo(ContentPage bindable)
{
listView = bindable.FindByName<SfListView>("DogListView");
viewModel = bindable.BindingContext as DogViewModel;
base.OnAttachedTo(bindable);
}
} |
Code Snippet C#: App.xaml.cs
|
public partial class App : Application
{
NavigationPage navigationPage;
public App()
{
InitializeComponent();
var dp = new MainPage();
navigationPage = new NavigationPage(dp)
{
BindingContext = new DogViewModel()
};
dp.Behaviors.Add(new DogBehaviorClass());
MainPage = navigationPage;
}
} |
We have prepared and attached the tested sample and you can download the same using the following link,
Please check the sample and let us know if this is helpful.
Regards,
Chandrasekar Sampathkumar