尝试create a "require all" multi trigger within Xamarin时,我会在执行时抛出异常。
错误:
Xamarin.Forms.Xaml.XamlParseException:位置18:25。静态资源 找不到关键数据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
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 ();
}
}
}
我不熟悉xaml或c#如何处理类,所以我的故障排除并没有解决任何问题。
答案 0 :(得分:1)
我错过了一些针对我的资源嵌套的xml类型结构。这次更新后一切正常。
MyPage.xaml:
<ContentPage ...>
.
.
.
<ContentPage.Resources> <!--***Added this part***-->
<ResourceDictionary>
...
</ResourceDictionary>
</ContentPage.Resources>
.
.
.
<Button ...>
<Button.Triggers> <!--***Added this part***-->
<MultiTrigger ...>
.
.
.
</MultiTrigger>
</Button.Triggers>
.
.
.