【问题标题】:XAML Converter in different namespace不同命名空间中的 XAML 转换器
【发布时间】:2015-05-06 13:45:21
【问题描述】:

我正在 VC2013 中开发一个 c# Windows Phone 8.1 应用程序并偶然发现了一个奇怪的问题。

为了让我的代码更“干净”,我决定将应用程序的不同部分放入不同的文件夹中。在 c# 代码中从这些文件夹调用 XAML 页面没有问题。

但是我似乎在 XAML 代码本身中链接这些文件夹时遇到问题。例如我有以下结构:

root, Files: App.cs+xaml, Mainpage.cs+xaml
|
-- Folder: Login
     |
     -- Files: LoginPage.cs+xaml
-- Folder: Converters
     |
     -- Files: converterClass.cs

要像往常一样使用converterClass.cs 中的IValueConverter,我将以下内容放在我的 XAML 文件的标题中:

<Page
 x:Class="myApp.Login.LoginPage"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:local="using:meinStundenplaner.Login"
 xmlns:myConverter="using:myApp.Converters"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../Styles/standardStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <myConverter:DateTimeConverter x:Key="DateTimeConverter" />
        ... some more converters

    </ResourceDictionary>
</Page.Resources>
...

现在发生了,如果我键入 &lt;myConverter:,自动完成功能确实会像往常一样找到我的转换器类,但是在编译(正在编译)和在设备上测试时,所有转换器都不起作用,而且我在 Error -列举说法

The name 'DateTimeConverter' in namespace 'using:myApp.Converters' does not exist.

我哪里做错了?

【问题讨论】:

  • 再次检查 DateTimeConverter 类是否在 myApp.Converters 命名空间中。您可能已将文件拖到文件夹中,但忘记更改 C# 代码中的命名空间。
  • 另外,请检查您的转换器是否实际称为 DateTimeConverter。
  • 我很困惑。根据this(以及其他参考资料),对于Windows Phone 应用程序,您应该在命名空间声明中使用clr-namespace 而不是using。您真的在编写 Windows Phone 应用程序吗?如果是这样,你为什么不使用clr-namespace?如果你这样做会发生什么?如果编辑器不知道 Windows Phone 和 Windows RT 之间的区别,因此我不会感到惊讶
  • 好的,我首先尝试将其重命名为clr-namespace,但和之前一样,当我输入命名空间,它将其更正回using。命名空间和转换器的名称正确(再次检查)
  • 谢谢彼得·杜尼霍!我正在编写一个作为 UniversalApp 的 win10mobile 应用程序,我需要切换到 using 而不是 clr-namespace

标签: c# xaml namespaces windows-phone-8.1 converter


【解决方案1】:

哇...在使用 VS2015 之后,同样的错误仍然存​​在,我在设置和其他东西上玩了一下。

我一直很好奇,为什么它会编译和部署,即使它触发了这么多错误。问题似乎不一致。

解决方案与预期不同:

您必须将Solution Platforms 设置为x86 而不是ARM(我需要在设备上部署)。幸运的是 VS2015 在一个方便的快捷方式中提供了这个选项,这是 VS2013 所不能说的。

还是解决了……

【讨论】:

    猜你喜欢
    • 2015-10-29
    • 2013-04-14
    • 2015-04-28
    • 2015-08-19
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 2011-10-28
    相关资源
    最近更新 更多