c# - MVVM (Cross) 的逻辑问题。在我倒计时后拍照

标签 c# mvvm xamarin

我的 MVVM 逻辑有点问题(我使用 MVVM Cross for Xamarin)

这是我的架构: MVVM Archi

我不知道如何在我的 View (MyViewCamera) 中调用我的 Camera.TakePicture ->

事实上,当我在我的 View 中单击我的按钮时,它被绑定(bind)到我在 MyViewModel 中的属性 StartTimer 上,这个 ViewModel 调用一个函数 LaunchTimer,它调用我的服务 TimerCustom()。 当我的计时器(倒计时)为 0 时,我想在我的 ViewCamera 中调用我的功能来拍照。

我的问题纯粹是合乎逻辑的。这是 MVVM...

谢谢你的帮助

编辑我的代码:

 public class TimerCustom
{
    private bool _started; 

    public int Time { get; private set; }
    public event EventHandler<int> TimeElapsed;

    public TimerCustom(int startTime)
    {
        Time = startTime + 1; 
    }

    public async Task StartAsync(CancellationToken token = default(CancellationToken))
    {
        if (_started) return;

        _started = true;

        while (_started && Time > 0)
        {
            // wait 1000 ms
            await Task.Delay(1000, token).ConfigureAwait(false);
            if (--Time == 0)
            {
                //Timer finished
                _started = false; 
            }
            TimeElapsed?.Invoke(this, Time);
        }
    }
}

我的 View 模型:

public class SecondStep_Photo_ViewModel : MvxViewModel
{
    public event TakePictureEvent TakePicture;
    public delegate void TakePictureEvent();

    #region Prop => Display Timer
    private IMvxCommand _displayTimer;

    public IMvxCommand DisplayTimer
    {
        get
        {
            _displayTimer = _displayTimer ?? new MvxCommand(StartTimer);
            return _displayTimer;
        }
    }

    private void StartTimer()
    {
        var timer = new TimerCustom(5);
        timer.TimeElapsed += (s, t) => ValueOfTimer = t;    
        timer.StartAsync(); 
    }
    #endregion
....

在我看来我有:

protected override void OnCreate(Bundle bundle)
{
...

     (base.ViewModel as SecondStep_Photo_ViewModel).TakePicture += SecondStep_Photo_View_TakePicture;

 }

 private void SecondStep_Photo_View_TakePicture()
 {

 }

最佳答案

您可以使用 Delegate Event 来注册 TakePicture 方法并在您的 ViewModel 中使用它。

    public event TakePictureEvent TakePicture;
    public delegate void TakePictureEvent ();

在您的 View 中注册事件

ViewModel.TakePicture += MyTakePictureMethod;

在此之后,在 ICommand 中设置定时器的逻辑

private MvxCommand _startTakePictureSequence;
public System.Windows.Input.ICommand StartTakePictureSequence
{
    get
    {
        _startTakePictureSequence = _startTakePictureSequence ?? new MvxCommand(doStartTakePictureSequence);
        return _startTakePictureSequence;
    }
}

private void doStartTakePictureSequence () {

    // You're TimerService call for example
}

现在将 ICommand 绑定(bind)到一个按钮。

 <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/TakePictureButton"
        local:MvxBind="ItemClick StartTakePictureSequence" />

选择按钮后,seqeunce (doStartTakePictureSequence) 开始。计时器服务进行倒计时,当它达到 0 时,您调用委托(delegate) TakePictureEvent() 从指定设备拍摄照片。

这是实现您正在尝试的目标的一种方法,我不确定这是否是这种方法,但我在我的一些应用程序中使用它来实现多平台解决方案。 iOS、Android 和 Windows 手机使用相同的 ViewModel。

编辑

Action 添加到调用 TakePicture 委托(delegate)timer.StartAsync

public async Task StartAsync(Action completeAction, CancellationToken token = default(CancellationToken))
{
    ...
    completeAction.Invoke();
}

还有你的 StartTimer:

private void StartTimer()
{
    var timer = new TimerCustom(5);
    timer.TimeElapsed += (s, t) => ValueOfTimer = t;    
    timer.StartAsync(() => {
        TakePicture();
    });
}

关于c# - MVVM (Cross) 的逻辑问题。在我倒计时后拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36494471/

相关文章:

c# - 如何在 xamarin android 项目上集成 webrtc android native 库?

c# - 使用 Xamarin 构建适用于 Windows CE 5.2 的应用程序?

c# - 如何将进程输出重定向到 System.String

c# - 如何将对象转换为泛型?

wpf - VirtualizingStackPanel + MVVM + 多选

wpf - 绑定(bind)到 CompositeCollection 中的单个元素

c# - 如何在 subview 的ViewModel中引发ViewModel中的事件

android - Xamarin 工具->Android 菜单在 Visual Studio 2015 中被禁用

c# - 颠覆说我的文件 "remains in conflict"

c# - 为什么 ReSharper 提示这种 ResourceLoader.GetString 的使用?