C# 如何将xbf文件添加到visual studio项目

C# 如何将xbf文件添加到visual studio项目,c#,windows-8,windows-store-apps,windows-10,win-universal-app,C#,Windows 8,Windows Store Apps,Windows 10,Win Universal App,我已经为Windows通用平台(Win 10 UWP)创建了一个类库 该库包含一些用户控件 当我将此库中的dll添加到Win 10 UWP应用程序中并使用UserControls时,它会给出一个XamlParseException,如前所述 但是当我引用整个项目时,没有例外,我可以使用UserControl。这可能是因为当我引用dll文件时,有一些xbf文件没有添加到Win 10应用程序项目中 在某个项目中,我需要手动将xbf文件添加到Win 10应用程序项目中,我无法引用整个项目,我只能引用d

我已经为Windows通用平台(Win 10 UWP)创建了一个类库

该库包含一些用户控件

当我将此库中的dll添加到Win 10 UWP应用程序中并使用UserControls时,它会给出一个XamlParseException,如前所述

但是当我引用整个项目时,没有例外,我可以使用UserControl。这可能是因为当我引用dll文件时,有一些xbf文件没有添加到Win 10应用程序项目中

在某个项目中,我需要手动将xbf文件添加到Win 10应用程序项目中,我无法引用整个项目,我只能引用dll并添加所需的文件

我尝试在Visual Studio项目中创建一个文件夹并添加xbf文件,还尝试使用不同的名称创建文件夹并通过windows资源管理器将xbf文件复制到“bin”目录中。但是没有成功

那么,如何将xbf文件添加到Windows10UWP项目中

更新1:-XAML和代码供参考

public sealed partial class CustomPopupControl : UserControl
{
    internal CustomPopupControl()
    {
        this.InitializeComponent();    //-------CRASHES HERE-------
    }

    internal CustomPopupControl() : base()
    {
        Debug.WriteLine("CustomPopupControl");
        //
        //do some stuff
        //
        //
    }

    private void OnPopupLoaded(object sender, RoutedEventArgs e)
    {
        this.Popup_Container.HorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        this.Popup_Container.VerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;
    }

    internal void OpenPopup()
    {
        Debug.WriteLine("OpenPopup");
        Popup_Container.IsOpen = true;

        var currentFrame = Window.Current.Content as Frame;
        var currentPage = currentFrame.Content as Page;
        currentPage.IsHitTestVisible = false;

        Debug.WriteLine("OpenPopup Done");
    }

    private void OnLayoutUpdated(object sender, object e)
    {
        if (Grid_Child.ActualWidth == 0 && Grid_Child.ActualHeight == 0)
        {
            return;
        }

        double ActualHorizontalOffset = Popup_Container.HorizontalOffset;
        double ActualVerticalOffset = Popup_Container.VerticalOffset;

        double NewHorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        double NewVerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;

        if (ActualHorizontalOffset != NewHorizontalOffset || ActualVerticalOffset != NewVerticalOffset)
        {
            Popup_Container.HorizontalOffset = NewHorizontalOffset;
            Popup_Container.VerticalOffset = NewVerticalOffset;
        }
    }
}
XAML:-

<UserControl
x:Class="MyLibrary.CustomPopupControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyLibrary"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Popup Name="Popup_Container" LayoutUpdated="OnLayoutUpdated">
    <Border BorderThickness="1" BorderBrush="{ThemeResource AppBarBorderThemeBrush}">
        <Grid Name="Grid_Child">
            <Grid Name="Grid_Content"  Height="300" Width="400" />
        </Grid>
    </Border>
</Popup>

尝试将构造函数定义为公共的而不是内部的

另外,您的第二个构造函数正在调用base,但如果它不需要任何参数,我不确定您为什么需要它

请尝试以下代码:

public sealed partial class CustomPopupControl : UserControl
{
    public CustomPopupControl()
    {
        this.InitializeComponent();    

        Debug.WriteLine("CustomPopupControl");
    }

    private void OnPopupLoaded(object sender, RoutedEventArgs e)
    {
        this.Popup_Container.HorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        this.Popup_Container.VerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;
    }

    internal void OpenPopup()
    {
        Debug.WriteLine("OpenPopup");
        Popup_Container.IsOpen = true;

        var currentFrame = Window.Current.Content as Frame;
        var currentPage = currentFrame.Content as Page;
        currentPage.IsHitTestVisible = false;

        Debug.WriteLine("OpenPopup Done");
    }

    private void OnLayoutUpdated(object sender, object e)
    {
        if (Grid_Child.ActualWidth == 0 && Grid_Child.ActualHeight == 0)
        {
            return;
        }

        double ActualHorizontalOffset = Popup_Container.HorizontalOffset;
        double ActualVerticalOffset = Popup_Container.VerticalOffset;

        double NewHorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        double NewVerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;

        if (ActualHorizontalOffset != NewHorizontalOffset || ActualVerticalOffset != NewVerticalOffset)
        {
            Popup_Container.HorizontalOffset = NewHorizontalOffset;
            Popup_Container.VerticalOffset = NewVerticalOffset;
        }
    }
}

谢谢,

除了Thomas的答案之外,您还需要在项目属性页下的构建配置中选中“生成库布局””选项。

我们需要参考的文件:

  • ClassLibrary1(类库名称)文件夹
    • ClassLibrary1.xr.xml
    • CustomPopupControl.xaml
  • ClassLibrary1.dll
  • ClassLibrary1.pri->包资源索引文件
将这些文件复制到任意位置,UWP项目只需在Visual Studio中添加对ClassLibrary1.dll文件的引用,所有这些文件都将自动添加


当我尝试在“InitializeComponent()”方法上使用UserControl时,它只是抛出一个xaml解析异常


添加引用时,可能缺少.pri文件。

当我尝试在“InitializeComponent()”方法上使用UserControl时,它只是抛出一个xaml解析异常。它还提供一个NullReferenceException作为使用库的应用程序中的外部异常。我以前通过库使用过UserControls,从来没有生成或需要任何xbf文件,至少在WP8中是这样。Windows 10 UWP平台出现这种情况。我可以不通过代码直接使用用户控件吗?不在xaml中添加它们,而是动态创建它们??如果是这样的话,为什么我引用了这个项目?我发布的代码被简化了。第二个构造函数确实有一些参数。无论如何,请检查@FranklinChen的答案,我选择了它作为解决方案。谢谢!我自己解决不了这个问题!我从来没有在互联网上找到任何接近这个解决方案的东西。您能给我指的任何资源?在我的例子中,Generate library out是灰色的。如何解决这个问题?我不明白你打算做什么。@franklin我没有“生成库布局”选项。我能做什么?你们解决你们的问题了吗?
public sealed partial class CustomPopupControl : UserControl
{
    public CustomPopupControl()
    {
        this.InitializeComponent();    

        Debug.WriteLine("CustomPopupControl");
    }

    private void OnPopupLoaded(object sender, RoutedEventArgs e)
    {
        this.Popup_Container.HorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        this.Popup_Container.VerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;
    }

    internal void OpenPopup()
    {
        Debug.WriteLine("OpenPopup");
        Popup_Container.IsOpen = true;

        var currentFrame = Window.Current.Content as Frame;
        var currentPage = currentFrame.Content as Page;
        currentPage.IsHitTestVisible = false;

        Debug.WriteLine("OpenPopup Done");
    }

    private void OnLayoutUpdated(object sender, object e)
    {
        if (Grid_Child.ActualWidth == 0 && Grid_Child.ActualHeight == 0)
        {
            return;
        }

        double ActualHorizontalOffset = Popup_Container.HorizontalOffset;
        double ActualVerticalOffset = Popup_Container.VerticalOffset;

        double NewHorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        double NewVerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;

        if (ActualHorizontalOffset != NewHorizontalOffset || ActualVerticalOffset != NewVerticalOffset)
        {
            Popup_Container.HorizontalOffset = NewHorizontalOffset;
            Popup_Container.VerticalOffset = NewVerticalOffset;
        }
    }
}