2

我已經爲Windows通用平臺(Win 10 UWP)創建了一個類庫。如何將xbf文件添加到Visual Studio項目

庫存有一些用戶控件。

當我從這個庫中添加DLL到10場UWP的應用程序,並使用用戶控件,它給人的規定here in another question I posted

一個XamlParseException但是,當我引用整個項目,沒有例外,我可以使用UserControl。這種情況的發生應該是因爲在我引用dll文件時,沒有將這些xbf文件添加到Win 10應用程序項目中。

在某個項目,我需要手動添加XBF文件到10場的應用程序的項目,我不能引用整個項目,我只能引用的DLL,並添加所需的文件。

我試圖創建在Visual Studio項目文件夾,並添加XBF文件,並嘗試創建具有不同名稱的文件夾,並通過Windows資源管理器的「bin」目錄中有複製XBF文件。但沒有成功。

所以,我怎麼手冊冊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(); 
+0

你解決了你的問題嗎? –

回答

2

除了托馬斯的答案,你需要檢查「生成庫佈局」在構建配置項目的屬性頁下的選項。 enter image description here

的文件,我們需要參考:

  • ClassLibrary1的(類庫名)文件夾
    • ClassLibrary1.xr.xml
    • CustomPopupControl.xaml
  • ClassLibrary1的。 dll
  • Class LIBRARY1。PRI - >包資源索引文件

將這些文件複製到任何地方和UWP項目只需要在Visual Studio中添加引用ClassLibrary1.dll文件,它們都將被自動添加。

,當我嘗試使用用戶控件上「的InitializeComponent()」方法

也許當您添加參考.pri文件丟失,它只是拋出一個XAML解析異常。

+0

謝謝!我自己無法解決這個問題!我從來沒有在互聯網上的任何地方發現任何接近此解決方案你可以指點我的任何資源? – kshitijgandhi

0

儘量將您的構造函數定義爲公共的,而不是內部的。

而且,你的第二個構造函數是調用基礎,但我不知道理解爲什麼你有/需要它,如果它不需要任何參數。

試試這個代碼:

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

感謝,

+0

當我嘗試在「InitializeComponent()」方法上使用UserControl時,它僅引發xaml解析異常。它還會在使用該庫的應用程序中給出NullReferenceException作爲外部異常。至少在WP8中,我通過庫之前使用過UserControls,並且從未生成或需要任何xbf文件。這發生在Windows 10 UWP平臺上。我能不能直接通過代碼使用用戶控件 - 不是在xaml中添加它們,而是動態創建它們?如果是這樣,如果我參考該項目,爲什麼它會工作? – kshitijgandhi

+0

請查看更新的問題 – kshitijgandhi

+0

我已根據您的代碼更新了我的答案。 –

相關問題