Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何读取生成操作:资源项_C#_Wpf_Resources_Embedded Resource - Fatal编程技术网

C# 如何读取生成操作:资源项

C# 如何读取生成操作:资源项,c#,wpf,resources,embedded-resource,C#,Wpf,Resources,Embedded Resource,如何读取具有构建操作资源而不是构建操作嵌入资源的资源。资源的布局更好,但我想知道如何在不使用应用程序(WPF)对象的情况下进行此操作。我这样做是为了从资源中收集图像,其中构建操作是从外部应用程序收集资源。但是,我们必须参考System.Windows.Resources并使用应用程序.GetResourceStream() 基本上我们使用以下方法 private static Stream GetResourceStream(string resourcePath) {

如何读取具有构建操作资源而不是构建操作嵌入资源的资源。资源的布局更好,但我想知道如何在不使用
应用程序
(WPF)对象的情况下进行此操作。

我这样做是为了从资源中收集图像,其中构建操作是从外部应用程序收集
资源
。但是,我们必须参考
System.Windows.Resources
并使用
应用程序.GetResourceStream()

基本上我们使用以下方法

    private static Stream GetResourceStream(string resourcePath)
    {
        try
        {

            string s = System.IO.Packaging.PackUriHelper.UriSchemePack;
            var uri = new Uri(resourcePath);
            StreamResourceInfo sri = System.Windows.Application.GetResourceStream(uri);
            return sri.Stream;
        }
        catch (Exception e)
        {
            return null;
        }
    }
现在,它返回一个流,然后可以将其转换为byte[]数组或用于构建其他对象类型

这可以被称为像

            //set variables
            string myAssembly = "Test.Assembly";
            string resourceItem = "resources/myimage.png";

            //get the stream
            using (var bSteam = GetResourceStream(string.Format("pack://application:,,,/{0};component//{1}", myAssembly, resourceItem)))
            {
                //covert the stream to a memory stream and return the byte array
                using (var ms = new MemoryStream())
                {
                    bSteam.CopyTo(ms);
                    return ms.ToArray();
                }
            }
就像我说的,它确实使用了Application.GetResourceStream()。如果你想避免使用这种方法,这个答案可能不合适


干杯,

您尝试过ResourceManager吗?是的。我的意思是,我知道如何从程序集中读取资源,但WPF似乎在构造名称方面做得很好(比嵌入资源更好)。我很想这样做,但在非WPF应用程序中-因此我不能真正使用
应用程序
对象。