XAML MAUI转换器抛出异常

bxjv4tth  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(163)

我的项目需要根据是否设置了LeftAccessibilityName在可访问性树中添加或删除UI元素。为此,我将AutomationProperties.IsInAccessibleTree绑定到可访问性名称,然后使用IsStringNotNullOrEmpty转换器获取布尔值。每当我尝试导航到包含此控件的页面时,都会出现以下导航异常:

Id = 1, Status = RanToCompletion, Method = {null}, Result = NavigationResult { Success = False, Exception = Prism.Navigation.NavigationException: An error occurred while resolving the page. This is most likely the result of invalid XAML or other type initialization exception
 ---> System.Exception: Unable to create Page 'RegistrationUserDetailsPage'.
 ---> Prism.Ioc.ContainerResolutionException: An unexpected error occurred while resolving 'RegistrationUserDetailsPage'
 ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ArgumentException: targetType needs to be assignable from System.Boolean. (Parameter 'targetType')

字符串
如果我删除转换器,我可以毫无问题地导航到我的页面。我尝试使用更详细的语法:

<Grid 
     xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     xmlns:converters="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
     x:Name="This">
   <Grid.Resources>
        <ResourceDictionary>
            <converters:IsStringNotNullOrEmptyConverter x:Key="IsStringNotNullOrEmptyConverter"/>
        </ResourceDictionary>
    </Grid.Resources>

   <ContentView
        Grid.Column="0"
        AutomationProperties.IsInAccessibleTree="{Binding LeftAccessibilityName, Converter={StaticResource IsStringNotNullOrWhiteSpaceConverter}, Source={x:Reference This}}" />

...


但这并没有什么区别。
在我的MauiProgram.cs中,如果我像这样初始化社区工具包:.UseMauiCommunityToolkit(x => x.SetShouldSuppressExceptionsInConverters(true));问题也解决了,我可以导航到页面。

代码

<Grid
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:converters="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    x:Name="This"
    ColumnDefinitions="*">

    <ContentView
        Grid.Column="0"
        AutomationProperties.IsInAccessibleTree="{Binding LeftAccessibilityName, Converter={converters:IsStringNotNullOrWhiteSpaceConverter}, Source={x:Reference This}}" />
</Grid>

sirbozc5

sirbozc51#

我无法重现您的问题,IsStringNotNullOrEmptyConverter对我有效。但是你的代码有问题。
1.在xaml中,请在声明转换器为资源之前添加命名空间。例如:

<Grid xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             >

    <Grid.Resources>
        <ResourceDictionary>
            <mct:IsStringNotNullOrEmptyConverter x:Key="IsStringNotNullOrEmptyConverter" />
        </ResourceDictionary>
    </Grid.Resources>

</Grid>

字符串
1.如果LeftAccessibilityName是viewmodel中的属性,请删除绑定中的Source={x:Reference This},您已经将该属性绑定到LeftAccessibilityName。例如:

<Grid
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:components="clr-namespace:Weighter.UI.Components"
    xmlns:converters="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    x:Name="This"
    ColumnDefinitions="*">

    <ContentView
        Grid.Column="0"
        AutomationProperties.IsInAccessibleTree="{Binding LeftAccessibilityName, Converter={converters:IsStringNotNullOrWhiteSpaceConverter}}" />
</Grid>


1.如果LeftAccessibilityName是ContentView或其他控件的属性,请更改有关绑定的代码。您可以查看有关绑定值转换器的官方文档。

相关问题