WPF自定义控件设计错误

时间:2015-07-13 20:33:37

标签: c# wpf xaml visual-studio-2013 custom-controls

我的应用程序运行正常,视觉工作室中的错误只会让我发疯。实际错误是:

  

无法找到路径的一部分' C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ emailTemplates'。

我的程序启动,并使用相对于应用程序的目录中的所有.msg文件填充一个组合框。就像我说的,它编译并运行良好。我尝试过重建,清洁等等。清洁似乎修复它,直到我再次构建它。发生了什么事?

enter image description here

主窗口:

<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))
        {
            ...
        }
    }
}

1 个答案:

答案 0 :(得分:2)

  

无法创建&#39; HTMLViewer&#39;。

的实例

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

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

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

 public HtmlViewer()
 {
     InitializeComponent();

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

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