来自带有ResourceWriter的MemoryStream的动态资源

时间:2015-07-05 23:20:48

标签: c# wpf

我正在使用C#和WPF开发应用程序; 我正在为MemoryStream创建资源。

void yuru (){
    v2Pozisyon = this.transform.position;
    if (Input.GetKey (KeyCode.LeftArrow)) {
        v2Pozisyon.x = this.transform.position.x - hiz;
    }
    if (Input.GetKey (KeyCode.RightArrow)) {
        v2Pozisyon.x = this.transform.position.x + hiz;
    }
    this.transform.position = v2Pozisyon;
}

一切都很好,直到这里,但是,我不知道如何使用这个资源? 你能帮我使用吗?

1 个答案:

答案 0 :(得分:0)

将资源读入流后,需要将其添加到MergedDictionaries中,以使其在应用程序中可用。这方面的一个例子可能如下所示:

var resourceInfo = skinAssembly.GetManifestResourceInfo(resourceName);
if (resourceInfo.ResourceLocation != ResourceLocation.ContainedInAnotherAssembly)
{
    var resourceStream = skinAssembly.GetManifestResourceStream(resourceName);

    using (var resourceReader = new ResourceReader(resourceStream))
    {
        foreach (DictionaryEntry entry in resourceReader)
        {
            if (IsRelevantResource(entry, bamlResourceName))
            {
                skinBamlStreams.Add(entry.Value as Stream);
            }
        }
    }
}

上面的代码来自我的演示应用程序,您可以在http://jsfiddle.net/nmggoqrg/上看到完整的源代码。

protected override sealed void LoadResources()
{
    var skinResolver = PreLoadResources();
    try
    {
        var skinBamlStreams = skinResolver.GetSkinBamlStreams(_fullName, _resourceName);
        foreach (var resourceStream in skinBamlStreams)
        {
            var skinResource = BamlHelper.LoadBaml<ResourceDictionary>(resourceStream);
            if (skinResource != null)
            {
                Resources.Add(skinResource);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        PostLoadResources();
    }
}