2016-03-07 52 views
0

我出現以下情況例外,當我嘗試創建棱鏡外殼:無法創建棱鏡外殼

型 「Microsoft.Practices.ServiceLocation.ActivationException」的異常出現在 Microsoft.Practices。嘗試獲取類型MainWindowViewModel的 實例時發生激活錯誤,「」鍵

:ServiceLocation.dll但在用戶 代碼

其他信息沒有處理

這是我的引導程序類:

public class Bootstrapper : UnityBootstrapper 
    { 
     protected override DependencyObject CreateShell() 
     { 
      return Container.TryResolve<MainWindow>(); 

     } 

     protected override void InitializeShell() 
     { 
      Application.Current.MainWindow.Show(); 
     } 
    } 

App類:

public partial class App : Application 
    { 
     protected override void OnStartup(StartupEventArgs e) 
     { 
      base.OnStartup(e); 

      Bootstrapper bs = new Bootstrapper(); 
      bs.Run(); 

      bs.Container.RegisterType<ICustomer, Customer>(); 

     } 

    } 

和App.xaml中:

<Application x:Class="MVVMPractice2.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Application.Resources> 

    </Application.Resources> 

</Application> 

ViewModel類:

public class MainWindowViewModel : BindableBase 
    { 
     //instantiate the model 
     public ICustomer customer; 

     //property for button click command 
     public DelegateCommand UpdateCommand { get; set; } 


     //constructor to instantiate the buttons click command 
     public MainWindowViewModel(ICustomer customer) 
     { 
      this.customer = customer; 


      UpdateCommand = new DelegateCommand(() => {customer.CalculateTax();OnPropertyChanged(() => TaxAmount);}, customer.IsValid); 

     } 


     //this property maps customer name from model to the view 
     public string TxtCustomerName 
     { 
      get { return customer.CustomerName; } 
      set { customer.CustomerName = value; } 
     } 

     //this property maps amount from model to the view 
     public string TxtAmount 
     { 
      get { return Convert.ToString(customer.Amount); } 
      set { customer.Amount = Convert.ToDouble(value); } 
     } 


     //this property maps and transforms color from model to the view 
     public string LblAmountColor 
     { 
      get 
      { 
       if (customer.Amount > 2000) 
       { 
        return "Blue"; 
       } 
       else if (customer.Amount > 1500) 
       { 
        return "Red"; 
       } 
       return "Yellow"; 
      } 
     } 

     //this property maps and transforms married from model to the view 
     public bool IsMarried 
     { 
      get 
      { 
       if (customer.Married == "Married") 
       { 
        return true; 
       } 

       else if (customer.Married == "UnMarried") 
       { 
        return false; 
       } 

       return false; 

      } 

      set 
      { 
       if (value) 
       { 
        customer.Married = "Married"; 
       } 

       else 
       { 
        customer.Married = "UnMarried"; 
       } 
      } 



     } 

     //this property maps tax from model to the view 
     public string TaxAmount 
     { 
      get { return Convert.ToString(customer.Tax); } 

     } 

    } 
} 

查看XAML:

<Window x:Class="MVVMPractice2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:prism="http://prismlibrary.com/" 
     prism:ViewModelLocator.AutoWireViewModel="True" 
     Title="MainWindow" Height="350" Width="525"> <!--PRISM POWER--> 
    <Grid> 
     <Label Content="Customer Name" HorizontalAlignment="Left" Margin="0,0,0,292.8"></Label> 
     <Label Name="lblName" HorizontalAlignment="Left" Margin="108,0,0,292.8" Width="37" Content="{Binding TxtCustomerName}"></Label> 

     <Label Content="Sales Amount" HorizontalAlignment="Left" Margin="0,28,0,264.8"></Label> 
     <TextBox Name="lblAmount" HorizontalAlignment="Left" Margin="101,28,0,264.8" Width="44" Text="{Binding TxtAmount}"></TextBox> 

     <Label Content="Buying Habits" HorizontalAlignment="Left" Margin="0,56,0,236.8"></Label> 
     <Label Name="lblBuyingHabits" HorizontalAlignment="Left" Margin="108,56,0,236.8" Width="52" Background="{Binding LblAmountColor}"></Label> 

     <Label Content="Married" HorizontalAlignment="Left" Margin="0,84,0,208.8" Width="62"></Label> 
     <CheckBox Name="chkMarried" HorizontalAlignment="Left" Margin="102,84,0,208.8" IsChecked="{Binding IsMarried}"></CheckBox> 

     <Label Content="Tax" HorizontalAlignment="Left" Margin="0,112,0,180.8"></Label> 
     <TextBlock Name="lblTax" HorizontalAlignment="Left" Margin="108,117,0,175.8" Width="37" Text="{Binding TaxAmount}"></TextBlock> 

     <Button Name="btnTax" Content="Calculate Tax" Margin="118,158,287.4,123.8" Command="{Binding UpdateCommand}" ></Button> 

    </Grid> 
</Window> 
+0

你可以發佈視圖和viewmodel?而順便說一句,你應登記客戶裏面'ConfigureContainer' ...,並確保'Application.Current.MainWindow'實際設定,'InitializeShell'應該這樣做。 – Haukinger

+0

InnerException可能包含有關錯誤的重要信息。你可以發佈嗎? –

回答

2

ICustomer註冊碰巧創造MainWindowViewModel之後,所以它不能被解決。

移動Container.RegisterType<ICustomer, Customer>();在在引導程序,你很好。它應該看起來像

protected override void ConfigureContainer() 
{ 
    base.ConfigureContainer(); 
    Container.RegisterType<ICustomer, Customer>(); 
} 
+0

謝謝。我這樣做了,但現在它在bs.Run()上失敗......它說:「Microsoft.Practices.Unity.ResolutionFailedException中發生未處理的異常類型的Microsoft.Practices.Unity.dll 附加信息:解析依賴失敗,type =「Microsoft.Practices.ServiceLocation.IServiceLocator」,name =「(none)」。 發生異常時:解析時 異常:InvalidOperationException - 當前類型Microsoft.Practices。 ServiceLocation.IServiceLocator,是一個接口,不能構造,你是否缺少類型映射?......「 – Ivan

+1

看看UnityContainer.ConfigureContainer的來源 - 它註冊了很多東西... – Haukinger

+0

謝謝再次爲您提供幫助。如果你有時間,請看到我面對的另一個問題:http://stackoverflow.com/questions/35846743/cant-navigate-using-prism – Ivan