0

xaml を使用して xamarin.forms にデータをバインドしようとしています。しかし、問題はそれが機能しないことです。私のxamlコード:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:BindingSample"
         x:Class="BindingSample.Test">
  <ContentPage.Resources>
    <ResourceDictionary>
      <OnPlatform x:Key="statusToImgConverter" 
            x:TypeArguments="local:StatusToImgSourceConverter"
            iOS="null"
            Android="local:StatusToImgSourceConverterDroid"
            WinPhone="local:StatusToImgSourceConverterUWP"/>
    </ResourceDictionary>
  </ContentPage.Resources>

  <ContentPage.BindingContext>
    <local:TestViewModel/>
  </ContentPage.BindingContext>

  <StackLayout>
    <Grid RowSpacing="1" ColumnSpacing="1">
      <Image Source="{Binding Enabled, Converter={StaticResource statusToImgConverter}}" />
      <Label Text="{Binding Enabled}" Grid.Column="1" Grid.ColumnSpan="4" />
    </Grid>
    <Button x:Name="enableBtn" Text="enable"/>
  </StackLayout>
</ContentPage>

ビューモデルクラスはこんな感じ(INotifyPropertyChangedのコード生成にFodyを使いたい)

[ImplementPropertyChanged]
class TestViewModel
{
    public TestViewModel()
    {
        Enabled = true;
    }

    bool Enabled { get; set; }
}

しかし問題は、画像とラベルが表示されないことです。ResourceDictionary と Image をコメントアウトしても効果はありません (したがって、問題はコンバーターにはありません)。

私は何を間違っていますか?

編集 - コンテンツ ページのコードを追加しました。ボタンは表示されますが、画像とラベルは表示されません。

public partial class Test : ContentPage
{
    public Protection()
    {
        InitializeComponent();

        enableBtn.Clicked += OnEnableClicked;
    }

    private void OnEnableClicked(object sender, EventArgs e)
    {
        //do nothing here at this time
    }

}
4

1 に答える 1

0

最後に、エラーの原因を突き止めました。正しいコードは次のとおりです。

<ResourceDictionary>
    <OnPlatform x:TypeArguments="local:StatusToImgSourceConverter" x:Key="statusToImgConverter">
      <OnPlatform.Android>
        <local:StatusToImgSourceConverterDroid/>
      </OnPlatform.Android>
      <OnPlatform.WinPhone>
        <local:StatusToImgSourceConverterUWP/>
      </OnPlatform.WinPhone>
    </OnPlatform>          
</ResourceDictionary>

Xaml で。また、Enabled プロパティは明示的に public として宣言する必要があります。

于 2016-08-25T09:44:43.457 に答える