XAML 未检测到已安装的组件,无法解析TargetName HighContrastBorder - UWP Windows 10

sirbozc5  于 12个月前  发布在  Windows
关注(0)|答案(3)|浏览(122)

我的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

关于我的两个问题的任何想法,即文本截断和未处理异常?
谢谢

UPDATE - 1:

当我从CommandBarVisible属性中删除binded属性(CommandBarViewModel.IsCommandBarVisible)并将其硬编码为Visible时,错误将沿着行向下移动,而不是发生在App.g.i.cs中,它现在发生在它试图设置可见性的第一个按钮的binded属性上。

<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???没道理啊!
有什么建议/想法来解决这个问题吗?

UPDATE - 2:

如果我删除所有可见性属性(及其相应的绑定),则相应地显示命令(即没有更多的切断文本),我可以点击省略号按钮一遍又一遍,它不再崩溃!!
所以它肯定与Visibility相关,并将其绑定到视图模型的属性,但具体是什么,我不知道。
是否只有在页面加载时才构造ViewModel,并且在这个阶段,命令栏和它的按钮正确初始化已经太晚了。
奇怪的是,关于显示/隐藏按钮的一切都按预期工作,除了我的文本被切断,我不能点击省略号按钮或它崩溃。

UPDATE - 3:

我找到了一个变通的办法,我并没有因为我觉得这是错误的而上下跳动,但现在它就可以了。为了避免这个错误,我确保在初始化ViewModel时将命令栏和按钮设置为可见,然后根据它所在的页面相应地隐藏它们。
public class AppShellViewModel { public void 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;

}
就我个人而言,我觉得这是一个错误的命令栏控件和按钮,因为我应该能够隐藏它(和它的按钮)从一开始就去,它应该
a)能够处理这个没有任何错误。B)能够正确地“重绘”自身而不需要切断文本。
当然,我可能是错的,这可能与我的代码有关,但从我的Angular 来看,删除可见性绑定修复了它,并使其可见,首先修复了它,所以它似乎指向这个方向。

更新- 4:

下面是实际代码。我已经尽可能地简化了它(即)。删除名称空间、数据模板、主内容等),只留下命令栏及其按钮。希望这能帮上忙。
正如您在使用mvvmlight时所看到的,我的源代码被设置为Locator,路径被设置为相关的ViewModel,在本例中是AppShellViewModel。
然而,正如我向Grace解释的那样,当我使用x:bind而不是binding时,我会得到以下错误:

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>

谢谢.

qzlgjiam

qzlgjiam1#

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

<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值时,可见性会相应更新。

tf7tbtn2

tf7tbtn22#

没有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:BindRefresh按钮设置为不可见,而Add按钮设置为可见,您会发现CommandBar中没有Refresh按钮的空间,Add按钮将取代它。这可以证实我的观点,如果你在初始化Commandbar时将false设置为AppbarButton,它不会生成这个按钮。
未检测到已安装的组件。无法解析TargetName HighContrastBorder
如果检查template of CommandBar,您将看到在CommandBar内部有一个名为HighContrastBorderRectangle
或者你可以在代码运行时使用Live Visual Tree来查找这个HighContrastBorder

正如您所看到的,它是CommandBarContentRoot中的Rectangle
我不能重现这个问题,或文本切断的问题,他们总是在我身边的罚款。我正在使用MVVM,但我没有使用MVVMLight来测试这个问题。也许你可以分享你的样本。

nwo49xxi

nwo49xxi3#

我刚刚得到了完全相同的错误,并跟踪到同一个ListView示例被显示了两次。
[我已经将用于将示例添加到显示列表的代码移动到了一个方法中,但忘记删除原始的代码。]
您的XAML可能会发生同样的事情。

相关问题