2015-10-23 20 views
4

請考慮創建包含一組自定義控件的庫MyCustomControlsProject的情況。相反放置XAML代碼爲所有這些控制在一個非常大的generic.xaml我想每個控制在自己的XAML文件分離,然後從generic.xaml引用文件使用MergedDictionaries(UWP)拆分generic.xaml文件的正確語法

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="<url_syntax_file_1>" /> 
    <ResourceDictionary Source="<url_syntax_file_2>" /> 
</ResourceDictionary.MergedDictionaries> 

的文件夾結構的解決方案資源管理器(以及在文件系統)是這樣的:

  • MyCustomControlsProject(項目/文件夾)
    • 主題(文件夾)
      • Generic.xaml(文件)
      • CONTROLTEMPLATES(文件夾)
        • MyControl1.xaml(文件)
        • MyControl2.xaml(文件)

在過去,我使用以下語法在Silverlight和WinPhone的Silverlight中執行此操作:

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="/MyCustomControlsProject;Component/Themes/ControlTemplates/MyControl1.xaml"/> 
    <ResourceDictionary Source="/MyCustomControlsProject;Component/Themes/ControlTemplates/MyControl2.xaml"/> 
</ResourceDictionary.MergedDictionaries> 

而對於Windows Phone的8.1使用此語法:

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="ms-appx:///Themes/ControlTemplates/MyControl1.xaml" /> 
    <ResourceDictionary Source="ms-appx:///Themes/ControlTemplates/MyControl2.xaml" /> 
</ResourceDictionary.MergedDictionaries> 

這些語法的作品都不在Win 10(UWP)。嘗試使用這些引出一個運行時異常:

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in MyApplication.exe but was not handled in user code 
WinRT information: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' because the type 'Windows.Foundation.String' cannot be assigned to the type 'Windows.Foundation.Uri'. 

我也試過這種語法,導致相同的異常:

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="ControlTemplates/MyControl1.xaml" /> 
    <ResourceDictionary Source="ControlTemplates/MyControl2.xaml" /> 
</ResourceDictionary.MergedDictionaries> 

有趣的是,似乎有App.xaml中使用沒有問題上面的語法。

有沒有人知道generic.xaml的ResourceDictionary節點中的source屬性中url字符串的正確語法?或者這是UWP還沒有趕上的東西嗎?

+0

您已經添加庫項目引用您的應用程序的項目?嘗試刪除並重新添加引用,然後重試。如果問題仍然存在,您可以共享一個可以在GitHub或在線文件共享中重現問題的示例。 –

+0

你是否在每個'MyControl.xaml'文件中定義了一個'UserControl',或者只是用於控件的數據模板?甚至是具有綁定的數據模板? – Herdo

回答

4

正確的語法是:

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="ms-appx:///MyCustomControlsProject/Themes/ControlTemplates/MyControl1.xaml" /> 
    <ResourceDictionary Source="ms-appx:///MyCustomControlsProject/Themes/ControlTemplates/MyControl2.xaml" /> 
</ResourceDictionary.MergedDictionaries> 
0

你需要的是添加的主題文件夾中的最後一次嘗試:

<ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="Themes/ControlTemplates/MyControl1.xaml" /> 
     <ResourceDictionary Source="Themes/ControlTemplates/MyControl2.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
+0

產生相同的錯誤。 此外,請注意,文件Generic.xaml在主題內,因此使用相對路徑,就像您期望使用「ControlTemplates/MyControl1」一樣。xaml「而不是」Themes/ControlTemplates/MyControl1.xaml「 另外請注意,該異常表明該源代碼不能轉換爲Uri,我想知道是否沒有比Uri更多的指向文件的東西。暗示字符串格式不正確 – Ladi

+0

對不起,我認爲你把它們全部加入到App.xaml中,所以你不需要Themes,但即使這樣,它也適用於我。你嘗試了你的App.xaml' '? – Stam

+0

我沒有app.xaml,因爲我在一個單獨的庫項目中創建這些控件。我不想問問在他們的解決方案中包含項目的人包含自定義控件來編輯他們的app.xaml;我希望單獨的項目是自包含的,如果合併的字典可以在generic.xaml中使用,那麼我的問題就解決了。儘管有sm在指定URL的方式上的所有語法差異。 – Ladi