数据在Xamarin Android应用中并不是绑定的。

huangapple go评论93阅读模式
英文:

Data is not binding in Xamarin android app

问题

我已创建了一个具有图标和文本的可重用患者卡片组件:

  1. [XamlCompilation(XamlCompilationOptions.Compile)]
  2. public partial class PatientInfoCard : ContentView
  3. {
  4. public static readonly BindableProperty TitleProperty = BindableProperty.Create(
  5. nameof(Title),
  6. typeof(string),
  7. typeof(PatientInfoCard),
  8. default(string)
  9. );
  10. public string Title
  11. {
  12. get => (string)GetValue(TitleProperty);
  13. set => SetValue(TitleProperty, value);
  14. }
  15. public PatientInfoCard()
  16. {
  17. InitializeComponent();
  18. }
  19. }

其XAML部分如下:

  1. <ContentView xmlns="http://xamarin.com/schemas/2014/forms"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3. x:Class="MobileApp.Control.PatientInfoCard">
  4. <ContentView.Content>
  5. <StackLayout Background="green" Padding="10" >
  6. <StackLayout Margin="10,10,10,0" BackgroundColor="gray">
  7. <Grid>
  8. <Grid.ColumnDefinitions>
  9. <ColumnDefinition Width="Auto" />
  10. <ColumnDefinition Width="*" />
  11. </Grid.ColumnDefinitions>
  12. <Label Grid.Column="0" Text="{Binding Title}" FontAttributes="Bold" VerticalOptions="Center" />
  13. <Label Grid.Column="1" Text="{Binding Title}" FontAttributes="Bold" VerticalOptions="Center" />
  14. </Grid>
  15. <Label Text="{Binding Description}" Margin="10,5,10,10" VerticalOptions="CenterAndExpand" />
  16. </StackLayout>
  17. </StackLayout>
  18. </ContentView.Content>
  19. </ContentView>

然后,我尝试在一个网格中使用它:

  1. <Grid Margin="10" Grid.Row="6" Grid.ColumnSpan="2" RowSpacing="10" ColumnSpacing="10"
  2. x:DataType="Control:PatientInfoCard">
  3. <Control:PatientInfoCard Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Title="Vaistai"/>
  4. <Control:PatientInfoCard Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="3" Title="Tyrimai" />
  5. <Control:PatientInfoCard Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Title="Ligos"/>
  6. <Control:PatientInfoCard Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Title="Alergijos"/>
  7. <Control:PatientInfoCard Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="2" Title="Skiepai"/>
  8. </Grid>

它仅显示了5个没有标题和图标的模板块:

数据在Xamarin Android应用中并不是绑定的。

所以我猜绑定值可能无法正常工作?我的代码缺少什么?

英文:

I have created a reusable component foor Patient card with icon and text:

  1. [XamlCompilation(XamlCompilationOptions.Compile)]
  2. public partial class PatientInfoCard : ContentView
  3. {
  4. public static readonly BindableProperty TitleProperty = BindableProperty.Create(
  5. nameof(Title),
  6. typeof(string),
  7. typeof(PatientInfoCard),
  8. default(string)
  9. );
  10. public string Title
  11. {
  12. get =&gt; (string)GetValue(TitleProperty);
  13. set =&gt; SetValue(TitleProperty, value);
  14. }
  15. public PatientInfoCard()
  16. {
  17. InitializeComponent();
  18. }
  19. }

Its xaml:

  1. &lt;ContentView xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
  2. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
  3. x:Class=&quot;MobileApp.Control.PatientInfoCard&quot;&gt;
  4. &lt;ContentView.Content&gt;
  5. &lt;StackLayout Background=&quot;green&quot; Padding=&quot;10&quot; &gt;
  6. &lt;StackLayout Margin=&quot;10,10,10,0&quot; BackgroundColor=&quot;gray&quot;&gt;
  7. &lt;Grid&gt;
  8. &lt;Grid.ColumnDefinitions&gt;
  9. &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
  10. &lt;ColumnDefinition Width=&quot;*&quot; /&gt;
  11. &lt;/Grid.ColumnDefinitions&gt;
  12. &lt;Label Grid.Column=&quot;0&quot; Text=&quot;{Binding Title}&quot; FontAttributes=&quot;Bold&quot; VerticalOptions=&quot;Center&quot; /&gt;
  13. &lt;Label Grid.Column=&quot;1&quot; Text=&quot;{Binding Title}&quot; FontAttributes=&quot;Bold&quot; VerticalOptions=&quot;Center&quot; /&gt;
  14. &lt;/Grid&gt;
  15. &lt;Label Text=&quot;{Binding Description}&quot; Margin=&quot;10,5,10,10&quot; VerticalOptions=&quot;CenterAndExpand&quot; /&gt;
  16. &lt;/StackLayout&gt;
  17. &lt;/StackLayout&gt;
  18. &lt;/ContentView.Content&gt;
  19. &lt;/ContentView&gt;

Then I try to use it in a grid:

  1. &lt;Grid Margin=&quot;10&quot; Grid.Row=&quot;6&quot; Grid.ColumnSpan=&quot;2&quot; RowSpacing=&quot;10&quot; ColumnSpacing=&quot;10&quot;
  2. x:DataType=&quot;Control:PatientInfoCard&quot;&gt;
  3. &lt;Control:PatientInfoCard Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot; Grid.ColumnSpan=&quot;3&quot; Title=&quot;Vaistai&quot;/&gt;
  4. &lt;Control:PatientInfoCard Grid.Row=&quot;0&quot; Grid.Column=&quot;3&quot; Grid.ColumnSpan=&quot;3&quot; Title=&quot;Tyrimai&quot; /&gt;
  5. &lt;Control:PatientInfoCard Grid.Row=&quot;1&quot; Grid.Column=&quot;0&quot; Grid.ColumnSpan=&quot;2&quot; Title=&quot;Ligos&quot;/&gt;
  6. &lt;Control:PatientInfoCard Grid.Row=&quot;1&quot; Grid.Column=&quot;2&quot; Grid.ColumnSpan=&quot;2&quot; Title=&quot;Alergijos&quot;/&gt;
  7. &lt;Control:PatientInfoCard Grid.Row=&quot;1&quot; Grid.Column=&quot;4&quot; Grid.ColumnSpan=&quot;2&quot; Title=&quot;Skiepai&quot;/&gt;
  8. &lt;/Grid&gt;

It gives me only 5 blocks template (no Title and Icon):

数据在Xamarin Android应用中并不是绑定的。

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:

  1. &lt;ContentView xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
  2. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
  3. x:Class=&quot;MobileApp.Control.PatientInfoCard&quot;
  4. x:Name=&quot;PatientCard&quot;&gt;
  5. &lt;ContentView.Content&gt;
  6. &lt;StackLayout Background=&quot;green&quot; Padding=&quot;10&quot; &gt;
  7. &lt;StackLayout Margin=&quot;10,10,10,0&quot; BackgroundColor=&quot;gray&quot;&gt;
  8. &lt;Grid&gt;
  9. &lt;Grid.ColumnDefinitions&gt;
  10. &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
  11. &lt;ColumnDefinition Width=&quot;*&quot; /&gt;
  12. &lt;/Grid.ColumnDefinitions&gt;
  13. &lt;Label Grid.Column=&quot;0&quot;
  14. Text=&quot;{Binding Title, Source={x:Reference PatientCard}}&quot;
  15. FontAttributes=&quot;Bold&quot;
  16. VerticalOptions=&quot;Center&quot; /&gt;
  17. &lt;!-- ... --&gt;
  18. &lt;/Grid&gt;
  19. &lt;/StackLayout&gt;
  20. &lt;/StackLayout&gt;
  21. &lt;/ContentView.Content&gt;
  22. &lt;/ContentView&gt;

Alternatively, you can also set the BindingContext = this; or to a ViewModel or Model class in the code-behind:

  1. [XamlCompilation(XamlCompilationOptions.Compile)]
  2. public partial class PatientInfoCard : ContentView
  3. {
  4. //...
  5. public PatientInfoCard()
  6. {
  7. InitializeComponent();
  8. BindingContext = this;
  9. }
  10. }

huangapple
  • 本文由 发表于 2023年3月8日 19:06:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定