未定义的枚举和WPF 组合框 & WCF序列化

发布于 2024-09-24 00:14:19 字数 924 浏览 0 评论 0原文

我在寻找处理 WPF 和 WCF 上的枚举字段的通用解决方案时遇到问题,我需要一些帮助。让我们用一个例子来解释:

当创建一个具有 Sex 枚举值 [Male, Female] 的人时,我看到三种可能性:

  1. Male is default -> >有两种可能性,但一种是默认的。只要没有女性感到被冒犯,绑定 ComboBox.SelectedValue 就没有问题,WCF 序列化也没有问题:)

  2. ((Sex)(3)) 默认值为未定义值 ->有两种可能性,但一开始用户被迫选择一种。 ComboBox.SelectedValue 正确设置 SelectedIndex = -1,但 WCF 抛出: 枚举值“3”对于类型“Sex”无效,并且无法序列化。如果类型具有 DataContractAttribute 属性,请确保存在必要的枚举值并用 EnumMemberAttribute 属性进行标记。

  3. Sex 属性可为空 ->设置性别是可选的,一旦你选择了一个,你甚至可以取消设置。

不幸的是,情况 2 是最常见的一种,但我们没有看到一种简单的方法来处理它,而无需进行如下黑客攻击:

  • 为性别定义“未知”值,该值对实体无效,我会考虑是否会这样做这不是最常见的情况。
  • 将枚举标记为 [Flags],以便 WCF 可以将“Male,Female”编码为未知。
  • 使用 [ComboHintUnkwnownOnNew] 之类的内容装饰 sex 属性。
  • 设置 SelectedIndex=-1,同时在 WPF 中以某种方式保持绑定。

我知道这是一个思想设计决策,而不是一个具体问题,但我希望得到一些帮助。

I'm having problems finding a general solution to deal with Enum fields on WPF and WCF, I need a little bit of help. Let's explain with an example:

When creating a person with a Sex enum value [Male, Female] I see three posibilites:

  1. Male is default -> There are two posibilities but one is default. No problems binding the ComboBox.SelectedValue and no problem with WCF serialization, as long as no female feels ofended :)

  2. ((Sex)(3)) Undefined value is default -> There are two posibilities but at the begining the user is forced to pick one. ComboBox.SelectedValue correctly sets SelectedIndex = -1, but WCF throws:
    Enum value '3' is invalid for type 'Sex' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.

  3. Sex property is nullable -> Its optional to set the Sex and u can even unset it once u pick one.

Unfortunately, case 2 is the most common one but we don't see a simple way to deal with it without making hacks like:

  • Define 'Unknown' value for Sex, this value is not valid for the entity, I would consider if it will not be the most common scenario.
  • Mark the enum as [Flags] so WCF can codify "Male,Female" as unknown.
  • Decorate the sex property with something like [ComboHintUnkwnownOnNew].
  • Set SelectedIndex=-1 while keeping the binding some how in WPF.

I know is a thought design decission, not a concrete problem, but I would appreciate some help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我爱人 2024-10-01 00:14:19

如何仅为视图定义一个客户端枚举(-1 表示未定义),然后根据所选的性别创建适当的 WCF 代理枚举,然后将其发送到服务。因此,不要直接将数据绑定到 WCF 代理类。创建仅客户端模型并转换为有效实体(WCF 代理类)是非常有效的。您可以使用自动映射器之类的工具轻松完成此操作。

How about defining a client side enum (with -1 for undefined) for the view only and then depending on the sex selected create appropriate WCF proxy enum and then send this to the service. So do not databind directly to the WCF proxy classes. It is quite valid to create client only models and convert to valid entities (WCF proxy classes). You can use something like automapper to do this easily.

慕巷 2024-10-01 00:14:19

到目前为止,我们找到了以下解决方案:

Sex Sex {get; set;}  //Default Male

Sex? Sex {get; set;}  //Optional field, none is available in the combobox

[NotNullValidator]
Sex? Sex {get; set;}  //Default is null, so SelectedIndex=-1 and can be sent via WCF, but the combo does not have the 'none' option and once one Sex is selected u can not turn back.  

抱歉提出这样一个抽象且难以回答的问题

So far we have found the following solution:

Sex Sex {get; set;}  //Default Male

Sex? Sex {get; set;}  //Optional field, none is available in the combobox

[NotNullValidator]
Sex? Sex {get; set;}  //Default is null, so SelectedIndex=-1 and can be sent via WCF, but the combo does not have the 'none' option and once one Sex is selected u can not turn back.  

Sorry for making such an abstract and hard to answer question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文