我一直在实验使用F#视图模块的异步命令。问题是,当我单击按钮时,命令将被执行,但之后按钮将保持禁用状态。
Xaml:
<Button Content="start async worker" Command="{Binding StartAsyncCommand}" />ViewModel:
type MainViewModel() as me =
inherit ViewModelBase()
//...
member __.StartAsyncCommand = me.Factory.CommandAsync(fun _ -> async { return () } )我做错了什么?
编辑:
由于@FoggyFinder,我们确定问题实际上与App.fs文件有关:
open System
open FsXaml
open System.Windows
type MainWindow = XAML< "MainWindow.xaml">
[<STAThread>]
[<EntryPoint>]
let main argv =
Application().Run(MainWindow().Root)创建基本为空的App.xaml并开始如下所示:
module main
open System
open FsXaml
type App = XAML<"App.xaml">
[<STAThread>]
[<EntryPoint>]
let main argv =
App().Root.Run()修好了。如果有人知道这件事的解释,请不要犹豫。
发布于 2016-02-24 19:44:18
我做错了什么?
问题是,在构造SynchronizationContext层时没有ViewModel,这意味着将事情推回UI上下文的内部代码不能正常工作。
在调用Application.Run()之前,您可以在入口点的开头添加以下内容来解决这一问题
if SynchronizationContext.Current = null then
DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher)
|> SynchronizationContext.SetSynchronizationContext这将确保创建了Dispatcher并安装了一个有效的SynchronizationContext,并且很可能会为您修复这个问题。
发布于 2016-02-22 16:32:17
我使用了异步命令,但这个问题从未出现过。我试着复制你的代码-一切正常。你确定你给出了完整的代码吗?
尝试运行代码:.xaml:
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<Button Content="start async worker" Command="{Binding StartAsyncCommand}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" />
<TextBlock Text="{Binding Count}" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Top"></TextBlock>
</Grid>ViewModel:
type MainViewModel() as me =
inherit ViewModelBase()
let count = me.Factory.Backing(<@ me.Count @>, 0)
member __.StartAsyncCommand = me.Factory.CommandAsync(fun _ -> async { count.Value <- count.Value + 1 })
member __.Count with get() = count.Value关于…之间的区别
let dosomething _ = async { return () }
member __.StartAsyncCommand = me.Factory.CommandAsync(dosomething)以及:
member __.StartAsyncCommand = me.Factory.CommandAsync(fun _ -> async { return () } )看这个答案:https://chat.stackoverflow.com/transcript/message/26511092#26511092
发布于 2016-02-23 06:32:34
简单的解决办法是使用交互触发器:
<Button>
<ia:Interaction.Triggers>
<ia:EventTrigger EventName="Click">
<fsx:EventToCommand Command="{Binding StartAsyncCommand}" />
</ia:EventTrigger>
</ia:Interaction.Triggers>
</Button>https://stackoverflow.com/questions/35554054
复制相似问题