0

我尝试使用 DesignData 在设计时填充 ItemsControl。

文件 /DesignData/SampleAlerts.xaml 的内容是:

<ext:AlertSource 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:ext="http://schemas.osterwalder.com/extensions"
             mc:Ignorable="d" d:IsDataSource="true" Guid="4D0E0886-575C-42F2-A777-2A36AB826D14" Name="Sample Alerts">
<ext:AlertSource.Alerts>
    <ext:AlertBase Cause="sdlfkhjsdlkfj" Id="0" IsActive="True" IsAutoReset="False" IsOwnerAlive="False" Message="skdjfhsdakjhf" Removal="skjfdhkjsdfhs" Severity="SystemFault" Source="{x:Null}"/>
    <ext:AlertBase Cause="sdlfkhjsdlkfj" Id="0" IsActive="True" IsAutoReset="False" IsOwnerAlive="False" Message="skdjfhsdakjhf" Removal="skjfdhkjsdfhs" Severity="EmergencyStop" Source="{x:Null}"/>
    <ext:AlertBase Cause="sdlfkhjsdlkfj" Id="0" IsActive="True" IsAutoReset="False" IsOwnerAlive="False" Message="skdjfhsdakjhf" Removal="skjfdhkjsdfhs" Severity="ImmediateStop" Source="{x:Null}"/>
    <ext:AlertBase Cause="sdlfkhjsdlkfj" Id="0" IsActive="True" IsAutoReset="False" IsOwnerAlive="False" Message="skdjfhsdakjhf" Removal="skjfdhkjsdfhs" Severity="CycleStop" Source="{x:Null}"/>
    <ext:AlertBase Cause="sdlfkhjsdlkfj" Id="0" IsActive="True" IsAutoReset="False" IsOwnerAlive="False" Message="skdjfhsdakjhf" Removal="skjfdhkjsdfhs" Severity="Warning" Source="{x:Null}"/>
</ext:AlertSource.Alerts>

并且文件 /DesignData/SampleAlerts.xaml 的构建操作设置为 DesignData。

当我尝试在 UI 中使用此设计数据时:

    <CollectionViewSource x:Key="AlertGroups" 
                          mc:Ignorable="d" d:DesignSource="{d:DesignData Source=/DesignData/SampleAlerts.xaml}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Severity"/>
        </CollectionViewSource.GroupDescriptions>
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Severity" Direction="Descending"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

在 VisualStudio 中,一切正常,但在 Visual Studio 设计器中出现以下异常:

The resource 'file:///C:/Users/reto/Documents/Visual Studio 2010/Projects/PPS/HMI/DesignData/SampleAlerts.xaml#3' could not be found. It should be located at 'C:\Users\reto\Documents\Visual Studio 2010\Projects\PPS\HMI\DesignData\SampleAlerts.xaml#3' but the file is not present.
   at MS.Internal.Providers.VSExternalResourceProvider.GetTextModel(String uri, FileModel relativeTo)
   at MS.Internal.Host.Isolation.Adapters.ContextToProtocolAdapter.MS.Internal.Host.Isolation.Protocols.IExternalResourceProtocol.GetTextModel(String uri, IFileModel relativeTo)
   at MS.Internal.Host.Isolation.Protocols.IExternalResourceProtocol.GetTextModel(String uri, IFileModel relativeTo)
   at MS.Internal.Host.Isolation.Adapters.ToExternalResourceAdapter.GetTextModel(String uri, FileModel relativeTo)
   at MS.Internal.Host.MarkupProjectContext.GetMarkupResourceImpl(Uri uri)
   at MS.Internal.Host.MarkupProjectContext.GetMarkupResource(Uri uri)
   at Microsoft.Windows.Design.DocumentModel.MarkupDocumentManagerBase.TreeChangeListener.Microsoft.Windows.Design.DocumentModel.IDocumentTreeConsumer.HandleMessage(DocumentTreeCoordinator sender, MessageKey key, MessageArguments args)
   at Microsoft.Windows.Design.DocumentModel.MarkupDocumentManagerBase.CancelableDocumentTreeCoordinator.RouteMessage[T](IDocumentTreeConsumer consumer, MessageKey`1 key, T args)
   at Microsoft.Windows.Design.DocumentModel.DocumentTreeCoordinator.SendMessage[T](MessageKey`1 key, T args, Boolean isPrivateMessage)
   at Microsoft.Windows.Design.DocumentModel.DocumentTreeCoordinator.SendMessage[T](MessageKey`1 key, T args)
   at Microsoft.Windows.Design.DocumentModel.DocumentTreeCoordinator.QueryValue[TResult,TParameter](QueryKey`2 key, IDocumentTreeConsumer consumer, TParameter parameter)
   at Microsoft.Windows.Design.Platform.ViewProducerBase.MS.Internal.Platform.IResourceResolver.GetMarkupResource(Uri uri)
   at MS.Internal.Platform.CiderProjectContext.ResolveMarkupResource(Uri uri, Func`2 fallbackResolver)
   at Microsoft.Windows.Design.Platform.ViewProducerBase.GetExternalView(String path)
   at Microsoft.Windows.Design.Platform.ViewProducerBase.Microsoft.Expression.DesignModel.DocumentModel.IDocumentRootResolver.GetDocumentRoot(String path)
   at Microsoft.Expression.DesignModel.InstanceBuilders.DesignDataInstanceBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode)
   at Microsoft.Expression.DesignModel.Core.ViewNodeManager.Instantiate(ViewNode viewNode)

而且在 Blend 中,一切构建良好,但设计器显示以下错误: - 类型“AlertSource”是抽象的,必须包含显式值。Ln 1 Col 0 - 成员“警报”无法识别或无法访问。LN 7 栏 4

AlertSource 和 AlertBase 类型是具有以下骨架的抽象类:

public abstract class AlertBase : IAlert
{
    public long Id { get { ... } }

    public abstract object Message { get; }

    public abstract object GetLocalizedMessage(CultureInfo culture);

    public abstract object Cause { get; }

    public abstract object GetLocalizedCause(CultureInfo culture);

    public abstract object Removal { get; }

    public abstract object GetLocalizedRemoval(CultureInfo culture);

    public AlertSource Source { get { ... } }
    AlertSource IAlert.Source { get { ... } }

    public bool IsOwnerAlive { get { ... } }

    public bool IsActive { get { ... } set { ... } }

    public abstract bool IsAutoReset { get; }

    public abstract AlertSeverity Severity { get; }

    public void Acknowledge() { ... }

    public AlertBase(long id, AlertSource owner) { ... }

    public event EventHandler Set;

    public event EventHandler Reset;

    public event EventHandler Acknowledged;
}

public abstract class AlertSource : IAlertSource
{
    public abstract Guid Guid { get; }

    public abstract string Name { get; }

    public ICollection<IAlert> Alerts { get { ... } }

    public AlertSource() { ... }

    public virtual bool Acknowledge(IAlert alert) { ... }

    public event NotifyCollectionChangedEventHandler CollectionChanged;

    public event EventHandler AlertSet;
    public event EventHandler AlertReset;
    public event EventHandler AlertAcknowledged;

    public IEnumerator<IAlert> GetEnumerator() { ... }

    IEnumerator IEnumerable.GetEnumerator() { ... }
}

有人知道如何解决这个问题吗?

4

1 回答 1

0

这只是一个奇怪的 WPF 编译器错误,一旦您构建了项目,它就会消失。即使是一个与 WPF XAML 代码错误无关的错误也可能在 WPF 中导致这种奇怪的错误。

因此,我建议您首先注释 XAML 中导致该错误的所有行,而不是查找阻止项目编译的原始错误。然后取消注释 XAML 中的行。

于 2014-03-20T09:08:01.373 回答