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

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

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个没有标题和图标的模板块:

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

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

英文:

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 =&gt; (string)GetValue(TitleProperty);
        set =&gt; SetValue(TitleProperty, value);
    }
    
    public PatientInfoCard()
    {
        InitializeComponent();
    }
}

Its xaml:

&lt;ContentView xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot; 
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             x:Class=&quot;MobileApp.Control.PatientInfoCard&quot;&gt;
  &lt;ContentView.Content&gt;
        &lt;StackLayout Background=&quot;green&quot; Padding=&quot;10&quot; &gt;
            &lt;StackLayout Margin=&quot;10,10,10,0&quot; BackgroundColor=&quot;gray&quot;&gt;
                &lt;Grid&gt;
                    &lt;Grid.ColumnDefinitions&gt;
                        &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
                        &lt;ColumnDefinition Width=&quot;*&quot; /&gt;
                    &lt;/Grid.ColumnDefinitions&gt;
                    &lt;Label Grid.Column=&quot;0&quot; Text=&quot;{Binding Title}&quot; FontAttributes=&quot;Bold&quot; VerticalOptions=&quot;Center&quot; /&gt;
                    &lt;Label Grid.Column=&quot;1&quot; Text=&quot;{Binding Title}&quot; FontAttributes=&quot;Bold&quot; VerticalOptions=&quot;Center&quot; /&gt;
                &lt;/Grid&gt;
                &lt;Label Text=&quot;{Binding Description}&quot; Margin=&quot;10,5,10,10&quot; VerticalOptions=&quot;CenterAndExpand&quot; /&gt;
            &lt;/StackLayout&gt;
        &lt;/StackLayout&gt;
  &lt;/ContentView.Content&gt;
&lt;/ContentView&gt;

Then I try to use it in a grid:

&lt;Grid Margin=&quot;10&quot; Grid.Row=&quot;6&quot; Grid.ColumnSpan=&quot;2&quot; RowSpacing=&quot;10&quot; ColumnSpacing=&quot;10&quot;
      x:DataType=&quot;Control:PatientInfoCard&quot;&gt;
    &lt;Control:PatientInfoCard Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot; Grid.ColumnSpan=&quot;3&quot; Title=&quot;Vaistai&quot;/&gt;
    &lt;Control:PatientInfoCard Grid.Row=&quot;0&quot; Grid.Column=&quot;3&quot; Grid.ColumnSpan=&quot;3&quot; Title=&quot;Tyrimai&quot; /&gt;
    &lt;Control:PatientInfoCard Grid.Row=&quot;1&quot; Grid.Column=&quot;0&quot; Grid.ColumnSpan=&quot;2&quot;  Title=&quot;Ligos&quot;/&gt;
    &lt;Control:PatientInfoCard Grid.Row=&quot;1&quot; Grid.Column=&quot;2&quot; Grid.ColumnSpan=&quot;2&quot; Title=&quot;Alergijos&quot;/&gt;
    &lt;Control:PatientInfoCard Grid.Row=&quot;1&quot; Grid.Column=&quot;4&quot; Grid.ColumnSpan=&quot;2&quot;  Title=&quot;Skiepai&quot;/&gt;
&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:

&lt;ContentView xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot; 
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             x:Class=&quot;MobileApp.Control.PatientInfoCard&quot;
             x:Name=&quot;PatientCard&quot;&gt;
  &lt;ContentView.Content&gt;
        &lt;StackLayout Background=&quot;green&quot; Padding=&quot;10&quot; &gt;
            &lt;StackLayout Margin=&quot;10,10,10,0&quot; BackgroundColor=&quot;gray&quot;&gt;
                &lt;Grid&gt;
                    &lt;Grid.ColumnDefinitions&gt;
                        &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
                        &lt;ColumnDefinition Width=&quot;*&quot; /&gt;
                    &lt;/Grid.ColumnDefinitions&gt;

                    &lt;Label Grid.Column=&quot;0&quot;
                           Text=&quot;{Binding Title, Source={x:Reference PatientCard}}&quot;
                           FontAttributes=&quot;Bold&quot;
                           VerticalOptions=&quot;Center&quot; /&gt;

                    &lt;!-- ... --&gt;

                &lt;/Grid&gt;
            &lt;/StackLayout&gt;
        &lt;/StackLayout&gt;
  &lt;/ContentView.Content&gt;
&lt;/ContentView&gt;

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;
    }
}

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:

确定