UWP绑定枚举差异 [英] UWP - binding Enum differences

查看:189
本文介绍了UWP绑定枚举差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常有趣的问题。

I have come across a very interesting issue.

假设您在UWP应用中有以下XAML页面内容:

Suppose you have the following XAML page content in a UWP app:

<ContentControl Content="{x:Bind ApplicationDataLocalityEnum}" />
<ContentControl Content="{x:Bind FontStyleEnum}" />

在页面的代码隐藏包含以下属性:

And in the code-behind of the page contains the following properties:

    public ApplicationDataLocality ApplicationDataLocalityEnum { get; } =
               ApplicationDataLocality.Local;

    public FontStyle FontStyleEnum { get; } = 
               FontStyle.Normal;

预期的结果是,该应用将显示本地和正常。

Expected result would be, that the app would display "Local" and "Normal".

实际结果如下:

这是什么原因?我很好奇,但即使我已经尝试了很长时间的调试器值,它从来没有透露任何可能导致这种情况的事情。

What is the reason behind this? I am very curious about it, but even though I have tried digging into debugger values for a long time, it never revealed anything that could cause this.

你可以玩耍使用我的 GitHub上的示例项目

推荐答案

如果您查看 FontStyle 的来源(只需点击F12),您会发现在 Windows.Foundation.UniversalApiContract.winmd 中。这不是.NET程序集,而是.NET中投射的本机程序集。 MSDN for IReference 说:

Should you look into the source of FontStyle (just hit F12) you will find it is in Windows.Foundation.UniversalApiContract.winmd. This is not a .NET assembly, but a native assembly projected into .NET. MSDN for IReference says:


当使用.NET编程时,此界面是隐藏的,开发人员应使用Nullable类。所有基本IDL签名显示IReference(具有约束)的Windows运行时成员都会使用可空值类型的空值语法(例如,?bool)进行公开。

When programming with .NET, this interface is hidden and developers should use the Nullable class. All Windows Runtime members where the basic IDL signature shows IReference (with a constraint) are instead exposed using the nullable syntax of the nullable value type (for example, ?bool).



为什么不起作用?



答案的核心是,这不是.NET类型,像.NET类型。意义 ToString()本身不是像枚举一样实现,而是像 GetType()。ToString(),它解释了你看到的输出。

So why doesn't it work?

The heart of the answer is, this is not a .NET type and does not behave like a .NET type. Meaning ToString() is not natively implemented as if it is an enum but acts instead like GetType().ToString(), which explains the output you are seeing.

要在平台方面纠正这个问题,需要一种机制来区分数字,结构和代表。在结构和代理的情况下,您将期望 ToString()返回 GetType()。ToString()和不是名字值所以这种通用行为是选项中最常见的选择。

To correct this on the platform side, the type would need a mechanism to differentiate between numerations, structures and delegates. In the case of structures and delegates, you would expect ToString() to return GetType().ToString() and not a name value; so this generic behavior is the most common choice across the options.

这篇关于UWP绑定枚举差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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