Mvvm 视图模型中的F#异步/等待 我的ViewModel在F中#
我正在尝试使用F而不是C来实现我的ViewModel。 我遵循这一点(顺便说一句,有什么新的或更好的建议?) 假设我有我的视图模型基础实现(Mvvm 视图模型中的F#异步/等待 我的ViewModel在F中#,mvvm,f#,async-await,fsharp.viewmodule,Mvvm,F#,Async Await,Fsharp.viewmodule,我正在尝试使用F而不是C来实现我的ViewModel。 我遵循这一点(顺便说一句,有什么新的或更好的建议?) 假设我有我的视图模型基础实现(MVVM.ViewModel,它是C语言,但我可以从F中引用它)和一个简单的状态属性 namespace FuncViewModel open MVVM.ViewModel open System type MyFuncViewModel() = inherit ViewModelBase() let mutab
MVVM.ViewModel
,它是C语言,但我可以从F中引用它)和一个简单的状态
属性
namespace FuncViewModel
open MVVM.ViewModel
open System
type MyFuncViewModel() =
inherit ViewModelBase()
let mutable status=""
member this.RunSetStatus() =
status <- "Reset @" + DateTime.Now.ToString "yy.MM.dd hh:mm:ss"
base.OnPropertyChanged("Status")
member this.SetStatus = new DelegateCommand(fun _ -> this.RunSetStatus() )
member this.Status
with get() =
status
and set(value) =
status <- value
base.OnPropertyChanged(fun () -> this.Status)
我犯了一个错误
未指定成员或对象构造函数“OnPropertyChanged”
可到达的只能从内部访问私有成员
声明类型。受保护的成员只能从
无法从内部lambda表达式访问扩展类型和
编辑(续)
最后,我也试过这个
member this.RunSetStatus() =
status <- "Start resetting @" + DateTime.Now.ToString "yy.MM.dd hh:mm:ss"
base.OnPropertyChanged("Status")
let task = async {
do! Async.Sleep (30 * 1000)
}
Async.StartWithContinuations(task,
(fun _ -> this.Status <- "Reset done @" + DateTime.Now.ToString "yy.MM.dd hh:mm:ss"),
(fun _ -> this.Status <- "Operation failed."),
(fun _ -> this.Status <- "Operation canceled."))
异常的原因是F#的函数不能表示为
MemberExpression
:
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
{
if (selectorExpression == null)
throw new ArgumentNullException("selectorExpression");
MemberExpression body = selectorExpression.Body as MemberExpression;
if (body == null)
throw new ArgumentException("The body must be a member expression");
OnPropertyChanged(body.Member.Name);
}
受保护的虚拟void OnPropertyChanged(表达式selectorExpression)
{
如果(selectorExpression==null)
抛出新ArgumentNullException(“selectorExpression”);
MemberExpression body=selectoreexpression.body作为MemberExpression;
if(body==null)
抛出新ArgumentException(“主体必须是成员表达式”);
OnPropertyChanged(body.Member.Name);
}
在调试器中,您将看到得到的异常实际上是“主体必须是成员表达式”
您的第一个代码:
member this.RunSetStatus() =
status <- "Reset @" + DateTime.Now.ToString "yy.MM.dd hh:mm:ss"
base.OnPropertyChanged("Status")
成员this.RunSetStatus()=
状态为什么不直接使用FsVM
?或者甚至是Gjarlarhorn
?我问,因为我复制了你的代码,它工作得很好。@GoFunctional不管各种C#interop问题,有什么理由不使用普通的旧版本吗?@如果你有任何问题,请随时提问
member this.Status
with get() =
status
and set(value) =
status <- value
base.OnPropertyChanged("Status")
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
{
if (selectorExpression == null)
throw new ArgumentNullException("selectorExpression");
MemberExpression body = selectorExpression.Body as MemberExpression;
if (body == null)
throw new ArgumentException("The body must be a member expression");
OnPropertyChanged(body.Member.Name);
}
member this.RunSetStatus() =
status <- "Reset @" + DateTime.Now.ToString "yy.MM.dd hh:mm:ss"
base.OnPropertyChanged("Status")