我正在尝试从 VS2012 中相同解决方案中构建的控制台应用程序执行 WPF 类库。我已经确定我需要在第一次遇到此错误后执行此操作
输出类型为类库的项目不能直接启动。为了调试这个项目,将一个可执行项目添加到这个引用库项目的解决方案中。将可执行项目设置为启动项目。
这是当我添加另一个控制台项目并添加初始 WPF 作为控制台应用程序的参考时。我还右键单击解决方案>属性>启动项目>我选择了控制台项目并选中“单个启动项目”
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 项目的所有代码。
客户端.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 组件都需要这个。
如果我需要创建一个新帖子,或者我可以在发布前先进行研究。但是蒂姆能够解决这篇文章的问题。
编辑结束