WPF自定义控件设计错误

WPF Custom Control Design Error

我的应用程序运行良好,visual studio 中的错误让我抓狂。实际错误是:

Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\emailTemplates'.

我的程序启动,并使用与应用程序相关的目录中的所有 .msg 文件填充一个组合框。就像我说的,它编译并运行良好。我尝试过重建、清洁等,但没有任何效果。清洁似乎可以解决它,直到我重新构建它。怎么回事??

主要Window:

<Window x:Class="abfsEmailGenerator.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom ="clr-namespace:abfsEmailGenerator"
    Title="MainWindow" Height="700" Width="600">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Expander x:Name="emailSelectExpander" Header="Select Email" HorizontalAlignment="Right" Width="592">
        <custom:HtmlViewer></custom:HtmlViewer>
    </Expander>

</Grid>
</Window>

HtmlViewer:

<UserControl x:Class="abfsEmailGenerator.HtmlViewer"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="600" d:DesignWidth="600">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>

    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="28*"/>
        <ColumnDefinition Width="243*"/>
    </Grid.ColumnDefinitions>

    <Label Margin="3" Grid.Row="0">CC:</Label>
    <TextBox x:Name="ccText" Margin="3" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2" />
    <Label Margin="3" Grid.Row="1">Subject:</Label>
    <TextBox x:Name="subjectText" Margin="3" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2"/>
    <WebBrowser Margin="3,3,3,33"  x:Name="bodyBrowser" Grid.Row="2" Grid.ColumnSpan="3" VerticalAlignment="Stretch" Height="500" Grid.RowSpan="2"></WebBrowser>
    <ComboBox x:Name="emailSelector" Grid.Row="3" Grid.Column="0" Margin="3" Grid.ColumnSpan="3" DropDownClosed="emailSelector_DropDownClosed"/>
</Grid>

HtmlViewer 代码:

namespace abfsEmailGenerator
{
/// <summary>
/// Interaction logic for HtmlViewer.xaml
/// </summary>
public partial class HtmlViewer : UserControl
{
    outlook.Application oApp = new outlook.Application();       
    private Dictionary<string, Dictionary<string, dynamic>> emailDict = new Dictionary<string, Dictionary<string, dynamic>>();

    public HtmlViewer()
    {

        InitializeComponent();
        populateCb();



    }


    private void populateCb()
    {
        string emailFolder = AppDomain.CurrentDomain.BaseDirectory + "/emailTemplates";
        emailDict.Clear();
        emailSelector.ItemsSource = null;
        foreach (var file in Directory.EnumerateFiles(emailFolder, "*.msg", SearchOption.AllDirectories))
        {
            ...
        }
    }
}

Cannot create instance of 'HTMLViewer'.

这表明初始化可能是罪魁祸首。

在设计模式下,除了设置控件的基本外观外,您不希望控件尝试做其他工作。

为了将操作活动保持在最低限度,这可能会导致空实例失败,最好将失败可能性很高的代码隔离开来:

 public HtmlViewer()
 {
     InitializeComponent();

     if (!DesignerProperties.GetIsInDesignMode(this)) // If NOT in design mode...do work.
        populateCb();  
 }

根据依赖属性的设置方式,它们可能无法正确处理 null 并导致问题;如果是这样,上面的代码可以应用。