Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
从资源/WPF访问映像时出现问题_Wpf_Image_Resources_Static - Fatal编程技术网

从资源/WPF访问映像时出现问题

从资源/WPF访问映像时出现问题,wpf,image,resources,static,Wpf,Image,Resources,Static,您好,我无法访问资源中的图像。首先,我向参考资料中添加一个png图像(心脏的名称) 在app.xaml中,将资源作为静态资源放入xaml中 <Application x:Class="Spirit.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/w

您好,我无法访问资源中的图像。首先,我向参考资料中添加一个png图像(心脏的名称)

在app.xaml中,将资源作为静态资源放入xaml中

    <Application x:Class="Spirit.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                 xmlns:local="clr-namespace:Spirit.BootStraper"
                 xmlns:Converters="clr-namespace:Spirit.Converters" 
                 xmlns:Controls="clr-namespace:Spirit.Controls"
                 xmlns:props="clr-namespace:Spirit.Properties" >
        <Application.Resources>
            <ResourceDictionary>

                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary>
                        <local:MefBootStrapper x:Key="bootstrapper" />
                    </ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>

                <props:Resources x:Key="Res"/>

            </ResourceDictionary>
        </Application.Resources>
    </Application>


And use on image from resources on image source.



       <Image Name="TroubleImage"
           Style="{StaticResource InfoIcon}"
           Source="{Binding Source={StaticResource Res}, 
           Path=heart, 
           Converter={StaticResource imageToGrayConverter}}">

If I run app I get this error:
什么是坏的

编辑:

ChatView.XAML

 <Window x:Class="Spirit.Views.ChatView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:Controls="clr-namespace:Spirit.Controls" 
        xmlns:Micro="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
        Icon="/Spirit;component/Images/Logo/Icons/Ico/32.ico"
        Height="545" Width="763"
        Background="{StaticResource LightGrayBackground}">    
    <Grid   Margin="4,4,4,4">        
        <Grid Grid.Column="1" Margin="2,2,2,2">
            <Grid.RowDefinitions>
                <!--<RowDefinition Height="{Binding ElementName=InfoExpander, 
                    Path=IsExpanded, Converter={StaticResource expandedToGridLengthConverter}}" 
                               MaxHeight="220"/>-->
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="25"></RowDefinition>
                <RowDefinition Height="80"></RowDefinition>
            </Grid.RowDefinitions>

            <Image Name="TroubleImage"
                                           Style="{StaticResource InfoIcon}"
                                           Source="{Binding Source={StaticResource Res}, 
                                                            Path=heart, 
                                                            Converter={StaticResource imageToGrayConverter}
                                                  }"/>

        </Grid>
    </Grid>
</Window>

chatview.xaml的第23行是Path=心脏图像名称是heart.png

这是我资源的屏幕,我不知道我做了什么坏事

您正在App.xaml中声明
,但ChatView中发生异常。请发布ChatView.xaml以解决您的问题

编辑:

嗯。我试过了,我有点困惑。我多次遇到相同的异常,我可以确定您应该将
放在ChatView.xaml中。此外,我建议您将此声明放在
部分的末尾

编辑2

天哪,我真是太蠢了。问题是显而易见的!您根本不应该将资源作为静态资源放置!Properties.Resources类的实例是自动创建的!此类公开静态属性。因此您必须通过{x:Static}进行绑定:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    xmlns:self="clr-namespace:WpfApplication1"
    xmlns:props="clr-namespace:WpfApplication1.Properties">
<Window.Resources>
    <self:XmlConverter x:Key="Conv"/>
</Window.Resources>
<StackPanel>
    <Image Source="{Binding Source={x:Static props:Resources.dossier_ardoise_images}, Converter={StaticResource Conv}}"/>
</StackPanel>

您好,我添加了chatview窗口资源,但我得到了与以前相同的错误:{.Thank:)我刚开始学习wpF,我也对这个问题感到困惑。您也最终解决了这个问题,感谢您花时间解决了我的问题。
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    xmlns:self="clr-namespace:WpfApplication1"
    xmlns:props="clr-namespace:WpfApplication1.Properties">
<Window.Resources>
    <self:XmlConverter x:Key="Conv"/>
</Window.Resources>
<StackPanel>
    <Image Source="{Binding Source={x:Static props:Resources.dossier_ardoise_images}, Converter={StaticResource Conv}}"/>
</StackPanel>