是否可以将外部托管的XAML文件加载到WPF项目中?

时间:2015-02-17 20:19:05

标签: wpf xaml resourcedictionary

是否可以将外部托管的XAML文件加载到WPF项目中? 我试图从外部XAML文件中提取ResourceDictionary。我这样做,所以我可以从应用程序外部应用程序。 我这样做是因为我想看看这是否可行,因为应用程序将在多台计算机上运行,​​我不想每次需要进行简单的更改时重新上载或加载新的XAML文件按钮颜色或文本颜色。

下面是我尝试过的两个选项,但我一直认为资源不能是绝对的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 Zhou的建议尝试四。但是我收到了一个异常错误:

“PresentationFramework.dll中发生了'System.InvalidOperationException'类型的未处理异常

其他信息:ResourceDictionary 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>

2 个答案:

答案 0 :(得分:0)

怎么样:

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

答案 1 :(得分:0)

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

我会将Xaml作为String放入内存流中。然后将它交给XamlReader.Load。

相关问题