无法从ResourceDictionary检索自定义组件资源

本文关键字:自定义 组件 资源 检索 ResourceDictionary | 更新日期: 2023-09-27 18:04:11

当我试图从我的ResourceDictionary中拉出我的"JrTaskbarIcon"时,我一直得到一个ResourceReferenceKeyNotFoundException。

这是抛出这个异常的原始代码:

App.xaml.cs

public partial class App : Application
{
    private JrTaskbarIcon taskbarIcon;
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        taskbarIcon = (JrTaskbarIcon) FindResource("taskbarIcon");
    }
    protected override void OnExit(ExitEventArgs e)
    {
        taskbarIcon.Dispose();
        base.OnExit(e);
    }
}

App.xaml

<Application 
    x:Class="JobRequestApprovalNotifier.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ShutdownMode="OnExplicitShutdown">
    <Application.Resources>
        <ResourceDictionary x:Key="TaskbarIcon">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="JrTaskbarIcon.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

JrTaskbarIcon.xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:JobRequestApprovalNotifier">
    <local:JrTaskbarIcon
        x:Key="taskbarIcon"
        x:Name="taskbarIcon"
        ToolTipText="Job Requests"
        IconSource="/JobRequestApprovalNotifier;component/resources/notification.ico"/>
</ResourceDictionary>

JrTaskbarIcon.cs:从hardcode . wpf . taskbarnotification继承TaskbarIcon

public class JrTaskbarIcon : TaskbarIcon
{
    private BackgroundWorker jrMonitorThread;
    public JrTaskbarIcon()
    {
        jrMonitorThread = new BackgroundWorker();
        jrMonitorThread.WorkerReportsProgress = false;
        jrMonitorThread.WorkerSupportsCancellation = true;
        jrMonitorThread.DoWork += JrScan;
        jrMonitorThread.RunWorkerCompleted += JrScanComplete;
        jrMonitorThread.RunWorkerAsync();
    }
    private void JrScan(object sender, DoWorkEventArgs e)
    {
    }
    private void JrScanComplete(object sender, RunWorkerCompletedEventArgs e)
    {
    }
}

我知道错误发生在line:

taskbarIcon = (JrTaskbarIcon) FindResource("taskbarIcon");

在App.xaml.cs文件。我无论如何也无法检索到这个对象。

任何帮助都将非常感激。

问候,

凯尔。

无法从ResourceDictionary检索自定义组件资源

我找到了答案,不幸的是,我花了几个小时的时间把头撞在桌子上,但唉,我终于让它工作了。

我不太确定调用FindResource()的语法,因为我在那里尝试的都不起作用。但是,我能够获得对"资源"的引用,并从那里获得列表中的第一个MergedDictionary,取出其Values collection对象并取出其中的第一个项目。

这是更新后的App.xaml.cs文件:

public partial class App : Application
{
    private JrTaskbarIcon taskbarIcon;
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        ResourceDictionary resources = (ResourceDictionary) FindResource("TaskbarIcon");
        taskbarIcon = (JrTaskbarIcon) resources.MergedDictionaries[0]
                                               .Values
                                               .OfType<object>()
                                               .FirstOrDefault();
    }
    protected override void OnExit(ExitEventArgs e)
    {
        taskbarIcon.Dispose();
        base.OnExit(e);
    }
}

如你所见,不用:

taskbarIcon = (JrTaskbarIcon) FindResource("taskbarIcon");

我得到taskbarIcon使用:

    ResourceDictionary resources = (ResourceDictionary) FindResource("TaskbarIcon");
    taskbarIcon = (JrTaskbarIcon) resources.MergedDictionaries[0]
                                           .Values
                                           .OfType<object>()
                                           .FirstOrDefault();