小编isa*_*vis的帖子

带有Unity容器的WPF - 如何注册ViewModels并将其解析为视图

嗨,我想在WPF MVVM应用程序中使用Unity容器.我没有使用Prism,因为它似乎很重.这是应用程序结构.我试图弄清楚如何解决视图到ViewModels和视图模型(服务)的依赖关系.

应用:

查看

MainWindow.xaml
CustomerList.xaml
CustomerDetail.xaml
BookList.xaml
BookDetail.xaml
Run Code Online (Sandbox Code Playgroud)

的ViewModels

MainViewModel

CustomerListViewModel

BoolListViewModel

BookDetailViewModel

CustomerDetailViewModel
Run Code Online (Sandbox Code Playgroud)

图书馆

ICustomerService (AddCustomer, SaveCustomer, GetCustomers, GetCustomer)

CustomerService:ICustomerService

IBookService (GetBooks, GetBook)

BookService:IBookService

IBookReserveService(Reserve, Return)

BookReserveService:IBookReserveService
Run Code Online (Sandbox Code Playgroud)
  1. MainViewModel需要引用ICustomerService和IBookService

  2. CustomerListViewModel需要引用ICustomerService

  3. BoolListViewModel需要引用IBookService

  4. BookDetailViewModel需要引用ICustomerService和IBookReserveService

  5. CustomerDetailViewModel需要引用ICustomerService和IBookReserveService

我在每个视图模型中都有getter setter属性.

我遇到的问题是如何在WPF中使用依赖注入,特别是对于Views和ViewModel.我尝试使用Unity在一个工作正常的控制台应用程序中注册和解决.但我想了解如何为WPF应用程序做到这一点.我试过注册

 container.RegisterType<ICustomerService, CustomerService>()
 container.RegisterType<IBookReserveService, BookReserveService>()
 container.RegisterType<IBookService, BookService>()
Run Code Online (Sandbox Code Playgroud)

并使用container.Resolve();

但我不知道如何判断哪个视图必须使用哪个视图模型并在需要时解决它们而不是应用程序启动时.此外,我不解决应用程序启动中的所有映射.应该在选择菜单(选择客户以查看详细信息,选择书籍以查看详细信息,保存客户,预订簿等)时执行此操作.

我读到的大部分内容都想使用IView和IViewModel.但不确定我是否理解其中的优势.

任何帮助是极大的赞赏.

wpf unity-container

6
推荐指数
1
解决办法
2万
查看次数

当枚举值与字符串值不匹配时,从nHibernate EnumStringType处理异常

我的情况是枚举值作为字符串存储在Db中。检索时,我遇到异常,尝试使用基类为EnumStringType的mytype将字符串转换为枚举。

这是我得到的错误:

NHibernate.HibernateException : Can't Parse Enum4 as MyEnum
Run Code Online (Sandbox Code Playgroud)

例如:来自数据库的值是:“ Enum4”

根据MyEnum的代码,有效的枚举值为:

Enum1 Enum2 Enum3

在代码适应更改之前,以某种方式将Enum4引入到Db中。(我知道发生了疯狂的事情)

异常是正常的,因为我的Enum没有来自数据库的该值。但是我不希望用户得到例外。而是默认为第一个值。(我同意这在某些情况下不行,但是可以防止在我的情况下更为严重的异常)

如果我是对的,则GetInstance是执行从字符串到枚举的转换的方法。是否有某种TryGetXXXX可以解决此问题或如何解决?

谢谢你的时间!

这是我正在解决的Enum代码:

public class EnumMappingBase : EnumStringType
{
    public EnumMappingBase(Type type)
        :base(type)
    { 

    }

    public override object GetInstance(object code)
    {
        return base.GetInstance(code); // Here is where I get the exception. 
        // I am thinking this is where capturing the exception and defaulting must happen. 
        // I wish I had a TryGetInstance() here or may be it is there and I am not …
Run Code Online (Sandbox Code Playgroud)

c# nhibernate

5
推荐指数
1
解决办法
1156
查看次数

Prism Delegate命令不启用按钮

我试图根据用户输入的数据启用和禁用WPF(PRISM)用户控件中的按钮.

In the constructor I do
SubmitCommand = new DelegateCommand<object>(OnSubmit, CanSubmit);


public ICommand SubmitCommand { get; private set; }

private void OnSubmit(object arg)   
{
     _logger.Log(arg.ToString());
}

private bool CanSubmit(object arg) 
{
    return Title.Length > 0; 
}

private string _title="";

public string Title
{
get { return _title; }
set 
{
    if (_title != value)
    {
           _title = value;
       this.RaisePropertyChanged();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我在Xaml中绑定SubmitCommand,如下所示

<Button Content="Submit" Width="100" Command="{Binding Path=SubmitCommand}" CommandParameter="{Binding ElementName=TitleText, Path=Text}" />
Run Code Online (Sandbox Code Playgroud)

问题是当标题值更改时,按钮不会启用.可能是我错过了什么.谢谢你的帮助!

wpf prism

4
推荐指数
1
解决办法
5773
查看次数

WPF文本框删除焦点上的货币格式

是否可以删除焦点上WPF文本框上的货币格式?该应用程序遵循MVVM.

<TextBox HorizontalAlignment="Left"
         Height="23"
         Margin="156,264,0,0"
         TextWrapping="Wrap"
         HorizontalContentAlignment="Right"
         Text="{Binding Amount, StringFormat=c, ValidatesOnNotifyDataErrors=True}"
         VerticalAlignment="Top"
         Width="100" />
Run Code Online (Sandbox Code Playgroud)

文本框没有焦点时我需要格式化.但只有当它具有焦点以便用户轻松编辑时才需要将其删除.删除$的原因是当您选择Tab时,焦点位于$之前.这意味着用户必须再次单击或使用箭头键移动到数字.

当用户选中上述文本框时,应删除货币符号.谢谢你的帮助.

wpf

3
推荐指数
1
解决办法
3028
查看次数

使用Task.Factory.StartNew时,完成的任务会发生什么

我有以下代码,它创建了多个任务来同时处理数据.我想知道完成工作的任务会发生什么变化?他们会自动处理吗?

 var itemListByGroups = items.GroupBy(x => x.SomeValue);
   List<Task> tasks = new List<Task>();

   // Can create 20 to 30 tasks based on the result of the grouping
   foreach (var itemList in itemListByGroups)
   {
    var task = Task.Factory.StartNew(() => 
    {
     // intense processing 
    });
    tasks.Add(task);
   }

   Task.WaitAll(tasks.ToArray());
Run Code Online (Sandbox Code Playgroud)

当有更多项目要处理时,上面的代码会每几秒钟调用一次,我关心的是任务数量会继续增长吗?

c# task-parallel-library

0
推荐指数
1
解决办法
598
查看次数