0

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 メソッドにすぐにリダイレクトすることです。

4

1 に答える 1

0

私は問題を解決しました:

私がしなければならなかったのは、ViewModel をページ リソースの代わりに App.xaml リソースに配置することだけでした。

App.xaml アプリケーション.リソース

<Application.Resources>
    <ResourceDictionary>
        <vm:WorkListViewModel x:Key="workVM" />
    </ResourceDictionary>
</Application.Resources>

それを WorkListView ページに直接バインドし、グリッド内のバインディングを消去しました。

WorkListView.xaml

DataContext="{StaticResource workVM}"´

以前に 2 つのバインディング間で競合があった可能性があります。

于 2013-11-13T10:14:12.990 に答える