数据绑定F#视图模型 [英] Databinding a F# viewmodel

查看:97
本文介绍了数据绑定F#视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从XAML编辑器中,我可以将命名空间设置为C#项目中包含的Viewmodel

From the XAML editor, I can set the namespace to the Viewmodel contained in a C# project

namespace ViewModelDB
{
    public class DependencyViewModel : IViewModelDB
    {
        public string Message { get; set; }
    }
}

在我的xaml中

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ViewModelDB="clr-namespace:ViewModelDB;assembly=ViewModelDB"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
        >
    <UserControl.DataContext>
        <ViewModelDB:DependencyViewModel/>
    </UserControl.DataContext>
    <Grid>
        <TextBlock Text="{Binding Message}"/>
    </Grid>
</UserControl>

然后识别出绑定的消息".

The binding "Message" is then recognized.

当我指向类似选区的F#名称空间时

When I point at a F# namespace of similar constituency

namespace ModuleDBGraph

open Infrastructure
open Microsoft.Practices.Prism.Regions;
open Microsoft.Practices.Unity;

type IDependencyViewModel =
    inherit IViewModel
    abstract Message : string with get, set

type DependencyViewModel () = 
    interface IDependencyViewModel with 
        member val Message = "" with get, set

然后我放松了对绑定消息的识别

I then loose the recognition of the binding Message

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ViewModelDB="clr-namespace:ViewModelDB;assembly=ViewModelDB"
             xmlns:ViewModelDBFS="clr-namespace:ModuleDBGraph;assembly=ViewModelDBGraphFS"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
        >
    <UserControl.DataContext>
        <ViewModelDBFS:DependencyViewModel/>
    </UserControl.DataContext>
    <Grid>
        <TextBlock Text="{Binding Message}"/>
    </Grid>
</UserControl>

我做错什么了吗? 这是由于Message是接口IDependencyViewModel的实现和F#中接口的显式实现,这是一件好事,但是这里可以解决此问题吗?

Am I doing something wrong ? This is due to Message being an implementation of the interface IDependencyViewModel and explicit implementation of interfaces in F#, which is a good thing, but is there away to work around this here ?

推荐答案

我认为没有比我们在评论中讨论过的解决方案更好的解决方案了,所以我将其转化为更长的答案.

I don't think there is a better solution than the one we discussed in comments, so I'm turning that into a longer answer.

它不起作用的原因是-如您已经建议的-F#显式实现接口,因此WPF在作为接口成员时看不到Message属性.最直接的解决方法是将其定义为显式属性(接口实现可以仅引用main属性):

The reason why it does not work is - as you already suggested - that F# implements interfaces explicitly and so WPF does not see the Message property when it is an interface member. The most direct workaround is to define it as an explicit property (and the interface implementation can just refer to the main property):

type DependencyViewModel () = 
    member val Message = "" with get, set
    interface IDependencyViewModel with 
        member x.Message with get() = x.Message and set(v) = x.Message <- v

通常,我认为为C#推荐的模式在F#中并非总是能很好地工作.例如,由于F#更简洁(使事情更易于重写)并且不易出错(静态捕获更多错误),所以我认为在这种情况下您可能根本不需要接口.

In general, I think the patterns recommended for C# do not always work nicely in F#. For example, because F# is more succinct (making things easier to rewrite) and less error-prone (catching more bugs statically), I think you might not actually need an interface in this case at all.

一个更复杂的解决方法是在运行时使用反射来生成接口的隐式实现(从显式实现),然后将其设置为DataContext,但对于编辑器来说效果不佳,所以也许这不是一个好方法方向.

A more complicated workaround would be to generate an implicit implementation of the interface using reflection at runtime (from the explicit implementation) and then set it as DataContext but that would not work nicely with editors, so maybe that's not a good direction.

这篇关于数据绑定F#视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆