我的 MVVM 逻辑有点问题(我使用 MVVM Cross for Xamarin)
我不知道如何在我的 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/