2015-02-17 86 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

相关问题