2

Windows Phone 8 プロジェクトで GalaSoft - MVVM Light Toolkit を使用すると、奇妙な問題が発生しました。突然(いくつかのものをマージした後)、すべての EventToCommand 呼び出しが機能しなくなりました。彼らは以前働いていました。既に MvvmLight ツールキットを削除して Nuget で再度参照しようとしましたが、結果は同じままです。

一例:

MainMenuPage.xaml

<phone:PhoneApplicationPage 
       ...
       xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
       xmlns:commands="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
       ....>

 <phone:PhoneApplicationPage.DataContext >  
    <Binding Source="{StaticResource MainMenuViewModel}"/>
 </phone:PhoneApplicationPage.DataContext>


 <!-- catch back key press -->
 <i:Interaction.Triggers>
    <i:EventTrigger EventName="BackKeyPress">
        <commands:EventToCommand 
            Command="{Binding BackKeyPressCommand}"
            PassEventArgsToCommand="True"/>
    </i:EventTrigger>
 </i:Interaction.Triggers>

MainMenuViewModel.cs

 // back key press command
 public RelayCommand<object> BackKeyPressCommand { get; set; }

 public MainMenuViewModel()
 {
       BackKeyPressCommand = new RelayCommand<object>(
           BackKeyPress,
           (o) => true
           );

       ...
  }

  private void BackKeyPress(Object o)
  {
        // handle back key press
  }

これは以前は完全に機能していましたが、BackKeyPress(Object o) メソッドが呼び出されることはなくなりました。これは、すべての EventToComamnd 呼び出しで発生します。

xmlns タグを削除すると、Resharper はこれを追加することを提案します。

xmlns:command="http://www.galasoft.ch/mvvmlight"

その結果:

「EventToCommand」という名前は名前空間「 http://www.galasoft.ch/mvvmlight」に存在しません

誰かが同様の問題に遭遇したか、何がこれを引き起こしたのか考えていますか?

4

2 に答える 2

2

これらの名前空間は正しいです。

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:commands="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"

しかし、あなたの PhoneApplicationPage には間違った DataContext があります。

DataContext="{Binding Path=MainMenuViewModel, Source={StaticResource Locator}}"

MainMenuViewModel は ViewModelLocator のプロパティです。

public MainMenuViewModel MainMenuViewModel
{
    get
    {
        return ServiceLocator.Current.GetInstance<MainMenuViewModel>();
    }
}

ところで、BackKeyPress の引数は CancelEventArgs です。以下を使用する必要があります。

public RelayCommand<CancelEventArgs> BackKeyPressCommand { get; private set; }

this.BackKeyPressCommand = new RelayCommand<CancelEventArgs>(this.BackKeyPress)

private void BackKeyPress(CancelEventArgs e)
{
}
于 2013-10-30T10:04:20.187 に答える
1

Windows Phone 8.1

Windows 8.1 Behavior SDK: InvokeAction を InputConverter と共に使用してコマンドに引数を渡す方法

Microsoft は、独自の EventToCommand 機能を開発しました。Behaviors SDK にあります。stackoverflow の誰かが、Nuget 経由でこの SDK を取得するように指示しました。NuGet でパッケージが見つからない場合は、 で入手してAdd reference dialogください。

ここに画像の説明を入力Productivity Power Tools(参照の追加ダイアログは、拡張子 が原因で元のダイアログと異なる場合があります)

簡単な使用例を次に示します。

<ListBox ItemsSource="{Binding Persons, Mode=OneWay}" 
         SelectedItem="{Binding SelectedPerson, Mode=TwoWay}">
    <interactivity:Interaction.Behaviors>
        <core:EventTriggerBehavior EventName="SelectionChanged">
            <core:InvokeCommandAction Command="{Binding DisplayPersonCommand}" />
        </core:EventTriggerBehavior>
    </interactivity:Interaction.Behaviors>
</ListBox>
于 2014-12-22T10:21:15.863 に答える