StaticResource не найден для ключа

При попытке создать мультитриггер "требовать все" в Xamarin, я выбрасываю исключение при выполнении.

Ошибка:

Xamarin.Forms.Xaml.XamlParseException: позиция 18:25. StaticResource не найден для ключевых данныхHasBeenEntered

Что подчеркивает ошибку в RaceTeam.MyPage.xaml.g.cs (последняя строка)

//  <autogenerated>

namespace RaceTeam {
    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;


    public partial class MyPage : global::Xamarin.Forms.ContentPage {

        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::Xamarin.Forms.Entry carWeight;

        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::Xamarin.Forms.Entry carDistro;

        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::Xamarin.Forms.Button calculateButton;

        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private void InitializeComponent() {
            this.LoadFromXaml(typeof(MyPage)); //this line

Моя страница.xaml:

<?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:local="clr-namespace:RaceTeam;assembly=RaceTeam"
x:Class="RaceTeam.MyPage">
    <ResourceDictionary>
       <local:MultiTriggerConverter x:Key="dataHasBeenEntered"/>
    </ResourceDictionary>
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness" iOS="20, 40, 20, 20" Android="20, 20, 20, 20" WinPhone="20, 20, 20, 20" />
    </ContentPage.Padding>
    <ContentPage.Content>
        <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Orientation="Vertical" Spacing="15">
            <Label Text="Car Weight (lbs)" />
            <Entry x:Name="carWeight" Text="" Keyboard="Numeric" Placeholder="1000-9000" />
            <Label Text="Car (Front) Distribution %" />
            <Entry x:Name="carDistro" Text="" Keyboard="Numeric" Placeholder="25-75" />
            <Button x:Name="calculateButton" Text="Calculate" Clicked="onCalculate" IsEnabled="false">
                <MultiTrigger TargetType="Button">
                    <MultiTrigger.Conditions>
                        <BindingCondition Binding="{Binding Source={x:Reference carWeight}, Path=Text.Length, Converter={StaticResource dataHasBeenEntered}}" Value="true"/>
                        <BindingCondition Binding="{Binding Source={x:Reference carDistro}, Path=Text.Length, Converter={StaticResource dataHasBeenEntered}}" Value="true"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="IsEnabled" Value="True" />
                </MultiTrigger>
            </Button>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

MyPage.xaml.cs

using System;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Globalization;

namespace RaceTeam
{
    public partial class MyPage : ContentPage
    { //...//
    }

    public class MultiTriggerConverter : IValueConverter
    {
        public object Convert (object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            //...//
        }
        public object ConvertBack (object value, Type targetType,
                                  object parameter, CultureInfo culture)
        {
            throw new NotSupportedException ();
        }
    }
}

Я не знаком с xaml или с тем, как C# обрабатывает классы, поэтому устранение неполадок ничего не исправило.


person Jim    schedule 17.02.2016    source источник


Ответы (1)


Мне не хватало некоторой вложенности структуры типа xml для моих ресурсов. После этого обновления все заработало.

Моя страница.xaml:

<ContentPage ...>
   .
   .
   .
   <ContentPage.Resources>  <!--***Added this part***-->
            <ResourceDictionary>
                ...
            </ResourceDictionary>
   </ContentPage.Resources>
      .
      .
      .
      <Button ...>
         <Button.Triggers>   <!--***Added this part***-->
            <MultiTrigger ...>
                .
                .
                .
            </MultiTrigger>
         </Button.Triggers>
         .
         .
         .
person Jim    schedule 19.02.2016
comment
Не могли бы вы показать более подробную структуру типа xml? Это доставляло мне неприятности в течение некоторого времени. Спасибо!! - person AmyNguyen; 16.06.2020
comment
@AmyNguyen Я не совсем уверен ... Я не настоящий программист ... но я заметил тогда (это было более 4 лет назад), что примеры и API, которые я нашел в Интернете, обычно имели эти ContentPage. Разделы ресурсов... а у меня их не было. Я бы сравнил ваш код с этими сайтами: здесь1 и здесь2 а затем посмотреть, чего не хватает. - person Jim; 17.06.2020