简体   繁体   中英

StaticResource not found for key

When trying to create a "require all" multi trigger within Xamarin , I throw an exception when executing.

Error:

Xamarin.Forms.Xaml.XamlParseException: Position 18:25. StaticResource not found for key dataHasBeenEntered

Which highlights there error in RaceTeam.MyPage.xaml.g.cs (last line)

//  <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

MyPage.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 ();
        }
    }
}

I'm not familiar with xaml or how c# handles classes, so my troubleshooting hasn't fixed anything.

I was missing some xml-type structure nesting for my resources. Everything worked fine after this update.

MyPage.xaml:

<ContentPage ...>
   .
   .
   .
   <ContentPage.Resources>  <!--***Added this part***-->
            <ResourceDictionary>
                ...
            </ResourceDictionary>
   </ContentPage.Resources>
      .
      .
      .
      <Button ...>
         <Button.Triggers>   <!--***Added this part***-->
            <MultiTrigger ...>
                .
                .
                .
            </MultiTrigger>
         </Button.Triggers>
         .
         .
         .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM