如何在xamarin中将条目(从登录)绑定到masterdetailpage
这是我的MasterDetailPage(MainPage.xaml.cs)背后的代码:如何在xamarin中将条目(从登录)绑定到masterdetailpage,xamarin,Xamarin,这是我的MasterDetailPage(MainPage.xaml.cs)背后的代码: 公共部分类主页面:MasterDetailPage { 公共列表菜单列表{get;set;} 公共字符串Get_项; 公共主页(字符串SelectedItem) { 初始化组件(); Get_item=SelectedItem; menuList=新列表(); SetValue(NavigationPage.HasNavigationBarProperty,false); //将菜单项添加到
公共部分类主页面:MasterDetailPage
{
公共列表菜单列表{get;set;}
公共字符串Get_项;
公共主页(字符串SelectedItem)
{
初始化组件();
Get_item=SelectedItem;
menuList=新列表();
SetValue(NavigationPage.HasNavigationBarProperty,false);
//将菜单项添加到菜单列表,您可以定义标题、页面和图标
添加(新的MasterPageItem(){Title=“Home”,Icon=“Home\u red.png”,TargetType=typeof(Main)});
添加(新的MasterPageItem(){Title=“Settings”,Icon=“setting_violet.png”,TargetType=typeof(Settings)});
添加(新的MasterPageItem(){Title=“Records”,Icon=“blue_database.png”,TargetType=typeof(HomePageX)});
添加(新的MasterPageItem(){Title=“About”,Icon=“black_About.png”,TargetType=typeof(test)});
添加(新的MasterPageItem(){Title=“LogOut”,Icon=“yellow\u LogOut.png”,TargetType=typeof(Login)});
//将列表设置为MainPage.xaml中ListView的ItemSource
navigationDrawerList.ItemsSource=menuList;
//初始导航,这可以用于我们的主页
Detail=newNavigationPage((Page)Activator.CreateInstance(typeof(Main));
}
}
我在MainPage.xaml中有这个网格:
<Grid>
<Image Source="peach_gradient.png" Aspect="AspectFill" />
<StackLayout>
<forms:AnimationView x:Name="animationView"
Animation="diamond.json"
Loop="True"
AutoPlay="True"
HeightRequest="150"
WidthRequest="150"/>
<Label Text="{Binding Get_item}"
TextColor="White"
FontSize="Large"
HorizontalTextAlignment="Center"
VerticalOptions="Center"
HorizontalOptions="Center"/>
</StackLayout>
</Grid>
这是我的Login.xaml代码:
<ScrollView>
<StackLayout>
<Grid VerticalOptions="CenterAndExpand" Margin="20,80,20,0" RowSpacing="20">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<local:RoundedEntry x:Name="EntryLoginUsername"
Placeholder="Username"
Grid.Row="0"
TextColor="WhiteSmoke"
PlaceholderColor="WhiteSmoke"/>
<local:RoundedEntry x:Name="EntryLoginPassword"
Placeholder="Password"
Grid.Row="1"
IsPassword="True"
TextColor="WhiteSmoke"
PlaceholderColor="WhiteSmoke"/>
<Button Text="Login"
Grid.Row="2"
Clicked="Button_Clicked_1"/>
</Grid>
</StackLayout>
</ScrollView>
这是我的登录页面(login.xaml.cs)后面的代码:
公共无效按钮\u单击\u 1(对象发送者,事件参数)//登录!
{
var dbpath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),“Userdatabase.db”);
var db=新的SQLiteConnection(dbpath);
var loginquery=db.Table()。其中(u=>u.Username.Equals(EntryLoginUsername.Text)和&u.Password.Equals(EntryLoginPassword.Text)).FirstOrDefault();
App.Current.MainPage=新导航页面(新主页(EntryLoginUsername.Text));
}
如果我运行此代码,则主页中的标签文本=“{Binding Get_item}”为空,我想这样实现它:
条目中的文本(来自登录页面)应放在此处:
您只能绑定到公共属性<代码>获取项目不是公共属性
public string Get_item;
换成这个
public string Get_item { get; set; }
您还需要为页面设置BindingContext
this.BindingContext = this;
public string Get_item { get; set; }
this.BindingContext = this;