Xamarin Forms - C# 等效于 XAML 绑定

Joh*_*ore 1 xamarin xamarin.forms

我想将以下 XAML 转换为使用 C# 中定义的自定义 ViewCell...

<ListView x:Name="___listview" HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Button Image="{Binding ImageName}" Command="{Binding ShowDetailsCommand}" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

所以转换后我有...

<ListView x:Name="___listview" HasUnevenRows="True">
    <ListView.ItemTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

和 C#...

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        ___listview.ItemsSource = Repository.GetList();
        ___listview.ItemTemplate = new DataTemplate(typeof(CustomViewCell));
    }
}

public class CustomViewCell : ViewCell
{
    bool _initialized = false;

    public CustomViewCell()
    {
        var stack = new StackLayout();

        var button = new Button();

        stack.Children.Add(button);

        View = stack;
    }
}
Run Code Online (Sandbox Code Playgroud)

在 XAML 中完成按钮ImageCommand属性上的绑定语法所需的代码是什么?

Jas*_*son 5

var button = new Button();
button.SetBinding(Button.ImageProperty, new Binding("ImageName"));
button.SetBinding(Button.CommandProperty, new Binding("ShowDetailsCommand"));
Run Code Online (Sandbox Code Playgroud)