【问题标题】:Can't create Prism shell无法创建 Prism 外壳
【发布时间】:2016-03-07 19:10:23
【问题描述】:

当我尝试创建 Prism shell 时出现以下异常:

类型异常 'Microsoft.Practices.ServiceLocation.ActivationException' 发生在 Microsoft.Practices.ServiceLocation.dll 但未在用户中处理 代码

附加信息:尝试获取时发生激活错误 MainWindowViewModel 类型的实例,键“”

这是我的引导程序类:

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

        }

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

应用类:

 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>

【问题讨论】:

  • 你能发布视图和视图模型吗?顺便说一句,您应该在 ConfigureContainer... 中注册客户,并确保 Application.Current.MainWindow 确实已设置,InitializeShell 应该这样做。
  • InnerException 很可能包含有关错误的重要信息。可以发一下吗?

标签: c# mvvm prism


【解决方案1】:

ICustomer的注册发生在MainWindowViewModel创建之后,所以无法解析。

在引导程序中将Container.RegisterType&lt;ICustomer, Customer&gt;(); 移动到ConfigureContainer 中,就可以了。它应该看起来像

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

【讨论】:

  • 谢谢。我这样做了,但现在它在 bs.Run() 上失败了......它说:“Microsoft.Practices.Unity.dll 中发生了类型为 'Microsoft.Practices.Unity.ResolutionFailedException' 的未处理异常附加信息:解决依赖项失败,type = "Microsoft.Practices.ServiceLocation.IServiceLocator", name = "(none)"。在解析时发生异常。异常是:InvalidOperationException - 当前类型 Microsoft.Practices.ServiceLocation.IServiceLocator 是接口,无法构造。你是否缺少类型映射?......“
  • 看看UnityContainer.ConfigureContainer的来源——它注册了很多东西......
  • 再次感谢您的帮助。有时间的话,请看我面临的另一个问题:stackoverflow.com/questions/35846743/cant-navigate-using-prism
猜你喜欢
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多