构造函数显示错误?

时间:2014-01-28 10:18:31

标签: c# wpf

以下是我的应用程序的启动代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using AFICController.ViewModel;


namespace AFICController
{
class App : Application
{
[STAThread()]
static void Main()
{
  Splasher.Splash = new SplashScreen();
  Splasher.ShowSplash();

  Mouse.OverrideCursor = null;
for (int i = 0; i < 5000; i++)
  {
    Thread.Sleep(1);
  }

  Splasher.CloseSplash();
  new App();
 }
/// <summary>
/// 
/// </summary>
public App()
{

  App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(@"\Resources\Dictionary\ATTColors.xaml", UriKind.Relative) });

  App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new U  Uri(@"\Resources\Dictionary\AppButton.xaml", UriKind.Relative) });

  Console.WriteLine("EULA Opened");
  StartupUri = new System.Uri("EULA.xaml", UriKind.Relative);

  //StartupUri = new System.Uri("View/WizardDialog.xaml", UriKind.Relative);


  Run();
}

我有上面的代码来运行我的应用程序。首先我有启动画面看起来很好然后我有一个WPF窗口,它基本上是一个EULA协议,但它编译器引发异常“The invocation of the constructor on type that matches the specified binding constraints threw an exception.” 此异常在上面的代码中的Run()方法中出现。 以下是EULA的WPF代码:

<Window 
x:Class="AFICController.EULA" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res="clr-namespace:AFICController.Resources"
Title="{x:Static res:Strings.WizardWelcomeWindow_Title}"
Width="800"
Height="600"  
WindowStartupLocation="CenterScreen"
Icon="/AFICController;Component/Resources/Images/att_icon.ico"
ResizeMode="NoResize">

以下是EULA.xaml.cs代码:

namespace AFICController
{
/// <summary>
 /// Interaction logic for EULA.xaml
 /// </summary>
 public partial class EULA : Window
 {
  public EULA()
  {
   InitializeComponent();

  //Closing += new CancelEventHandler(EULA_Closing);

  TextRange range;

  var fileName = "AFICController.Resources.EULA.EULA-Formatted activeARC.rtf";

  Assembly assembly = Assembly.GetExecutingAssembly();

  var stream = assembly.GetManifestResourceStream(fileName);

  if (stream == null)
  {
    Console.WriteLine("File Not Found");
  }
  else
  {
    Console.WriteLine("File Found");
  }

  range = new TextRange(EULAParagraph.ContentStart, EULAParagraph.ContentEnd);

  range.Load(stream, System.Windows.DataFormats.Rtf);

  }

 private void AcceptButton_Click(object sender, RoutedEventArgs e)
 {
   WizardDialog wizardDialog = new WizardDialog();

   wizardDialog.Show();

   this.Close();
 }

  private void DeclineButton_Click(object sender, RoutedEventArgs e)
 {
   Application.Current.Shutdown();
 }

任何帮助都会非常明显,因为我已经尝试了所有技术并进行了彻底的搜索?

1 个答案:

答案 0 :(得分:0)

首先确保只在stream不为空时才执行代码

  public EULA()
  {
   InitializeComponent();

  //Closing += new CancelEventHandler(EULA_Closing);

  TextRange range;

  var fileName = "AFICController.Resources.EULA.EULA-Formatted activeARC.rtf";

  Assembly assembly = Assembly.GetExecutingAssembly();

  var stream = assembly.GetManifestResourceStream(fileName);

  if (stream == null)
  {
    Console.WriteLine("File Not Found");
  }
  else
  {
    Console.WriteLine("File Found");

  range = new TextRange(EULAParagraph.ContentStart, EULAParagraph.ContentEnd);

  range.Load(stream, System.Windows.DataFormats.Rtf);
  }

  }

如果您说的是真的,即stream总是为null,那可能是因为该文件不是资源。在您的项目中,转到EULA-Formatted activeARC.rtf - &gt;右键单击 - &gt;属性 - &gt;构建行动 - &gt;选择'嵌入资源'。

你确定fileName是否正确?看起来有点像它可能是一条路径(用/而不是点)?