英文:
Data is not binding in Xamarin android app
问题
我已创建了一个具有图标和文本的可重用患者卡片组件:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PatientInfoCard : ContentView
{
public static readonly BindableProperty TitleProperty = BindableProperty.Create(
nameof(Title),
typeof(string),
typeof(PatientInfoCard),
default(string)
);
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public PatientInfoCard()
{
InitializeComponent();
}
}
其XAML部分如下:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MobileApp.Control.PatientInfoCard">
<ContentView.Content>
<StackLayout Background="green" Padding="10" >
<StackLayout Margin="10,10,10,0" BackgroundColor="gray">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding Title}" FontAttributes="Bold" VerticalOptions="Center" />
<Label Grid.Column="1" Text="{Binding Title}" FontAttributes="Bold" VerticalOptions="Center" />
</Grid>
<Label Text="{Binding Description}" Margin="10,5,10,10" VerticalOptions="CenterAndExpand" />
</StackLayout>
</StackLayout>
</ContentView.Content>
</ContentView>
然后,我尝试在一个网格中使用它:
<Grid Margin="10" Grid.Row="6" Grid.ColumnSpan="2" RowSpacing="10" ColumnSpacing="10"
x:DataType="Control:PatientInfoCard">
<Control:PatientInfoCard Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Title="Vaistai"/>
<Control:PatientInfoCard Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="3" Title="Tyrimai" />
<Control:PatientInfoCard Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Title="Ligos"/>
<Control:PatientInfoCard Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Title="Alergijos"/>
<Control:PatientInfoCard Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" Title="Skiepai"/>
</Grid>
它仅显示了5个没有标题和图标的模板块:
所以我猜绑定值可能无法正常工作?我的代码缺少什么?
英文:
I have created a reusable component foor Patient card with icon and text:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PatientInfoCard : ContentView
{
public static readonly BindableProperty TitleProperty = BindableProperty.Create(
nameof(Title),
typeof(string),
typeof(PatientInfoCard),
default(string)
);
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public PatientInfoCard()
{
InitializeComponent();
}
}
Its xaml:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MobileApp.Control.PatientInfoCard">
<ContentView.Content>
<StackLayout Background="green" Padding="10" >
<StackLayout Margin="10,10,10,0" BackgroundColor="gray">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding Title}" FontAttributes="Bold" VerticalOptions="Center" />
<Label Grid.Column="1" Text="{Binding Title}" FontAttributes="Bold" VerticalOptions="Center" />
</Grid>
<Label Text="{Binding Description}" Margin="10,5,10,10" VerticalOptions="CenterAndExpand" />
</StackLayout>
</StackLayout>
</ContentView.Content>
</ContentView>
Then I try to use it in a grid:
<Grid Margin="10" Grid.Row="6" Grid.ColumnSpan="2" RowSpacing="10" ColumnSpacing="10"
x:DataType="Control:PatientInfoCard">
<Control:PatientInfoCard Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Title="Vaistai"/>
<Control:PatientInfoCard Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="3" Title="Tyrimai" />
<Control:PatientInfoCard Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Title="Ligos"/>
<Control:PatientInfoCard Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Title="Alergijos"/>
<Control:PatientInfoCard Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" Title="Skiepai"/>
</Grid>
It gives me only 5 blocks template (no Title and Icon):
So I guess that binding values are not working properly? What do my code lack of?
答案1
得分: 1
代码部分不需要翻译,以下是您要翻译的内容:
One way to bind from a View's XAML to its own code-behind is to define an x:Name
for the XAML-part, e.g. PatientCard
and then use x:Reference PatientCard
as a Source
in the Binding:
Alternatively, you can also set the BindingContext = this;
or to a ViewModel or Model class in the code-behind:
这是您需要的翻译,请忽略代码部分。
英文:
One way to bind from a View's XAML to its own code-behind is to define an x:Name
for the XAML-part, e.g. PatientCard
and then use x:Reference PatientCard
as a Source
in the Binding:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MobileApp.Control.PatientInfoCard"
x:Name="PatientCard">
<ContentView.Content>
<StackLayout Background="green" Padding="10" >
<StackLayout Margin="10,10,10,0" BackgroundColor="gray">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0"
Text="{Binding Title, Source={x:Reference PatientCard}}"
FontAttributes="Bold"
VerticalOptions="Center" />
<!-- ... -->
</Grid>
</StackLayout>
</StackLayout>
</ContentView.Content>
</ContentView>
Alternatively, you can also set the BindingContext = this;
or to a ViewModel or Model class in the code-behind:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PatientInfoCard : ContentView
{
//...
public PatientInfoCard()
{
InitializeComponent();
BindingContext = this;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论