简体   繁体   English

C# UWP项目如何正确添加WinUI控件

[英]How correctly add WinUI control to C# UWP project

I try to add Microsoft.UI.Xaml.Controls.TreeView in my C# UWP project.我尝试在我的 C# UWP 项目中添加 Microsoft.UI.Xaml.Controls.TreeView。 But when I debug my app LoadComponent(...) throws XamlParseException:但是当我调试我的应用程序 LoadComponent(...) 时抛出 XamlParseException:

在此处输入图像描述

I follow all the steps described in Microsoft WinUI Docs .我遵循Microsoft WinUI Docs中描述的所有步骤。 I don't need to create WinUI project.我不需要创建 WinUI 项目。
My project properties:我的项目属性:

在此处输入图像描述

My project repos Here the minidump is committed.我的项目回购小型转储已提交。

My project links:我的项目链接:

在此处输入图像描述

Application XAML:申请XAML:

<Application
    x:Class="SqlDBObjects.SqlDbApplication"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Microsoft.UI.Xaml.Controls"
    xmlns:local="using:SqlDBObjects">
    <Application.Resources>
        <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>
    </Application.Resources>
</Application>

My main window content is custom control based on ContentControl.我的主要内容window是基于ContentControl的自定义控件。 It's XAML fragment:这是 XAML 片段:

<ContentControl
    x:Class="SqlDBObjects.SqlDbContentControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
    xmlns:mctk="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:local="using:SqlDBObjects"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Name="SQLDBContentControl"
    Background="LightGray"
    Loaded="OnLoaded">
    ...
    <muxc:TreeView
        Name="DBObjectTree"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        Background="White"
        ItemsSource="{x:Bind Items, Mode=TwoWay}"
        ItemTemplate="{StaticResource DBOBJECT_TEMPLATE}"
        Expanding="OnTreeViewExpanding">
    </muxc:TreeView>
    ...
</ContentControl>

But when I debug my app LoadComponent(...) throws XamlParseException但是当我调试我的应用程序时 LoadComponent(...) 抛出 XamlParseException

I could reproduce your problem when missing add <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" /> .当缺少添加<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />时,我可以重现您的问题。 please follow Getting started with the Windows UI 2 Library document, and install Microsoft.UI.Xaml nuget version 2.7.请按照 Getting started with the Windows UI 2 Library文档,安装Microsoft.UI.Xaml nuget 2.7 版本。 then build your app.然后构建您的应用程序。 if the problem still exist, please clean your project or remove bin and obj folder.如果问题仍然存在,请清理您的项目或删除 bin 和 obj 文件夹。

Update更新

The problem is you custom app's entry point as SqlDbApplication , and this causes winui XamlControlsResources can't load correctly.问题是您自定义应用程序的入口点为SqlDbApplication ,这会导致 winui XamlControlsResources无法正确加载。 please feel free post this problem in WinUI github issue box.请随时在 WinUI github 问题框中发布此问题。 Currently there is a workaround that is edit SqlDbApplication to App as the app's default entry point.目前有一种变通方法是将SqlDbApplication编辑为App作为应用程序的默认入口点。 (please don't forget update the entry-point within package manifest) (请不要忘记更新 package 清单中的入口点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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