我正在使用MVVM Light模式創建WPF前端。該前端將調用Web服務從應用程序服務器檢索數據。應用程序服務器是通過.dll公開Web方法的專有Vendor應用程序服務器。將MVVM Light與供應商提供的Webservice一起使用
我需要從服務器獲取客戶端會話才能從服務器獲取結果。問題是,當我調用我的模型,它具有對服務器的連接,我收到以下錯誤:
'The invocation of the constructor on type [APP].ViewModel.ViewModelLocator' that matches the specified binding constraints threw an exception.' Line number '12' and line position '10'.
當我拿出
cashaccount.getConnection();
系統繼續併產生行帶有來自WPF Toolkit的數據網格的WPF窗口。
MainWindow.xaml
<Window x:Class="CreditSuisse.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
mc:Ignorable="d"
Height="301"
Width="520"
Title="MVVM Light Application"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<dg:DataGrid ItemsSource="{Binding Path=CashAccount}"
Margin="5" AutoGenerateColumns="False">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Binding="{Binding AcctCd}" Header="Account Code" />
<dg:DataGridTextColumn Binding="{Binding AcctCd}" Header="Security Name" />
<dg:DataGridTextColumn Binding="{Binding QtySod}" Header="Quantity Start of Day" />
<dg:DataGridTextColumn Binding="{Binding QtySod}" Header="Cash Delta (Price Delta)" />
<dg:DataGridTextColumn Binding="{Binding QtySod}" Header="Action" />
</dg:DataGrid.Columns>
</dg:DataGrid>
</Window>
ViewModel.cs
public MainViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
logger.Info("----- Start -----");
cashaccount.getConnection();
}
}
我略爲簡潔起見。
這裏是模型
CashAccount.cs
public class CashAccount : xx.xx.Position
{
private bool cashChanged=false;
private string secName = null;
private xxx.Wsi.ClientSession privateSession;
private static Logger logger = LogManager.GetCurrentClassLogger();
public bool didCashChange
{
get
{
return cashChanged;
}
set
{
this.cashChanged = value;
}
}
public void getConnection()
{
try
{
app.Helper.Session session = new Session();
privateSession = session.getSession();
}
catch (TransportException e)
{
Console.WriteLine("Error communicating with server: " + e.Message);
logger.Info("Couldn't log into xxx...");
logger.Error(e.Message);
}
{
}
}
}
}
我想看看是否服務代理將是一個更好的辦法。如果有人有想法,我將不勝感激。