【问题标题】:Provide value on 'System.Windows.StaticResourceExtension'在“System.Windows.StaticResourceExtension”上提供值
【发布时间】:2016-06-20 19:11:06
【问题描述】:

在做了很长时间的 winform 之后,我又在做一些 WPF,但我在使用转换器时遇到了一些问题。

我有一个 UserControl,里面有一个 DevExpress 网格:

<UserControl x:Class="X.Y.Z.Views.LogMessagesView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:X.Y.Z.Views"
             xmlns:mvvm="http://prismlibrary.com/"
             xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
             xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
             xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
             xmlns:devExpress="clr-namespace:X.Wpf.Converters.DevExpress;assembly=X.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" mvvm:ViewModelLocator.AutoWireViewModel="True">
    <UserControl.Resources>
        <devExpress:CollectionToCriteriaOperatorConverter x:Key="CollectionToCriteriaOperatorConverter"/>
    </UserControl.Resources>
    <dxg:GridControl  SelectionMode="Row" ItemsSource="{Binding EventsList}" FilterCriteria="{Binding VisibleLevels, Converter={StaticResource CollectionToCriteriaOperatorConverter}, ConverterParameter='Level'}">
        <dxg:GridControl.SortInfo>
            <dxg:GridSortInfo FieldName="TimeStamp" SortOrder="Descending" />
        </dxg:GridControl.SortInfo>
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="TimeStamp" Header="Local Computer Time">
                <dxg:GridColumn.EditSettings>
                    <dxe:TextEditSettings DisplayFormat="G"/>
                </dxg:GridColumn.EditSettings>
            </dxg:GridColumn>
            <dxg:GridColumn FieldName="Level" Header="Level" >
                <dxg:GridColumn.DisplayTemplate>
                    <ControlTemplate>
                        <StackPanel Orientation="Horizontal"  DataContext="{Binding EditValue, RelativeSource={RelativeSource TemplatedParent}}">
                            <!-- Convert severity to image -->
                            <TextBlock Text="{Binding Name}" />
                        </StackPanel>
                    </ControlTemplate>
                </dxg:GridColumn.DisplayTemplate>
            </dxg:GridColumn>
            <dxg:GridColumn FieldName="RenderedMessage" Header="Message"/>
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView ShowFixedTotalSummary="True" AutoWidth="True" Name="view" AllowSorting="False" ShowIndicator="False"  ShowGroupPanel="False" ShowAutoFilterRow="False" AllowColumnFiltering="False" AllowEditing="False" AllowFilterEditor="False" AllowGrouping="False" AllowMasterDetail="False">
                <dxg:TableView.ColumnMenuCustomizations>
                    <dxb:RemoveBarItemAndLinkAction ItemName="BestFitColumns"  />
                </dxg:TableView.ColumnMenuCustomizations>
            </dxg:TableView>
        </dxg:GridControl.View>
    </dxg:GridControl>
</UserControl>

我已经在资源中声明了我的Converter,我在绑定中使用了它。一切都可以编译,但我在运行时遇到了异常:

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  LineNumber=38
  LinePosition=130
  Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '17' and line position '78'.
  Source=PresentationFramework
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at X.Y.Z.Views.LogMessagesView.InitializeComponent() in E:\Dev\WS1\Branches\ReworkedServer\Solution\AAAAAA\Views\LogMessagesView.xaml:line 1
  InnerException: 
       HResult=-2147467263
       Message=The method or operation is not implemented.
       Source=PresentationFramework
       StackTrace:
            at System.Windows.Baml2006.Baml2006SchemaContext.ResolveBamlType(BamlType bamlType, Int16 typeId)
            at System.Windows.Baml2006.Baml2006SchemaContext.GetXamlType(Int16 typeId)
            at System.Windows.Baml2006.Baml2006Reader.Process_ElementStart()
            at System.Windows.Baml2006.Baml2006Reader.ReadObject(KeyRecord record)
            at System.Windows.ResourceDictionary.CreateObject(KeyRecord key)
            at System.Windows.ResourceDictionary.OnGettingValue(Object key, Object& value, Boolean& canCache)
            at System.Windows.ResourceDictionary.OnGettingValuePrivate(Object key, Object& value, Boolean& canCache)
            at System.Windows.ResourceDictionary.GetValueWithoutLock(Object key, Boolean& canCache)
            at System.Windows.ResourceDictionary.GetValue(Object key, Boolean& canCache)
            at System.Windows.StaticResourceExtension.FindResourceInEnviroment(IServiceProvider serviceProvider, Boolean allowDeferredReference, Boolean mustReturnDeferredResourceReference)
            at System.Windows.StaticResourceExtension.TryProvideValueInternal(IServiceProvider serviceProvider, Boolean allowDeferredReference, Boolean mustReturnDeferredResourceReference)
            at System.Windows.StaticResourceExtension.ProvideValueInternal(IServiceProvider serviceProvider, Boolean allowDeferredReference)
            at System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
       InnerException: 

转换器如果重要的话:

public class CollectionToCriteriaOperatorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        String columnName = parameter as String;
        if (string.IsNullOrEmpty(columnName))
        {
            throw new ArgumentNullException(nameof(columnName), "You have to provide a valid column name");
        }

        IEnumerable collection = value as IEnumerable;
        if (collection == null)
        {
            return null;
        }

        return new InOperator(new OperandProperty(columnName), collection.OfType<object>().Select(x => new OperandValue(x)));
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我发现了一些其他的情况,但是他们说转换器应该在使用之前初始化(似乎是这样,对吧?)

我也在用Prism,不知道会不会带来一些其他的问题(这个usercontrol放到view里面)。

我错过了什么?

【问题讨论】:

  • 你能编辑完整的例外吗?它可能会提供线索。
  • 请参阅"Should questions include “tags” in their titles?",其中的共识是“不,他们不应该”!
  • @J4N 您的转换器很好,应该可以正常工作并从我所看到的情况中过滤,一个原因可能是它找不到资源,尝试在同一个项目中声明转换器只是为了解决这个问题而不是从类库中导入它。
  • @Haukinger 这是完全的例外。我刚刚编辑了 1 个命名空间。
  • @AndreasNiedermair 好的,我会在以后的问题中考虑到这一点

标签: c# wpf xaml converter prism


【解决方案1】:

我刚刚发现了问题所在。我的 Visual Studio 崩溃了,Visual Studio 将目标重置为“混合平台”。我不知道它到底为什么不起作用,但我们的一些项目没有这个平台的目标。

我再次将其设置回 32 位,现在一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-24
    • 2012-04-17
    • 2011-04-23
    • 2019-01-26
    • 1970-01-01
    • 2020-04-07
    • 2012-10-01
    • 1970-01-01
    相关资源
    最近更新 更多