MVVM-Pattern を使用して、Windows RT/8.1 用の小さなアプリに取り組んでいます。
問題は、Page.BottomAppBar の CommandBar の AppBarButton にバインドされたコマンド (ViewModel から来る) がクリックされたときに起動しないことです。Page.BottomAppBar の CommandBar 内で DataContext を ViewModel に設定しても機能しません。
Page.BottomAppBar なしで CommandBar-Code をグリッドに移動すると、コマンドは正常に動作しますが、アプリはバックグラウンドでのタップを検出しなくなりました。 .
DataContext-Binding と関係があると思いますが、これを修正する方法がわかりません。
エラーやヒントも表示されません。そこに解決策はありますか?どうもありがとうございます!:)
WorkListView.xaml: ViewModel の設定
<Page.Resources>
<x:String x:Key="AppName">My Worklist</x:String>
<vm:WorkListViewModel x:Key="viewModel"/>
</Page.Resources>
WorkListView.xaml: グリッドの DataContext を設定する (参考までに)
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource viewModel}">
WorkListView.xaml: コマンドを BottomAppBar の AppBarButton にバインドします (これは機能しません。クリックしても RefreshCommand は起動しません)。
<Page.BottomAppBar>
<CommandBar x:Name="cmdBar" Background="#FF7300">
<AppBarButton Icon="Refresh" Label="Refresh" IsEnabled="True" Command="{Binding RefreshCommand}"/>
</CommandBar>
</Page.BottomAppBar>
WorkListView.xaml: グリッド内のコマンドの代替バインディング (これは機能しますが、アプリはバックグラウンドでのタップを検出しなくなりました)
<Grid Grid.Row="2">
<CommandBar x:Name="cmdBar" Background="#FF7300">
<AppBarButton Icon="Refresh" Label="Refresh" IsEnabled="True" Command="{Binding RefreshCommand}"/>
</CommandBar>
</Grid>
WorkListViewModel.cs: RefreshCommand の設定 / RefreshCommand を OnExecuteRefreshCommand に委譲する
private DelegateCommand _refreshCommand;
public DelegateCommand RefreshCommand
{
get { return _refreshCommand; }
set { this.SetProperty<DelegateCommand>(ref _refreshCommand, value); }
}
public WorkListViewModel()
{
this.ItemsView = this.CreateCollectionView(_workList);
this.ItemsView.CurrentChanged += this.OnCurrentItemChanged;
this.RefreshCommand = new DelegateCommand(this.OnExecuteRefreshCommand);
}
private async void OnExecuteRefreshCommand(object parameter)
{
ItemsView.Clear();
SetList(await Helpers.WebServiceConnector.refreshWorkList());
}
//編集: アプリが壊れているかどうかを確認するために、既に OnExecuteRefreshCommand 内にブレークポイントを配置しています。どうやら、バインディングをまったく認識していないようです。
別の奇妙なことは、AppBarButton のコマンド バインディング内の "RefreshCommand" を右クリックして "定義に移動" をクリックすると、Visual Studio が ViewModel の RefreshCommand メソッドにすぐにリダイレクトすることです。