C# 在页面内的CommandBar中对AppBarButton的命令。底部AppBar不';t火
我正在使用MVVM模式为Windows RT/8.1开发一个小应用程序 问题是,当单击Page.BottomAppBar中的CommandBar中的AppBarButton时,绑定到AppBarButton的命令(来自ViewModel)没有启动。如果我在Page.BottomAppBar中的CommandBar中将DataContext设置为ViewModel,它也不起作用 如果我在不使用Page.BottomAppBar的情况下将CommandBar代码移动到网格中,该命令工作正常,但应用程序不再检测到背景上的点击,这意味着CommandBar不会在应该关闭时关闭 我认为,这与DataContext绑定有关,但我不知道如何解决这个问题 也没有显示错误或提示。有解决办法吗?非常感谢!:) WorkListView.xaml:设置视图模型C# 在页面内的CommandBar中对AppBarButton的命令。底部AppBar不';t火,c#,mvvm,winrt-xaml,windows-8.1,commandbar,C#,Mvvm,Winrt Xaml,Windows 8.1,Commandbar,我正在使用MVVM模式为Windows RT/8.1开发一个小应用程序 问题是,当单击Page.BottomAppBar中的CommandBar中的AppBarButton时,绑定到AppBarButton的命令(来自ViewModel)没有启动。如果我在Page.BottomAppBar中的CommandBar中将DataContext设置为ViewModel,它也不起作用 如果我在不使用Page.BottomAppBar的情况下将CommandBar代码移动到网格中,该命令工作正常,但应用程
<Page.Resources>
<x:String x:Key="AppName">My Worklist</x:String>
<vm:WorkListViewModel x:Key="viewModel"/>
</Page.Resources>
我的工作清单
WorkListView.xaml:为网格设置数据上下文(仅供参考)
WorkListView.xaml:Bind命令到BottomAppBar中的AppBarButton(这不起作用;当单击时,RefreshCommand不会启动)
WorkListView.xaml:网格内命令的替代绑定(这可以工作,但应用程序不再检测后台点击)
WorkListViewModel.cs:将RefreshCommand/Delegate 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());
}
private DelegateCommand\u刷新命令;
公共DelegateCommand刷新命令
{
获取{return\u refreshCommand;}
set{this.SetProperty(ref _refreshCommand,value);}
}
公共工作列表模型()
{
this.ItemsView=this.CreateCollectionView(\u工作列表);
this.ItemsView.CurrentChanged+=this.OnCurrentItemChanged;
this.RefreshCommand=新的DelegateCommand(this.OnExecuteRefreshCommand);
}
私有异步void OnExecuteRefreshCommand(对象参数)
{
ItemsView.Clear();
SetList(wait Helpers.WebServiceConnector.refreshWorkList());
}
//编辑:
我已经在onexecuterefresh命令中设置了一个断点来检查应用程序是否中断。显然,它似乎根本不认识绑定
另一件奇怪的事情是,当我右键单击AppBarButton中命令绑定中的“RefreshCommand”并单击“转到定义”时,Visual Studio会立即将我重定向到ViewModel中的RefreshCommand方法。我解决了这个问题:
我所要做的就是:将ViewModel放入App.xaml资源,而不是页面资源:
App.xaml Application.Resources
<Application.Resources>
<ResourceDictionary>
<vm:WorkListViewModel x:Key="workVM" />
</ResourceDictionary>
</Application.Resources>
以前两个绑定之间可能存在冲突。如果您尝试了不起作用的场景,输出窗口是否会显示一些绑定错误?是的,但没有显示错误。我忘了说,我在OnExecuteRefreshCommand方法中也设置了一个断点,当我点击按钮时,应用程序从未中断。如果我右键单击AppBarButton中的RefreshCommand,然后单击“转到定义”,他实际上是跳转到ViewModel。如果我理解得很好,如果AppBar位于网格之外,则该场景不起作用。事实上,在本例中,由于DataContext设置为网格,AppBar没有任何DataContext来搜索RefreshCommand属性。试着在页面级别设置DataContext。是的,你理解得很好。谢谢你的建议,但没有真正奏效。我做了这样的事
<Grid Grid.Row="2">
<CommandBar x:Name="cmdBar" Background="#FF7300">
<AppBarButton Icon="Refresh" Label="Refresh" IsEnabled="True" Command="{Binding RefreshCommand}"/>
</CommandBar>
</Grid>
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());
}
<Application.Resources>
<ResourceDictionary>
<vm:WorkListViewModel x:Key="workVM" />
</ResourceDictionary>
</Application.Resources>
DataContext="{StaticResource workVM}"´