如何将 xbf 文件添加到 visual studio 项目
How to add xbf files to visual studio project
我已经为 Windows 通用平台 (Win 10 UWP) 创建了一个 class 库。
库中有一些用户控件。
当我将此库中的 dll 添加到 Win 10 UWP 应用程序并使用 UserControls 时,它给出了一个 XamlParseException,如
所述
但是当我引用整个项目时,没有异常,我可以使用UserControl。据推测,这是因为当我仅引用 dll 文件时,有 xbf 文件未添加到 Win 10 应用程序项目中。
在某个项目中,我需要手动将xbf文件添加到Win 10 app项目中,无法引用整个项目,只能引用dll并添加所需的文件。
我尝试在 Visual Studio 项目中创建一个文件夹并添加 xbf 文件,还尝试创建具有不同名称的文件夹并通过 [=40= 将 xbf 文件复制到 "bin" 目录中】 探险家。但是没有成功。
那么,如何将 xbf 文件手动添加到 Windows 10 UWP 项目?
更新 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>
我直接在另一个应用程序中使用控件,如-
CustomPopupControl myctrl = new CustomPopupControl();
myctrl.OpenPopup();
尝试将您的构造函数定义为 public 而不是内部。
另外,你的第二个构造函数正在调用 base 但我不确定为什么你 have/need 它不需要任何参数。
试试这个代码:
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(Class库名)文件夹
- ClassLibrary1.xr.xml
- CustomPopupControl.xaml
- ClassLibrary1.dll
- ClassLibrary1.pri -> 包资源索引文件
将这些文件复制到任何地方,UWP项目只需要在Visual Studio中添加对ClassLibrary1.dll文件的引用,所有这些都会自动添加。
It just throws a xaml parse exception when I try to use the UserControl on the "InitializeComponent()" method
可能是您添加引用时缺少 .pri 文件。
我已经为 Windows 通用平台 (Win 10 UWP) 创建了一个 class 库。
库中有一些用户控件。
当我将此库中的 dll 添加到 Win 10 UWP 应用程序并使用 UserControls 时,它给出了一个 XamlParseException,如
但是当我引用整个项目时,没有异常,我可以使用UserControl。据推测,这是因为当我仅引用 dll 文件时,有 xbf 文件未添加到 Win 10 应用程序项目中。
在某个项目中,我需要手动将xbf文件添加到Win 10 app项目中,无法引用整个项目,只能引用dll并添加所需的文件。
我尝试在 Visual Studio 项目中创建一个文件夹并添加 xbf 文件,还尝试创建具有不同名称的文件夹并通过 [=40= 将 xbf 文件复制到 "bin" 目录中】 探险家。但是没有成功。
那么,如何将 xbf 文件手动添加到 Windows 10 UWP 项目?
更新 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>
我直接在另一个应用程序中使用控件,如-
CustomPopupControl myctrl = new CustomPopupControl();
myctrl.OpenPopup();
尝试将您的构造函数定义为 public 而不是内部。
另外,你的第二个构造函数正在调用 base 但我不确定为什么你 have/need 它不需要任何参数。
试试这个代码:
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(Class库名)文件夹
- ClassLibrary1.xr.xml
- CustomPopupControl.xaml
- ClassLibrary1.dll
- ClassLibrary1.pri -> 包资源索引文件
将这些文件复制到任何地方,UWP项目只需要在Visual Studio中添加对ClassLibrary1.dll文件的引用,所有这些都会自动添加。
It just throws a xaml parse exception when I try to use the UserControl on the "InitializeComponent()" method
可能是您添加引用时缺少 .pri 文件。