我正在尝试从VS2012中的同一解决方案中构建的控制台应用程序执行WPF类库。我已经确定我需要在第一次遇到此错误后执行此操作
无法直接启动具有类库输出类型的项目。 要调试此项目,请将可执行项目添加到此解决方案,该项目引用库项目。将可执行项目设置为启动项目。
这是我添加另一个控制台项目并添加初始WPF作为控制台应用程序的参考。 我也右键单击解决方案>属性>启动项目>我选择了Console项目并选中了“Single startup project”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Interfaces.Connection;
namespace API
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Do I need to do something in here to call the initializecomponent() method from the WPF project???");
Console.ReadLine();
}
}
}
正如代码writeLine所说,我是否需要在控制台应用程序的program.Cs中的Main()方法中执行某些操作来调用该WPF中的initializecomponent? 因为当Main()完全为空时运行解决方案时,它似乎只是运行控制台应用程序。它可以非常快速地打开和关闭命令行。
如果需要,以下是WPF项目的所有代码。
Client.cs
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Interfaces.Connection.ApplicationService;
namespace Interfaces.Connection
{
/// <summary>
/// This class is used to operate on the web service.
/// </summary>
public class Client
{
public ApplicationServiceClient Client { get; private set; }
public string Username { get; set; }
public string Context { get; set; }
public string Password { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Client"/> class.
/// </summary>
/// <param name="client">The client.</param>
public Client(ApplicationServiceClient client)
{
Client = client;
}
/// <summary>
/// Pings the web service.
/// </summary>
/// <returns></returns>
public bool Ping()
{
using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
{
AddCredentials();
PingResponse pingResponse = (PingResponse)Client.Run(new PingRequest());
return pingResponse != null;
}
}
/// <summary>
/// Creates an instance of a given entity.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
/// <param name="pivotMember">The pivot member.</param>
/// <param name="parent">The parent.</param>
/// <returns></returns>
public Instance Create(string entityName, Guid? pivotMember, ParentReference parent)
{
using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
{
AddCredentials();
return Client.Create(entityName, pivotMember, parent);
}
}
/// <summary>
/// Saves the specified instance.
/// </summary>
/// <param name="instance">The instance.</param>
public void Save(Instance instance)
{
using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
{
AddCredentials();
Client.Save(instance);
}
}
/// <summary>
/// Deletes the instance with the specified primary key.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
/// <param name="id">The id.</param>
public void Delete(string entityName, Guid id)
{
using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
{
AddCredentials();
Client.Delete(entityName, id);
}
}
/// <summary>
/// Selects an instance by its primary key.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
/// <param name="id">The id.</param>
/// <returns></returns>
public Instance SelectById(string entityName, Guid id)
{
using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
{
AddCredentials();
return Client.SelectById(entityName, id);
}
}
/// <summary>
/// Executes the given QueryBase and returns the result.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public QueryResult SelectByQuery(QueryBase query)
{
using (OperationContextScope scope = new OperationContextScope(Client.InnerChannel))
{
AddCredentials();
return Client.SelectByQuery(query);
}
}
private void AddCredentials()
{
const string contextNamespace = "http://.com/2011/servicecontracts/headers";
const string contextName = "ContextualPerspective";
const string userNameHeaderName = "UserName";
const string passwordHeaderName = "Password";
MessageHeader activeMember = MessageHeader.CreateHeader(contextName, contextNamespace, Context);
OperationContext.Current.OutgoingMessageHeaders.Add(activeMember);
MessageHeader userNameHeader = MessageHeader.CreateHeader(userNameHeaderName, contextNamespace, Username);
OperationContext.Current.OutgoingMessageHeaders.Add(userNameHeader);
MessageHeader userPasswordHeader = MessageHeader.CreateHeader(passwordHeaderName, contextNamespace, Password);
OperationContext.Current.OutgoingMessageHeaders.Add(userPasswordHeader);
}
}
}
MainWindowXAML(这是窗口)
<Window x:Class="Interfaces.Connection.ConnectionDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="API Connector" Height="Auto" Width="525">
<Grid Background="#F0F0F0">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="10,10,5,5"/>
</Style>
<Style TargetType="Button">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Height" Value="26"/>
<Setter Property="Width" Value="100"/>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="Varasset Server Url" />
<TextBox x:Name="txtVarassetServer" Grid.Column="1" Margin="5,10,10,5" />
<TextBlock Grid.Row="1" Text="Username" />
<TextBox x:Name="txtUsername" Grid.Row="1" Grid.Column="1" Margin="5,5,10,5" />
<TextBlock Grid.Row="2" Text="Member Code" />
<TextBox x:Name="txtContext" Grid.Row="2" Grid.Column="1" Margin="5,5,10,5" />
<TextBlock Grid.Row="3" Text="Password" />
<PasswordBox x:Name="txtPassword" Grid.Row="3" Grid.Column="1" Margin="5,5,10,5" />
<Border Grid.Row="4" Grid.ColumnSpan="2" Margin="10,10,10,0" Height="1" BorderBrush="Gray" BorderThickness="0,1,0,0"/>
<StackPanel Grid.Row="5" Grid.Column="1" Orientation="Horizontal" Margin="10"
HorizontalAlignment="Right" VerticalAlignment="Bottom">
<Button x:Name="btnOk" Click="btnOk_Click">OK</Button>
<Button x:Name="btnCancel" Margin="10,0,0,0" Click="btnCancel_Click">Cancel</Button>
</StackPanel>
</Grid>
MainWindowxaml.cs(其CS)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.SqlServer.Dts.Runtime.Design;
using Microsoft.SqlServer.Dts.Runtime;
namespace Interfaces.Connection
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class ConnectionDialog : Window
{
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionDialog"/> class.
/// </summary>
/// <param name="context">The context.</param>
public ConnectionDialog(ConnectionManagerUI context)
{
InitializeComponent();
DataContext = context;
txtPassword.Password = context.Password;
txtUsername.Text = context.Username;
txtContext.Text = context.Context;
txtServer.Text = context.Address;
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
ConnectionManagerUI manager = ((ConnectionManagerUI)DataContext);
manager.Password = txtPassword.Password;
manager.Username = txtUsername.Text;
manager.Context = txtContext.Text;
manager.Address = txtServer.Text;
Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}
还有更多文件,但我不认为它们在这一点上是相关的。
我也尝试过像main这样调用initializeComponent。
Interfaces.Connection.ConnectionDialog app = new Interfaces.Connection.ConnectionDialog();
app.InitializeComponent();
但它然后提出错误说
Interfaces.Connection.ConnectionDialog'不包含带0个参数的构造函数。
我不确定它所期望的论点。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime.Design;
using Microsoft.SqlServer.Dts.Runtime;
namespace Interfaces.Connection
{
/// <summary>
/// This class extends the class Microsoft.SqlServer.Dts.Runtime.Design.IDtsConnectionManagerUI.
/// </summary>
public class ConnectionManagerUI : IDtsConnectionManagerUI
{
private ConnectionManager _connectionManager;
private IServiceProvider _serviceProvider;
/// <summary>
/// Gets or sets the username.
/// </summary>
/// <value>
/// The username.
/// </value>
public string Username
{
get
{
var property = _connectionManager.Properties["Username"].GetValue(_connectionManager);
return property == null ? string.Empty : property.ToString();
}
set { _connectionManager.Properties["Username"].SetValue(_connectionManager, value); }
}
/// <summary>
/// Gets or sets the context (member code).
/// </summary>
/// <value>
/// The context.
/// </value>
public string Context
{
get
{
var property = _connectionManager.Properties["Context"].GetValue(_connectionManager);
return property == null ? string.Empty : property.ToString();
}
set { _connectionManager.Properties["Context"].SetValue(_connectionManager, value); }
}
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>
/// The password.
/// </value>
public string Password
{
get
{
var property = _connectionManager.Properties["Password"].GetValue(_connectionManager);
return property == null ? string.Empty : property.ToString();
}
set { _connectionManager.Properties["Password"].SetValue(_connectionManager, value); }
}
/// <summary>
/// Gets or sets the address.
/// </summary>
/// <value>
/// The address.
/// </value>
public string Address
{
get
{
var property = _connectionManager.Properties["Address"].GetValue(_connectionManager);
return property == null ? string.Empty : property.ToString();
}
set { _connectionManager.Properties["Address"].SetValue(_connectionManager, value); }
}
/// <summary>
/// Method not implemented.
/// </summary>
/// <param name="parentWindow">The IWin32Window interface of the designer hosting the task.</param>
public void Delete(System.Windows.Forms.IWin32Window parentWindow)
{
throw new NotImplementedException("Delete");
}
/// <summary>
/// Edits an existing connection manager.
/// </summary>
/// <param name="parentWindow">The IWin32Window interface of the designer hosting the task.</param>
/// <param name="connections">The connections available for editing.</param>
/// <param name="connectionUIArg">A class that implements the <see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.ConnectionManagerUIArgs"/>, such as the <see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.FileConnectionManagerUIArgs"/> or <see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.MultiFileConnectionManagerUIArgs"/>.</param>
/// <returns>
/// true if the connection manager was modified.
/// </returns>
public bool Edit(System.Windows.Forms.IWin32Window parentWindow, Microsoft.SqlServer.Dts.Runtime.Connections connections, ConnectionManagerUIArgs connectionUIArg)
{
return new ConnectionDialog(this).ShowDialog().GetValueOrDefault();
}
/// <summary>
/// Initializes the connection manager user interface. This method is called when you need to create connections of a specific type.
/// </summary>
/// <param name="connectionManager">The connection manager to initialize.</param>
/// <param name="serviceProvider">The object used to get registered services. Defines a mechanism for retrieving services offered by the designer for such activities as monitoring changes to components.</param>
public void Initialize(Microsoft.SqlServer.Dts.Runtime.ConnectionManager connectionManager, IServiceProvider serviceProvider)
{
_connectionManager = connectionManager;
_serviceProvider = serviceProvider;
}
/// <summary>
/// Provides notification to tasks of newly created connection managers. This method is called after a new connection manager is added to the package.
/// </summary>
/// <param name="parentWindow">The IWin32Window interface of the designer hosting the task.</param>
/// <param name="connections">The connections collection to add the new connection to, or the connections to show in a drop-down or other control in the user interface.</param>
/// <param name="connectionUIArg">A class that implements the <see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.ConnectionManagerUIArgs"/>, such as the <see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.FileConnectionManagerUIArgs"/> or <see cref="T:Microsoft.SqlServer.Dts.Runtime.Design.MultiFileConnectionManagerUIArgs"/>.</param>
/// <returns>
/// true if a new connection was created.
/// </returns>
public bool New(System.Windows.Forms.IWin32Window parentWindow, Microsoft.SqlServer.Dts.Runtime.Connections connections, ConnectionManagerUIArgs connectionUIArg)
{
return new ConnectionDialog(this).ShowDialog().GetValueOrDefault();
}
}
}
提前致谢!
修改的 我已将program.cs Main()方法更改为:
class Program
{
static void Main(string[] args)
{
ConnectionManagerUI connectionManagerUI = new ConnectionManagerUI;
ConnectionDialog dialog = new ConnectionDialog(ConnectionManagerUI);
}
}
它现在运行,但它给出了错误
PresentationCore.dll中出现未处理的“System.InvalidOperationException”类型异常 附加信息:调用线程必须是STA,因为许多UI组件都需要这个。
如果我需要创建新帖子,或者我可以在发布之前先进行研究。但蒂姆能够解决这个帖子的问题。
编辑
的结尾答案 0 :(得分:1)
虽然我们正在解决真正的问题,但我会继续说你遇到的最后一个问题是ConnectionDialog
的构造函数被定义为
public ConnectionDialog(ConnectionManagerUI context)
{
...
}
了解ConnectionManagerUI对象如何作为参数?我不知道那个类是什么(你没有共享那个代码),但你需要将其中一个传递给构造函数
编辑:现在你已经添加了代码,我可以看到它有一个默认的构造函数,所以我将把代码更改为:
ConnectionManagerUI connectionManagerUI = new ConnectionManagerUI();
ConnectionDialog dialog = new ConnectionDialog(connectionManagerUI);
另外,不要在对话框上调用.InitializeComponent()。应该只从构造函数(它是)中调用它。两次打电话会很糟糕。
那说,这并不能解决你的根本问题,那就是你只想做一个WPF应用程序:)
编辑:仍然像我说的那样,这不是你的根本问题。你不应该这样做,除非你必须这样做,我怀疑你必须这样做。