【问题标题】:WPF Custom Control Design ErrorWPF 自定义控件设计错误
【发布时间】:2015-07-15 05:40:53
【问题描述】:

我的应用程序运行良好,Visual Studio 中的错误让我发疯。实际错误是:

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

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

主窗口:

<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>

HTML查看器:

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

【问题讨论】:

  • 您是否完全退出了visual studio,问题又出现了?我一直收到“幻影”错误,直到所有 Visual Studio 实例都关闭后才会消失......
  • 是的,我试过了。它通常有效,但这次不行。

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


【解决方案1】:

无法创建“HTMLViewer”的实例。

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

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

为了将可能导致空实例故障的操作活动保持在最低限度,最好将发生此类故障的可能性高的代码隔离开来:

 public HtmlViewer()
 {
     InitializeComponent();

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

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

【讨论】:

  • 当我尝试使用System.ComponentModel.DesignerProperties.IsInDesignModeProperty 时,编辑器会抛出一个错误,提示“!”运算符不能应用于“System.Windows.DependencyProperty”类型的操作数。我是否访问了错误的属性?
  • @MichaelBillingham 已更新,抱歉打错电话。查看更新
猜你喜欢
  • 2013-01-15
  • 2010-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
相关资源
最近更新 更多