How can I link a dictionary of resources that are loaded externally from disk that are not included in the project or assembly?

I have a ResourceDictionary in a xaml file that represents a skin in a folder on my hard drive in some random folder. Say D: \ Temp2 \ BlackSkin.xaml.

I set permissions correctly to provide full access so that there is no problem.

This ResourceDictionary BlackSkin.xaml refers to BaseSkin.xaml like this:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="{D:\Temp2\BaseSkin.xaml">
</ResourceDictionary.MergedDictionaries>

So, I download this BlackSkin.xaml using XamlReader.Load (...)

It works fine if I put:

Source="D:\Temp2\BaseSkin.xaml"/>

, , . , , (BlackSkin.xaml)... aka "D:\Temp2\BaseSkin.xaml"...or... "BaseSkin.xaml", , , .

? , ? "" - .cs ? XamlReader.Load(...)?

Custom Markup Extension , - ProvideValue?

?

, XamlReader.Load(fileStream) :

public void ApplySkin(string fileName, string baseSkinFileName)
    {
        if (File.Exists(baseSkinFileName))
        {
            ResourceDictionary baseSkinDictionary = null;
            using (FileStream baseSkinStream = new FileStream(baseSkinFileName, FileMode.Open))
            {
                //Read in the BaseSkin ResourceDictionary File so we can merge them manually!
                baseSkinDictionary = (ResourceDictionary)XamlReader.Load(baseSkinStream);
                baseSkinStream.Close();
            }
            using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
            {
                // Read in ResourceDictionary File
                ResourceDictionary skinDictionary = (ResourceDictionary)XamlReader.Load(fileStream);
                skinDictionary.MergedDictionaries.Add(baseSkinDictionary);
                // Clear any previous dictionaries loaded
                Resources.MergedDictionaries.Clear();
                // Add in newly loaded Resource Dictionary
                Resources.MergedDictionaries.Add(skinDictionary);
                fileStream.Close();
            }

, , BaseSkin.xaml BlackSkin.xaml... BaseSkin.xaml?

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="{StaticResource baseSkin}"/> <!--D:\Temp2\BaseSkin.xaml" /-->
</ResourceDictionary.MergedDictionaries>

, , , BlackSkin.xaml, - ? , , , , , , , ResourceDictionary, , , xaml.

.

EDIT:

, , XamlParseException, , , clr, , - - , .

, Xaml, , , , , , , ...?

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:base="clr-namespace:TimersXP.Extensions">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="{base:BaseSkinPath}"/> <!--D:\Temp2\BaseSkin.xaml" /-->
</ResourceDictionary.MergedDictionaries>

, , , , . - ? : http://tech.pro/tutorial/883/wpf-tutorial-fun-with-markup-extensions

using System;
using System.Windows.Markup;

namespace TimersXP.Extensions
{
    class BaseSkin : MarkupExtension
    {
        /// <summary>Gets the base skin file path.</summary>
        /// <value>The base skin.</value>
        public string BaseSkinPath
        {
            get { return "D:\\Temp2\\BaseSkin.xaml"; }
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return "D:\\Temp2\\BaseSkin.xaml";
        }
    }
}
+4
1

, , ResourceDictionary, MergedDictionaries.

:

public class SkinResourceDictionary : ResourceDictionary {
    public static string BaseDirectory { get; set; }

    public new Uri Source {
        get {
            return base.Source;
        }
        set {
            base.Source = BaseDirectory + value;
        }
    }
}

:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1">
<ResourceDictionary.MergedDictionaries>
    <local:SkinResourceDictionary Source="\BaseSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
...

, BaseDirectory;

+1

Source: https://habr.com/ru/post/1525287/


All Articles