自两个星期以来,我一直在使用Xamarin和C#,但是我真的在努力使像MVVM-Pattern这样的企业工作。 两天以来我一直处于困境,所以我想问专家,社区。 p>
我的问题是EntryCell没有属性“ IsPassword”,因此我创建了CostumCell,它是一个带有网格,Label和Entryinside的ViewCell。该条目具有“ IsPassword”属性,我在这里找到了
现在开始讨论实际问题。 实际上,EditedEntryCell无法显示绑定文本。普通的EntryCell能够显示所有信息: SettingsPage.xaml
<TableSection>
<EntryCell
Label="normal entry"
Text="{Binding Username}"/>
<EntryCell
Label="normal entry"
Text="{Binding Password}"/>
<local:EditedEntryCell
LabelText="edited entry"
EntryText="{Binding Password}"
EntryIsPassword="True"/>
</TableSection>
在构建时,它会出现在输出中:
[0:]绑定:在'ProblemDemonstration.ViewModel.SettingsPageViewModel'上找不到'LabelText'属性,目标属性:'Xamarin.Forms.Label.Text'
[0:]绑定:在'ProblemDemonstration.ViewModel.SettingsPageViewModel'上找不到'EntryText'属性,目标属性:'Xamarin.Forms.Entry.Text'
[0:]绑定:在'ProblemDemonstration.ViewModel.SettingsPageViewModel'上找不到'EntryIsPassword'属性,目标属性:'Xamarin.Forms.Entry.IsPassword'
我不明白为什么绑定试图在ViewModel而不是EditedEntryCell类上找到属性,因为它们可以通过
使用 xaml xmlns:local="clr-namespace:ProblemDemonstration.Extensions"
隐藏代码如下: SettingsPage.xaml.cs
public partial class SettingsPage : ContentPage
{
public static readonly BindableProperty UsernameProperty =
BindableProperty.Create(
propertyName: "Username",
returnType: typeof(string),
declaringType: typeof(SettingsPageViewModel));
public static readonly BindableProperty PasswordProperty =
BindableProperty.Create(
propertyName: "Password",
returnType: typeof(string),
declaringType: typeof(SettingsPageViewModel));
public string Username
{
get { return (string)GetValue(UsernameProperty); }
set { SetValue(UsernameProperty, value); }
}
public string Password
{
get { return (string)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}
public SettingsPage()
{
InitializeComponent();
BindingContext = new SettingsPageViewModel(((App)Application.Current).Settings);
}
}
这显示CustomCell定义:
EditedEntryCell.xaml
<ViewCell ...>
<Grid>
....
<Label
Text="{Binding LabelText}"
Grid.Row="0"
Grid.Column="0"
IsVisible="{Binding BothIsVisible}"/>
<Entry
Text="{Binding EntryText}"
Grid.Row="0"
Grid.Column="1"
IsPassword="{Binding EntryIsPassword}"
IsEnabled="{Binding EntryIsEnabled}"
IsVisible="{Binding BothIsVisible}"/>
</Grid>
</ViewCell>
EditedEntryCell.xaml.cs
public partial class EditedEntryCell : ViewCell
{
public static readonly BindableProperty LabelTextProperty =
BindableProperty.Create(
propertyName: "LabelText",
returnType: typeof(string),
declaringType: typeof(EditedEntryCell),
defaultValue: "default");
public static readonly BindableProperty EntryTextProperty =
BindableProperty.Create(
propertyName: "EntryText",
returnType: typeof(string),
declaringType: typeof(EditedEntryCell),
defaultValue: "default");
.....
public string LabelText
{
get { return (string)GetValue(LabelTextProperty); }
set { SetValue(LabelTextProperty, value); }
}
public string EntryText
{
get { return (string)GetValue(EntryTextProperty); }
set { SetValue(EntryTextProperty, value); }
}
....
}
ViewModel中的代码: SettingsPageViewModel.xaml.cs
public class SettingsPageViewModel
{
private Settings _settings;
public string Username
{
get { return _settings.Username; }
set { _settings.Username = value; }
}
public string Password
{
get { return _settings.Password; }
set { _settings.Password = value; }
}
public SettingsPageViewModel(Settings settings)
{
this._settings = settings;
}
}
答案 0 :(得分:0)
原因::您似乎忘记了设置BindingContext
中的CustomEntryCell
,因此绑定永远无法进行。
解决方案:添加以下代码
public EditedEntryCell()
{
InitializeComponent();
BindingContext = this;
}