I am working on a little app for Windows RT / 8.1 using the MVVM-Pattern.
The problem is, that the binded command (coming from the ViewModel) to a AppBarButton in a CommandBar in Page.BottomAppBar is not firing, when clicked on. It doesn't work too, if I set the DataContext to the ViewModel within the CommandBar in Page.BottomAppBar.
If I am moving the CommandBar-Code without Page.BottomAppBar into the grid, the command works fine, but the app doesn't detect taps on the Background anymore, which means, that the CommandBar doesn't Close, when it is supposed to.
I think, that it has something to do with the DataContext-Binding, but I don't have a clue, how to fix this.
There are also no errors or hints showing up. Is there a solution out there? Thanks you very much! :)
WorkListView.xaml: Set ViewModel
<Page.Resources>
<x:String x:Key="AppName">My Worklist</x:String>
<vm:WorkListViewModel x:Key="viewModel"/>
</Page.Resources>
WorkListView.xaml: Set DataContext for Grid (Just FYI)
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource viewModel}">
WorkListView.xaml: Bind Command to AppBarButton in BottomAppBar (This doesn't work; RefreshCommand doesn't fire up, when clicked on)
<Page.BottomAppBar>
<CommandBar x:Name="cmdBar" Background="#FF7300">
<AppBarButton Icon="Refresh" Label="Refresh" IsEnabled="True" Command="{Binding RefreshCommand}"/>
</CommandBar>
</Page.BottomAppBar>
WorkListView.xaml: Alternative binding of the Command within the Grid (This works, but the app doesn't detect taps on the Background anymore)
<Grid Grid.Row="2">
<CommandBar x:Name="cmdBar" Background="#FF7300">
<AppBarButton Icon="Refresh" Label="Refresh" IsEnabled="True" Command="{Binding RefreshCommand}"/>
</CommandBar>
</Grid>
WorkListViewModel.cs: Setting RefreshCommand / Delegate RefreshCommand to 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());
}
//EDIT: I already put a breaking Point within the OnExecuteRefreshCommand to check, if the app breaks. Apparently it doesn't, it seems, that it doesn't recognize the binding at all.
Another weird thing is, when I right click on "RefreshCommand" within the Command Binding in the AppBarButton and click on "Go To Definition", Visual Studio Redirects me immediately to the RefreshCommand Method in the ViewModel.