【问题标题】:No installed components were detected. Cannot resolve TargetName HighContrastBorder - UWP Windows 10未检测到已安装的组件。无法解析 TargetName HighContrastBorder - UWP Windows 10
【发布时间】:2016-08-03 01:11:48
【问题描述】:

我的CommandBar在 XAML 中将其 IsOpen 属性设置为 true,因此每个按钮的 is 文本都是可见的,因为我希望描述保持可见。

当我单击省略号按钮时,它会隐藏文本,第二次单击它时,出现以下错误:

No installed components were detected. Cannot resolve TargetName HighContrastBorder.

UI 出现了一些尴尬,可能与此有关,但我不知道是什么原因或原因:

如您所见,我正在显示的各种按钮的按钮文本被截断:

代码方面,据我所见,没有什么特别之处:

<Page.BottomAppBar>
    <CommandBar IsOpen="True" 
                ClosedDisplayMode="Compact" 
                IsSticky="True"
                Visibility="{Binding 
                CommandBarViewModel.IsCommandBarVisible, 
                Converter={StaticResource BoolToVisibilityConverter}}"
                Background="{ThemeResource SystemControlBackgroundAccentBrush}">

        <AppBarButton 
            Icon="Add" 
            Label="Add" 
            Foreground="White"
            Command="{Binding CommandBarViewModel.AddCommand}"
            Visibility="{Binding CommandBarViewModel.IsAddVisible,
                         Converter={StaticResource BoolToVisibilityConverter}}"/>

        <AppBarButton 
            Icon="Refresh" 
            Label="Refresh" 
            Foreground="White" 
            Command="{Binding CommandBarViewModel.RefreshListCommand}" 
            Visibility="{Binding 
            CommandBarViewModel.IsRefreshListVisible, 
            Converter={StaticResource BoolToVisibilityConverter}}"/>
    </CommandBar>
</Page.BottomAppBar>

没有InnerException,从App.g.i.cs抛出异常

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
 UnhandledException += (sender, e) =>
        {
            if (global::System.Diagnostics.Debugger.IsAttached) 
            global::System.Diagnostics.Debugger.Break();
        };
#endif

关于我的问题(即文本截断和未处理的异常)有什么想法吗?

谢谢

更新 - 1:

当我从 CommandBarVisible 属性中删除绑定属性 (CommandBarViewModel.IsCommandBarVisible) 并将其硬编码为 Visible 时,错误会向下移动,而不是在 App.gics 中发生,它现在正在发生它试图在 ie 上设置可见性的第一个按钮的绑定属性

        <AppBarButton 
            Icon="Add" 
            Label="Add" 
            Foreground="White"
            Command="{Binding CommandBarViewModel.AddCommand}"
            Visibility="{Binding CommandBarViewModel.IsAddVisible,
                         Converter={StaticResource BoolToVisibilityConverter}}"/>

但这次我得到以下错误:

An exception of type 'System.Runtime.InteropServices.COMException' 
occurred in GalaSoft.MvvmLight.dll but was not handled in user code

WinRT information: Cannot resolve TargetName HighContrastBorder.

Additional information: No installed components were detected.

如您所见,但它似乎来自 MVVMLight???毫无意义!

关于如何解决此问题的任何建议/想法?

更新 - 2:

如果我删除所有可见性属性(及其相应的绑定),命令会相应地显示(即不再有截断文本),我可以一遍又一遍地单击省略号按钮,它不再崩溃!

所以它肯定与 Visibility 相关,并将其绑定到视图模型的属性,但具体是什么,我不知道。

可能是 ViewModel 仅在页面加载时构建,在这个阶段,CommandBar 和它的按钮正确初始化为时已晚。

奇怪的是,关于显示/隐藏按钮的所有内容都按预期工作,除了我的文本被截断并且我无法单击省略号按钮或它崩溃。

更新 - 3:

我找到了一种解决方法,我不会因为我觉得它是错误的而跳来跳去,但现在它会做。为了避免这个错误,我确保在初始化 ViewModel 时将命令栏和按钮设置为可见,然后根据它所在的页面相应地隐藏它们。

公共类 AppShellViewModel { 公共无效 AppShellViewModel { this.CommandBarViewModel.IsCommandBarVisible = true; this.CommandBarViewModel.IsAddVisible = true; this.CommandBarViewModel.IsRefreshVisible = true; this.CommandBarViewModel.IsCancelVisible = true; }

...

\\Hide buttons accordingly in the various parts of your app.
this.CommandBarViewModel.IsCancelVisible = false;

}

我个人觉得这是 CommandBar 控件和按钮的错误,因为我应该能够从一开始就隐藏它(及其按钮),而且应该

a) 能够在没有任何错误的情况下处理此问题。 b) 能够正确地“重绘”自身而无需切断文本。

我当然可能是错的,这可能与我的代码有关,但从我的角度来看,删除可见性绑定可以修复它并使其可见首先修复它,所以它似乎指向这种方式。

更新 - 4:

这是实际代码。我已经尽可能地简化了它(即删除了命名空间、DataTemplates、主要内容等),只留下了 CommandBar 及其按钮。希望这会有所帮助。

正如您在使用 mvvmlight 时所见,我的源设置为 Locator,我的路径设置为相关的 ViewModel,在本例中为 AppShellViewModel。

但正如向 Grace 解释的那样,当我使用 x:bind 而不是绑定时,我收到以下错误:

Invalid binding path 'CommandBarViewModel.IsCommandBarVisible' :
Property 'CommandBarViewModel' can't be found on type 'AppShell'    
MyApp ..\MyApp\Views\AppShell.xaml


XAML 代码:

    <Page.Resources>
        <converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
        <converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
        <x:Double x:Key="EllipseDimension">30</x:Double>
    </Page.Resources>

    <Page.BottomAppBar>
        <CommandBar x:Name="AppBar"
                    IsOpen="{x:Bind CommandBarViewModel.IsCommandBarVisible}" 
                    ClosedDisplayMode="Compact" 
                    IsSticky="{x:Bind CommandBarViewModel.IsCommandBarVisible}"
                    Visibility="{x:Bind CommandBarViewModel.IsCommandBarVisible, 
Converter={StaticResource BoolToVisibilityConverter}}"
                    Background="{ThemeResource SystemControlBackgroundAccentBrush}"
                    IsEnabled="{x:Bind IsNotBusy}">
            <AppBarButton 
                Icon="Add" 
                Label="Add" 
                Foreground="White"
                Command="{x:Bind CommandBarViewModel.RegisterCommand}"
                Visibility="{x:Bind CommandBarViewModel.IsRegisterVisible, 
Converter={StaticResource BoolToVisibilityConverter}}"/>

            <AppBarButton 
                Icon="Refresh" 
                Label="Refresh" 
                Foreground="White" 
                Command="{x:Bind CommandBarViewModel.RefreshListCommand}" 
                Visibility="{x:Bind CommandBarViewModel.IsRefreshListVisible, 
Converter={StaticResource BoolToVisibilityConverter}}"/>
        </CommandBar>
    </Page.BottomAppBar>
</Page>

谢谢。

【问题讨论】:

    标签: xaml win-universal-app windows-10-universal uwp-xaml


    【解决方案1】:

    我用你的代码创建了一个示例项目:

    xaml:

    <Page
        x:Class="CommandBarSample.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:CommandBarSample"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="root">
    
        </Grid>
    
        <Page.BottomAppBar>
    
                <CommandBar IsOpen="True" 
                    IsSticky="True"
                            ClosedDisplayMode="Compact"
                    Background="{ThemeResource SystemControlBackgroundAccentBrush}">
    
                    <AppBarButton 
                Icon="Add" 
                Label="Add" 
                Foreground="White"
                Command="{Binding CommandBarViewModel.RegisterCardCommand}"
                />
    
                    <AppBarButton 
                Icon="Refresh" 
                Label="Refresh" 
                Foreground="White" 
                Command="{Binding CommandBarViewModel.RefreshCardListCommand}" 
                />
                </CommandBar>
    
        </Page.BottomAppBar>
    </Page>
    

    xaml.cs:

    默认模板,不做任何更改。

    我无法重现您的异常和 UI 错误,这是我的猜测:在您项目的某处,您提到 HighContrastBorder 作为样式的目标。我建议您在其他地方找到并修复它,您发布的代码正在运行。我的构建是 14316 桌面。

    更新

    我将Visibility="{Binding ShowButton}"添加到AppBarButton,将this.DataContext = this;添加到xaml.cs的构造函数

    在 xaml.cs 中添加以下内容:

    private bool _ShowButton = true;
    
            public bool ShowButton
            {
                get
                {
                    return _ShowButton;
                }
                set
                {
                    Set(ref _ShowButton, value);
                }
            }
    
            private void Set(ref bool _ShowButton, bool value, [CallerMemberName] string property = "")
            {
                _ShowButton = value;
                NotifyPropertyChanged(property);
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
    

    当我更改 ShowButton 值时,可见性会相应更新。

    【讨论】:

    • 我已经查看了 HighContrastBorder 的整个解决方案,但找不到它?还有其他建议吗?我要更新我的答案我发现了一些奇怪的东西。
    • 我更新了我的答案,仍然无法重现你的错误,你能把你的样本发给我吗?
    【解决方案2】:

    没有InnerException,从App.g.i.cs抛出异常

    #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
     UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) 
                global::System.Diagnostics.Debugger.Break();
            };
    #endif
    

    Add 按钮或Refresh 按钮在代码运行的最开始设置为不可见时,我可以重现此问题。

    要解决此问题,您可以使用x:Bind 代替Binding。 {x:Bind} 执行在编译时生成的专用代码,而 {Binding} 使用通用运行时对象检查。

    如果一开始将IsAddVisibleIsRefreshListVisible设置为“false”(不可见),它不会生成匹配的AppbarButton,使用Binding会发生错误。比如你用x:Bind设置Refresh按钮不可见,Add按钮可见,你会发现CommandBar中没有Refresh按钮的空间,Add按钮就会更换它。这可以证实我的观点,如果在初始化Commandbar 时将false 设置为AppbarButton,则不会生成此按钮。

    未检测到已安装的组件。无法解析 TargetName HighContrastBorder

    如果您检查template of CommandBar,您会看到RectangleCommandBar 内部有一个名为HighContrastBorder

    或者你可以在你的代码运行时使用 Live Visual Tree 找到这个HighContrastBorder

    如您所见,它是CommandBarContentRoot 中的Rectangle

    我无法重现此问题或文本截断问题,它们在我身边总是可以正常工作。我正在使用 MVVM,但我没有使用 MVVMLight 来测试问题。也许你可以分享你的样本。

    【讨论】:

    • 嗨,格蕾丝,感谢您提供详细的反馈。使用 x:bind 时出现错误:无效的绑定路径“CommandBarViewModel.IsCommandBarVisible”:当我调用时,在类型“AppShell”上找不到属性“CommandBarViewModel”:IsOpen="{x:Bind CommandBarViewModel.IsCommandBarVisible} " - 同样的错误适用于所有 AppBarButton,而不仅仅是 CommandBar。我不确定它为什么要在 AppShell 中寻找它,因为这是视图而不是在我的 ViewModel 中,即 AppShellViewModel。我尝试将路径设置为 AppShellViewModel,但出现另一个错误:“路径设置多次”。有什么想法吗?
    • 嗨,格蕾丝,我已经添加了 xaml 代码。希望这会有所帮助。谢谢。
    • @Thierry,是的,如果您使用 x:Bind,请尝试将 CommandBarViewModel.IsCommandBarVisible 更改为 IsCommandBarVisible,其他两个也一样。
    • 它如何知道 IsCommandBarVisible 绑定到 CommandBarViewModel 而不是 AppShellViewModel。当然,必须设置其他内容。我已经尝试了你的建议,但我仍然遇到同样的错误,嗯,类似的。我目前正在谷歌搜索 x:bind 示例,看看使用它时还需要做什么。
    • @Thierry,我很抱歉,我意识到我在上一条评论中混淆了 binding 和 x:bind。在使用Binding的时候,在后面的代码中,我新建了一个CommandBarViewModel的实例,并将它设置为这个页面的datacontext,它会找到这个DataContext,所以我们可以直接使用Visibility="{Binding IsCommandBarVisible, Converter={StaticResource BoolToVisibilityConverter}}",而使用x:Bind的时候,它应该是Visibility="{x:Bind CommandBarViewModel.IsCommandBarVisible, Converter={StaticResource BoolToVisibilityConverter}}"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2011-12-28
    • 2022-01-17
    • 2019-02-26
    • 1970-01-01
    • 2014-12-22
    • 2015-07-10
    相关资源
    最近更新 更多