2015-02-17 91 views
0

是否可以將外部託管XAML文件加載到WPF項目中? 我想從外部XAML文件中拉出ResourceDictionary。我正在這樣做,所以我可以從應用程序的外部風格應用程序。 我這樣做是因爲我想查看這是否可能,因爲應用程序將在多臺計算機上運行,​​並且我不希望每次需要重新加載上載或加載新的XAML文件時需要進行簡單的更改以按鈕顏色或文字顏色。是否可以將外部託管的XAML文件加載到WPF項目中?

以下是我嘗試過的2個選項,但我始終得知該資源不能是絕對URI。任何關於如何從外部託管源加載我的XAML文件的指針?

嘗試一個

namespace FunWithXaml 
{ 
/// <summary> 
/// Interaction logic for App.xaml 
/// </summary> 
public partial class App : Application 
{ 

public App() 
    { 
     EnsureApplicationResources(); 
     InitializeComponent(); 
    } 


    public static void EnsureApplicationResources() 
    { 

     if (Current == null) 
     { 
      // create the Application object 
      // new Application(); 

      // merge in your application resources 
      Current.Resources.MergedDictionaries.Add(
       LoadComponent(
        new Uri("http://example.com/scripts/XAMLTest.xml", //This can be .xaml or .xml from my understanding if it does not have the x:Class declared in the file. 
         UriKind.RelativeOrAbsolute)) as ResourceDictionary); 
     } 
     else 
     { 
      MessageBox.Show("Welp that didn't work."); 
    } 
    } 

} 

要做兩件。與上面類似,但我嘗試使用WebClient和XamlReader來完成。不知道我是否正確地做了這部分。

var Blob = new Uri("http://example.com/scripts/XAMLTest.xml"); 
      var wc = new WebClient(); 
      var sourceStream = wc.OpenRead(Blob); 
      StreamReader mysr = new StreamReader(sourceStream); 
      FrameworkElement rootObject = XamlReader.Load(mysr.BaseStream) as FrameworkElement; 

      Current.Resources.MergedDictionaries.Add(LoadComponent(rootObject)); //rootobject is giving me red lined error. 

試試三。與Try One類似,但完全取消了「If」,並得到一個錯誤,指出我不能擁有絕對URI。

public static void EnsureApplicationResources() 
    { 
    Current.Resources.MergedDictionaries.Add(
      LoadComponent(
       new Uri("http://example.com/scripts/XAMLTest.xml", 
        UriKind.RelativeOrAbsolute)) as ResourceDictionary); 


    } 

編輯 嘗試四基於由@Leon周以下的建議。但是,我得到一個異常錯誤:

「‘System.InvalidOperationException’類型的未處理的異常在PresentationFramework.dll

發生其他信息:資源字典LoadFrom操作與URI‘http://example.com/scripts/XAMLTest.xml’失敗」。

public App() 
    { 
     AFunction(); 
     InitializeComponent(); 
    } 


    public void AFunction() 
    { 
     var foo = new Uri("http://example.com/scripts/XAMLTest.xml", UriKind.Absolute); 
     Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo }); 
    } 

這是什麼在我的XML文件中我想拉。

<ResourceDictionary> 
<Style x:Key="HeaderStyle" TargetType="{x:Type TextBlock}"> 
<Setter Property="Foreground" Value="Green"/> 
</Style> 
</ResourceDictionary> 

這是我的App.xaml文件

<Application 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="FunWithXaml.App" 
StartupUri="MainWindow.xaml"> 
<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <!--Trying to get dynamic XML/XAML resource to populate here--> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

回答

0

如何:

Application.Current.Resources.MergedDictionaries.Add(
    new ResourceDictionary { Source = uri } 
); 
+0

當我嘗試我得到一個InValidOperationException。 Teh審計信息顯示「ResouceDictionary LoadFrom操作失敗,使用URI'http://example.com/scripts/XAMLTest.xml'」。我用我正在嘗試使用的XAML編輯我的問題。這就是它的全部內容。 – ClosDesign 2015-02-17 23:02:55

+0

如果uri指向一個「xaml」文件而不是「xml」文件,上面的代碼可以工作。是否有任何理由您的資源文件需要「xml」擴展名? – 2015-02-18 00:02:55

+0

我正在使用XML,因爲我已閱讀併成功交換了XML文件和XAML文件,以便在應用程序內使用XML文件中的相同XAML。我只是從XAML中移除了x:Class,所以它沒有引用C#文件。我最終將使用CMS來輸出XML文件,以便CMS中的作者可以編輯樣式,而不必直接編輯XAML本身。這是最終的目標。我將嘗試XAML擴展。感謝您的意見。 – ClosDesign 2015-02-18 15:37:44

0
var uri = new Uri("PathToMyXamlFile"); 
var stream = File.OpenRead(uri.ToString()); 
var resources = (ResourceDictionary)XamlReader.Load(stream); 

我會得到XAML中的字符串轉換成內存流。然後將其交給XamlReader.Load。

+0

你可以擴展你的例子嗎?我對代碼中的位置有點困惑,我會把它放在這裏。它將如何被添加到Application.xaml的MergeDictionaries部分? – ClosDesign 2015-02-18 15:57:56

+0

當我嘗試做你的解決方案時,我在Visual Studio中遇到錯誤,說我無法從'System.Uri'轉換爲'System.IO.Stream',你可以擴展你的建議嗎? – ClosDesign 2015-02-18 21:21:48

相關問題