【问题标题】:Cannot create xmlns for Xamarin.Forms project namespace无法为 Xamarin.Forms 项目命名空间创建 xmlns
【发布时间】:2018-06-27 11:01:27
【问题描述】:

当我尝试为当前项目创建新的 XAML 命名空间时遇到以下编译错误

Error: Failed to resolve assembly: 'TodoQ.dll, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'

我一直在使用我自己和外部项目中的自定义控件,编译器从来没有抱怨过。这是 XAML(省略号是我决定省略的其他一些标准命名空间):

<ContentPage xmlns:util="clr-namespace:TodoQ.Utilities" 
             xmlns:custom="clr-namespace:TodoQ.Controls" 
             xmlns:swipecards="clr-namespace:SwipeCards.Controls;assembly=SwipeCards.Controls"
             ... x:Class="TodoQ.Views.ItemsPage" Title="{Binding Title}" x:Name="BrowseItemsPage">

如您所见,我有两个命名空间,它们引用我当前的 Xamarin.Forms 项目。 custom 命名空间有效,但如果我尝试使用 util 我会收到上述错误。另一方面,swipecards 命名空间完美运行。

我尝试过指定程序集,例如 xmlns:util="clr-namespace:TodoQ.Utilities;assembly=TodoQ",但似乎没有什么不同。

到目前为止,我对util 的唯一用途是尝试添加转换器:

<Grid.Resources>
    <util:IsZero x:Key="IsZero" />
</Grid.Resources>

有人知道我做错了什么吗?我几乎把我的头发扯掉了。使用 WPF 版本的 XAML,事情就简单多了,一切似乎都在那里工作。

编辑:这是所要求的实用程序文件:

using System;
using System.Globalization;
using Xamarin.Forms;

namespace TodoQ.Utilities
{
    public class IsZero : IValueConverter
    {
        public object Convert(object value,
                             Type targetType,
                             object parameter,
                             CultureInfo language)
        {
            return ((int)value) == 0;
        }

        public object ConvertBack(object value,
                             Type targetType,
                             object parameter,
                                  CultureInfo language)
        {
            throw new NotImplementedException();
        }
    }
}

【问题讨论】:

  • 粘贴你的命名空间和你的实用程序文件的类名,即文件的顶部,如果文件很小,则粘贴整个文件
  • @TheGeneral 查看我的编辑,谢谢。
  • 也许您应该将程序集名称更改为TodoQ.exe

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

老实说,我认为它是另一回事,这可能是您尝试使用它的方式,您的代码是正确的:

ItemsPage

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:util="clr-namespace:TodoQ.Converters;assembly=TodoQ"  
    x:Class="TodoQ.Views.ItemsPage">
    <StackLayout.Resources>
        <ResourceDictionary>
            <util:IsZero x:Key="IsZero" /> 
        </ResourceDictionary>
    </StackLayout.Resources>
    <ContentPage.Content>
        <StackLayout>
            <Grid HorizontalOptions="CenterAndExpand" >
                <Label Text="Hello World" Grid.Row="{Binding UserL , Converter={StaticResource IsZero}}" Grid.Column="0" />
            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

【讨论】:

  • 问题是我的转换器声明中有x:Name,而不是x:Key。 ??‍♂️ 典型!
  • @Luke 至少应该接受我的回答,在应得的地方给予表扬
  • 我真的不觉得你给了我解决方案,虽然你确实促使我更仔细地查看我的代码,所以我会在此基础上接受你的回答。
【解决方案2】:

我发现了问题所在。愚蠢的错误。典型的。

我缺少&lt;ResourceDictionary&gt; 标签,并且转换器定义中还有x:Name 而不是x:Key

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多