簡體   English   中英

如何將xbf文件添加到visual studio項目中

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

我已經為Windows Universal Platform(Win 10 UWP)創建了一個類庫。

該庫包含一些UserControls。

當我將這個庫中的dll添加到Win 10 UWP應用程序並使用UserControls時,它會在我發布的另一個問題中給出一個XamlParseException。

但是當我引用整個項目時,沒有例外,我可以使用UserControl。 這可能是因為當我引用dll文件時,有些xbf文件未添加到Win 10應用程序項目中。

在某個項目中,我需要手動將xbf文件添加到Win 10應用程序項目中,我無法引用整個項目,我只能引用dll並添加所需的文件。

我嘗試在Visual Studio項目中創建一個文件夾並添加xbf文件,並嘗試創建具有不同名稱的文件夾,並通過Windows資源管理器將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();

除了Thomas的答案,您還需要在項目的Properties頁面下的Build配置中選中“ Generate library layout ”選項。 在此輸入圖像描述

我們需要引用的文件:

  • ClassLibrary1(類庫名稱)文件夾
    • ClassLibrary1.xr.xml
    • CustomPopupControl.xaml
  • ClassLibrary1.dll
  • ClassLibrary1.pri - >包資源索引文件

將這些文件復制到任何地方,UWP項目只需要在Visual Studio中添加對ClassLibrary1.dll文件的引用,所有這些文件都將自動添加。

當我嘗試在“InitializeComponent()”方法上使用UserControl時,它只拋出一個xaml解析異常

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

嘗試將構造函數定義為public而不是internal。

此外,你的第二個構造函數是調用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;
        }
    }
}

謝謝,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM