문제

모두

문제가 있습니다. 이제 MVVM 프레임 워크를 사용하여 Windows Phone 8 앱을 개발했습니다. 버튼을 누른 다음 녹음을 시작하면 버튼을 풀고 녹음을 중지하고 veroModelAction을 사용하여 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 이벤트를 발사하지 않았기 때문에 두 이벤트 핸들러를 다음과 같이 등록했습니다.

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)
{
}

좋아, 계속 가고 있지만 다음 문제가 다가오고있다. 그것은 ViewModel에서 Icommand를 발사하지 않았다. 그것은 button_mouseleftbuttondown, 오, 하나님, 나는 미친 짓이라고 불렀다.

누구든지 ViewModel에서 Icommand를 호출하는 방법을 알고 있습니까? 아니면 그것을 구현하는 또 다른 방법?

도움이 되었습니까?

해결책

당신이 사용할 수있는 ICommand.Execute. 따라서 핸들러가 있어야합니다

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

다른 팁

뷰 모델에서 IsRecording이라는 BOOL 속성에 대한 2 차 바인딩으로 버튼의 ispressed 속성을 바인딩하고 새 bool 값을 기반으로 세터 내부에서 녹음 로직을 시작/중지하십시오 (True는 시작을 의미합니다). 작동하는지 알려주세요.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top