質問

全て

問題があります。現在、MVVMフレームワークを使用してWindows Phone 8アプリを開発しています。ボタンを押してから何かを記録し始めたいだけで、ボタンをリリースするとき、録音を停止し、InvokeCommandactionを使用してViewModelでコマンドをバインドしました。これは次のようなコードです

xaml:

<Button x:Name="BtnRecord" Height="50" Width="180" Background="#D43637" Content="Record" Margin="20,0,0,0" Style="{StaticResource BasicButtonStyle}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonDown">
            <i:InvokeCommandAction Command="{Binding StartRecordCommand}"/>
         </i:EventTrigger>
        <i:EventTrigger EventName="MouseLeftButtonUp">
            <i:InvokeCommandAction Command="{Binding EndRecordCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

ModelView:

public ICommand StartRecordCommand
{
    get
    {
        return new DelegateCommand(StartRecord);
    }
}

public ICommand EndRecordCommand
{
    get
    {
        return new DelegateCommand(EndRecord);
    }
}

private void StartRecord(object parameter){}

private void EndRecord(object parameter){}

アプリをデバッグしたとき、MouseleftbuttondownもMouseleftbuttonupイベントも起動しないことがわかったので、次のように2つのイベントハンドラーを登録します。

BtnRecord.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Button_MouseLeftButtonDown), true);
BtnRecord.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Button_MouseLeftButtonUp), true);

private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
}
private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
}

OK、続けてください、しかし、次の問題が来ています、それはviewmodelでicommandを発射しませんでした、それはbutton_mouseleftbuttondownと呼ばれます、ああ、神、私は狂っています

ビューモデルでicommandを呼び出す方法を知っている人はいますか?またはそれを実装する別の方法は?

役に立ちましたか?

解決

使用できます ICommand.Execute. 。だから、あなたのハンドラーはそうあるべきです

private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    StartRecordCommand.Execute(null);
}

他のヒント

ビューモデルでiSRecordingと呼ばれるブールプロパティへのトワウイバインディングでボタンの入力プロパティをバインディングしてみて、新しいブール値に基づいてセッター内から録音ロジックを開始/停止します(trueはstartを意味します)。それが機能するかどうか教えてください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top