0

让我这样描述问题我已经创建了一个自定义控件,用户可以通过这种方式使用自定义控件

<ResourceDictionary.MergedDictionaries>
  <ResourceDictionary Source="/customControl;component/ThemeResources/Light.xaml"/>
  <ResourceDictionary Source="/customControl;component/ThemeResources.xaml"/>
</ResourceDictionary.MergedDictionaries>

此方法没有问题,VS IntelliSense 可以识别所有资源。

现在为了更容易使用自定义控件,我使用以下类:

public class Theme : ResourceDictionary
    {
        public Theme()
        {
            if (DesignerHelper.IsInDesignMode)
            {
                MergedDictionaries.Add(new ResourceDictionary
                {
                    Source = new Uri("pack://application:,,,/customControl;component/ThemeResources/Light.xaml")
                });
                MergedDictionaries.Add(new ResourceDictionary
                {
                    Source = new Uri("pack://application:,,,/customControl;component/ThemeResources.xaml")
                });
            }
            else
            {
                UpdateResource();
            }
        }

        private Uri _source;

        public new Uri Source
        {
            get => DesignerHelper.IsInDesignMode ? null : _source;
            set => _source = value;
        }

        
        public string Name { get; set; }

        private void UpdateResource()
        {
            if (DesignerHelper.IsInDesignMode) return;
                MergedDictionaries.Clear();
                MergedDictionaries.Add(new ResourceDictionary
                {
                    Source = new Uri("pack://application:,,,/customControl;component/ThemeResources.xaml")
                });
                MergedDictionaries.Add(new ResourceDictionary
                {
                    Source = new Uri("pack://application:,,,/customControl.Controls;component/ThemeResources/Light.xaml")
                });
        }

        
    }

现在这样我们就可以使用自定义控件了:

<ResourceDictionary.MergedDictionaries>
  <ui:Theme/>
</ResourceDictionary.MergedDictionaries>

现在的问题是IntelliSense没有检测到所有资源但是如果我们自己编写样式名称,我们可以使用资源。

为什么会出现这个问题?如何解决这个问题呢?

4

1 回答 1

2

XAML 智能感知需要在不运行任何代码和/或未构建解决方案的情况下工作。因此它不知道创建 Theme 会用pack://application:,/customControl;component/Themes/Generic.xaml.

于 2021-04-22T12:51:06.083 回答