简体   繁体   English

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

[英]How to add xbf files to visual studio project

I have created a class library for Windows Universal Platform (Win 10 UWP). 我已经为Windows Universal Platform(Win 10 UWP)创建了一个类库。

The library houses some UserControls. 该库包含一些UserControls。

When I add the dll from this library to a Win 10 UWP app, and use the UserControls, it gives a XamlParseException as stated here in another question I posted 当我将这个库中的dll添加到Win 10 UWP应用程序并使用UserControls时,它会在我发布的另一个问题中给出一个XamlParseException。

But when I reference the whole project, there is no exception and I can use the UserControl. 但是当我引用整个项目时,没有例外,我可以使用UserControl。 This happens supposedly because there are xbf files which are not added to the Win 10 app project when I just reference the dll file. 这可能是因为当我引用dll文件时,有些xbf文件未添加到Win 10应用程序项目中。

In a certain project, I need to add the xbf files manually to the Win 10 app project, I cannot reference the whole project, I can only reference the dll and add the required files. 在某个项目中,我需要手动将xbf文件添加到Win 10应用程序项目中,我无法引用整个项目,我只能引用dll并添加所需的文件。

I tried creating a folder in Visual Studio project and adding the xbf files, and also tried creating folders with different names and copying the xbf files there in the "bin" directory through windows explorer. 我尝试在Visual Studio项目中创建一个文件夹并添加xbf文件,并尝试创建具有不同名称的文件夹,并通过Windows资源管理器将xbf文件复制到“bin”目录中。 But no success. 但没有成功。

So, how do I manuall add xbf files to a Windows 10 UWP project? 那么,我如何手动将xbf文件添加到Windows 10 UWP项目中?

Update 1 :- XAML & Code for reference 更新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 :- 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>

I use the control directly in another app, as - 我在另一个应用程序中直接使用控件,如 -

CustomPopupControl myctrl = new CustomPopupControl();
myctrl.OpenPopup();

In addition to Thomas's answer, you need to check the " Generate library layout " option in the Build configuration under the project's Properties page. 除了Thomas的答案,您还需要在项目的Properties页面下的Build配置中选中“ Generate library layout ”选项。 在此输入图像描述

The files we need to reference: 我们需要引用的文件:

  • ClassLibrary1(Class Library name) Folder ClassLibrary1(类库名称)文件夹
    • ClassLibrary1.xr.xml ClassLibrary1.xr.xml
    • CustomPopupControl.xaml CustomPopupControl.xaml
  • ClassLibrary1.dll ClassLibrary1.dll
  • ClassLibrary1.pri -> Package Resource Index file ClassLibrary1.pri - >包资源索引文件

Copy these files to anywhere and the UWP project just need to add reference to the ClassLibrary1.dll file in the Visual Studio, all of them will be added automatically. 将这些文件复制到任何地方,UWP项目只需要在Visual Studio中添加对ClassLibrary1.dll文件的引用,所有这些文件都将自动添加。

It just throws a xaml parse exception when I try to use the UserControl on the "InitializeComponent()" method 当我尝试在“InitializeComponent()”方法上使用UserControl时,它只抛出一个xaml解析异常

Perhaps the .pri file is missing when you add the reference. 添加引用时可能缺少.pri文件。

Try to define your constructor as public and not internal. 尝试将构造函数定义为public而不是internal。

Also, your second constructor is calling base but i'm not sure to understand why you have/need it if it does not need any parameters. 此外,你的第二个构造函数是调用base,但我不确定为什么你需要它,如果它不需要任何参数。

Try this code: 试试这段代码:

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;
        }
    }
}

Thanks, 谢谢,

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

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