0
以下は、XAMLの "InqueritoViewModel"クラスへのバインディングコンテキストです。私は "GetinqueritoCommand"というコマンドを問題なく呼び出すことができますが、今はXAMLの代わりにC#を使用しようとしていますが、 "InqueritoViewModel"クラスから "GetinqueritoCommand"を呼び出すことはできません。C#コードのBindingContext
なぜコマンドにアクセスできないのですか?私はコンテンツで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,
}
};
}
}