【问题标题】:BindingContext in C# codeC# 代码中的 BindingContext
【发布时间】:2017-07-09 21:58:10
【问题描述】:

下面是“InqueritoViewModel”类的绑定上下文,在 XAML 中一切正常。我可以毫无问题地调用命令“GetinqueritoCommand”,但现在我尝试使用 C# 而不是 XAML 进行设计,但我无法从“InqueritoViewModel”类调用“GetinqueritoCommand”。

为什么我无法访问该命令?我正在做内容中的Bindingcontext。

<ContentPage.BindingContext>
    <viewModels:InqueritoViewModel/>
</ContentPage.BindingContext>

<StackLayout>
    <Button Command="{Binding GetinqueritoCommand}" Text="Open Inquery"></Button>

    <ListView x:Name="InqueritoView" ItemsSource="{Binding Inqueritos}" HasUnevenRows="True" ItemSelected="ListView_OnItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <StackLayout>
                            <Label x:Name="Label1" Text="{Binding Pergunta}"></Label>
                        </StackLayout>
                        <StackLayout Orientation="Horizontal">
                            <controls:Checkbox></controls:Checkbox>
                            <Label Text="{Binding Resposta}" VerticalTextAlignment="Center"></Label>
                        </StackLayout>
                        <StackLayout Orientation="Horizontal">
                            <controls:Checkbox></controls:Checkbox>
                            <Label Text="{Binding Resposta1}" VerticalTextAlignment="Center"></Label>
                        </StackLayout>
                        <StackLayout Orientation="Horizontal" >
                            <controls:Checkbox></controls:Checkbox>
                            <Label Text="{Binding Resposta2}" VerticalTextAlignment="Center"></Label>
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

</StackLayout>

InqueritoViewModel:

 public class InqueritoViewModel : INotifyPropertyChanged

{
    ApiServices _apiServices = new ApiServices();
    public List<Inquerito> _inqueritos;


    public string AccessToken { get; set; }

    public List<Inquerito> Inqueritos
    {
        get { return _inqueritos; }
        set
        {
            _inqueritos = value;
            OnPropertyChanged();
        }
    }


    public ICommand GetinqueritoCommand
    {
        get
        {
            return new Command(async () =>
            {
                AccessToken = Settings.AccessToken;
                Inqueritos = await _apiServices.GetinqueritosAsync(AccessToken);
            });
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

这是设计的 C# 代码:

public class Inqueritoscode : ContentPage
{


    public Inqueritoscode()
    {






        ListView listView = new ListView
        {

            HasUnevenRows = true,

            // Source of data items.

            ItemsSource = Inqueritos,






            ItemTemplate = new DataTemplate(() => {

                // Create views with bindings for displaying each property.
                Label perguntaLabel = new Label();
                Label respostaLabel = new Label();
                Label respostaLabel1 = new Label();
                Label respostaLabel2 = new Label();
                var command = new Command(() => Debug.WriteLine("Command executed"));
                var button = new Button
                {
                    Text = "Open Inquery",
                    Command = doesnt find the GetinqueritoCommand,
                };




                respostaLabel.VerticalTextAlignment = TextAlignment.Center;
                respostaLabel1.VerticalTextAlignment = TextAlignment.Center;
                respostaLabel2.VerticalTextAlignment = TextAlignment.Center;



                perguntaLabel.SetBinding(Label.TextProperty, "Pergunta");
                respostaLabel.SetBinding(Label.TextProperty, "Resposta");
                respostaLabel1.SetBinding(Label.TextProperty, "Resposta1");
                respostaLabel2.SetBinding(Label.TextProperty, "Resposta2");








                var cb = new Messier16.Forms.Controls.Checkbox() { IsEnabled = true };
                var cb1 = new Messier16.Forms.Controls.Checkbox() {IsEnabled = true };
                var cb2 = new Messier16.Forms.Controls.Checkbox() { IsEnabled = true };





                return new ViewCell
                {
                    View = new StackLayout
                    {

                        Children = {
                            new StackLayout {
                                Children = {
                                    perguntaLabel,
                                 },
                            },
                            new StackLayout
                            {
                                Orientation = StackOrientation.Horizontal,
                                Children =
                                {
                                    cb

                                }

                            },
                            new StackLayout
                            {
                                Orientation = StackOrientation.Horizontal,

                                Children =
                                {
                                    cb1


                                }

                            },
                           new StackLayout
                            {
                                Orientation = StackOrientation.Horizontal,

                                Children =
                                {
                                    cb2

                                }

                            }

                        }
                    }
                };
            })
        };

        // Accomodate iPhone status bar.
        this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);

        // Build the page.
        this.Content = new StackLayout
        {

            BindingContext = new ViewModels.InqueritoViewModel(),


            Children = {

                listView,

            }
        };


    }


}

【问题讨论】:

    标签: c# android xamarin binding


    【解决方案1】:
    button.SetBinding(Button.CommandProperty, "GetinqueritoCommand");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      相关资源
      最近更新 更多