C# 选项卡页未加载子页绑定上下文
这是我的C# 选项卡页未加载子页绑定上下文,c#,xamarin.forms,C#,Xamarin.forms,这是我的选项卡页面代码: <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MojLek.Views.OrdersPage" xmlns:local="c
选项卡页面
代码:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MojLek.Views.OrdersPage"
xmlns:local="clr-namespace:MyProject.Views"
Title="Orders">
<!--Pages can be added as references or inline-->
<local:NewClaimPage IconImageSource="Add.png" />
</TabbedPage>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NewClaimPage : ContentPage
{
NewClaimPageViewModel viewModel = new NewClaimPageViewModel();
public NewClaimPage()
{
InitializeComponent();
BindingContext = viewModel;
}
protected override void OnAppearing()
{
base.OnAppearing();
viewModel.LoadPatientDoctorsCommand.Execute(null);
}
}
OnAppearing
未激发,NewClaim
ContentPage未激发,NewClaimViewModel
未激发。有什么问题吗?可能您在viewmodel中遗漏了一些内容。请检查下面的建议
解决方案1:
由于我没有您的viewmodel的详细信息,我制作了一个简单的代码供您参考。在voewmodel中设置数据:
public class NewClaimPageViewModel
{
public NewClaimPageViewModel()
{
str = "Hello";
}
public string str { get; set; }
}
在xaml:NewClaimPage中绑定
<ContentPage.Content>
<StackLayout>
<Label
HorizontalOptions="CenterAndExpand"
Text="NewClaimPage!"
VerticalOptions="CenterAndExpand" />
<Label Text="{Binding str}" />
</StackLayout>
</ContentPage.Content>
解决方案2:
或者,您可以在新的claimpage中绑定上下文
<ContentPage.BindingContext>
<vm:NewClaimPageViewModel />
</ContentPage.BindingContext>